Power Query Switch
In this article
Power Query’s Switch function lets you evaluate a value against multiple conditions and return a corresponding result, similar to a nested IF statement but cleaner and more scalable. It’s ideal for categorizing data, mapping codes to labels, or applying business rules during data transformation. You need no special tools beyond Microsoft Excel or Power BI with Power Query enabled — it’s built-in and requires no installation.
What You'll Need
- Microsoft Excel 2016 or later, or Power BI Desktop
- A table loaded into Power Query with at least one column to evaluate
- Basic familiarity with Power Query Editor (opening it, viewing columns)
Step-by-Step
- Open your Excel workbook or Power BI file and load the data into Power Query by selecting any cell in your table, then navigating to Data > Get & Transform > From Table/Range (Excel) or getting your dataset via Get Data (Power BI).
- In the Power Query Editor, locate the column you want to transform using Switch. For example, if you have a column called "StatusCode" with values like "A", "B", "C", you’ll use Switch to convert these into readable labels like "Active", "Pending", "Archived".
- Click on the column header to select it, then go to the Add Column tab and choose Custom Column.
- In the Custom Column dialog box, enter a name for your new column — for example, "StatusLabel".
- In the formula box, type:
switch([StatusCode], "A", "Active", "B", "Pending", "C", "Archived", "Unknown"). Replace "StatusCode" with your actual column name, and adjust the value-result pairs to match your data. - Each pair consists of a value to test and the result to return if matched. The final argument ("Unknown") is the default value if none of the conditions match. Ensure all pairs are separated by commas and enclosed in parentheses.
- Click OK. Power Query will create a new column with the transformed values based on your Switch logic.
- Review the results. If any rows show "Unknown", check your source data for unexpected values — these are cases your Switch statement doesn’t account for.
- Once satisfied, click Close & Load to apply the changes back to your worksheet or dataset.
Common Mistakes
- Forgetting the default value at the end causes errors if unmatched values appear. Always include a final fallback value like "Unknown" or null.
- Using incorrect column names. The expression must reference the exact column name as it appears in Power Query — case-sensitive and spelling-sensitive.
- Typing literal text without quotes. All string values in the formula must be wrapped in double quotes; unquoted text will trigger a syntax error.
- Using Switch on a column with mixed data types. If your source column contains numbers and text, Switch may fail silently. Convert data types first using Change Type or Try/Convert functions.
- Adding too many conditions. While Switch supports many pairs, performance degrades beyond 50–60 conditions. For complex logic, consider merging with a reference table instead.
Tips
- Use Switch for discrete, finite values — like status codes, region IDs, or product categories. For ranges (e.g., sales between $100–$500), use if-then-else logic with comparison operators instead.
- Combine Switch with Text.Lower or Text.Upper to handle inconsistent casing. Example: switch(Text.Lower([Status]), "active", "On", "inactive", "Off", "Unknown")
- Test your Switch logic on a small sample first. Create a copy of your table, filter to 10–20 rows, and apply the transformation to verify behavior before running on full data.
- Use the Advanced Editor to manually write the M code for Switch if you need to reuse the logic elsewhere. The formula will appear as: = Table.AddColumn(#"Previous Step", "StatusLabel", each switch([StatusCode], "A", "Active", "B", "Pending", "C", "Archived", "Unknown"))
- For dynamic mappings (e.g., changing labels monthly), store your value-result pairs in a separate table, then use Table.Join to merge instead of hardcoding Switch values.
Troubleshooting
- If you get a "Formula Error" or "Expression.Error", check for unmatched parentheses, missing quotes, or extra commas. The formula must have an even number of arguments: value-result pairs plus one default.
- If results show "null" or blank, your source column might contain empty cells or non-text values. Use the Transform > Replace Values tool to replace blanks with a recognizable placeholder like "N/A" before applying Switch.
- If the column doesn’t appear after clicking Close & Load, check if you accidentally closed the editor without applying changes. Reopen Power Query and verify the step exists in the Applied Steps pane.
- If performance is slow, limit the number of conditions. Consider using a lookup table with Merge Queries instead for large or frequently changing mappings.
- If you're working in Excel and the new column disappears after refreshing, ensure your source data range hasn’t shrunk. Expand the table range or convert it to an Excel Table (Ctrl+T) to auto-extend.