Power Query Switch Statement: How to Use Conditional Logic
In this article
A switch statement in Power Query allows you to evaluate a single column and return different values based on a set of specific matches, acting as a more efficient alternative to deeply nested "if" statements. While Power Query does not have a function literally named "SWITCH" like DAX or SQL, you achieve this result using the Conditional Column interface or the if...then...else syntax in the M formula language. This is essential for categorizing data, mapping codes to names, or creating custom groupings without writing complex code for every single row.
What You'll Need
- Microsoft Excel or Power BI Desktop installed on your machine.
- A dataset loaded into the Power Query Editor.
- A clear mapping of which input values should result in which output values (e.g., "1" becomes "Low", "2" becomes "Medium", "3" becomes "High").
Step-by-Step
Depending on your comfort level with coding, there are two primary ways to implement switch-like logic in Power Query. Labels and menu paths may vary slightly depending on whether you are using Excel or Power BI, or the specific version of the software installed.
Method 1: Using the Conditional Column Interface (No-Code)
- Open the Power Query Editor by selecting Data > Get Data > Launch Power Query Editor in Excel, or Transform Data in Power BI.
- Navigate to the Add Column tab on the top ribbon.
- Click on Conditional Column. A dialog box will appear.
- In the New column name field, type a descriptive name for your new category (e.g., "Priority Level").
- Under the Column Name dropdown, select the column you want to evaluate (the "switch" column).
- Set the Operator to "equals".
- In the Value box, type the specific value you are looking for (e.g., "1").
- In the Output box, type the result you want for that match (e.g., "Low").
- Click Add Clause to create the next match. Repeat the process: select the same column, set operator to "equals", enter the next value (e.g., "2"), and the corresponding output (e.g., "Medium").
- Continue adding clauses until all your specific cases are covered.
- In the final Else box at the bottom, enter a default value for any rows that do not match your criteria (e.g., "Unknown" or "Other").
- Click OK. Power Query will automatically generate the M code and add the new column to your table.
Method 2: Using M Code for Advanced Logic (Formula Bar)
If you have dozens of conditions, the dialog box can become tedious. Writing the logic directly in a Custom Column is often faster.
- Navigate to the Add Column tab and select Custom Column.
- Give your column a name.
- In the Custom column formula box, use the following syntax structure. Note that M is case-sensitive; you must use lowercase
if,then, andelse. - Enter the formula following this pattern:
if [Status] = "A" then "Active" else if [Status] = "I" then "Inactive" else if [Status] = "P" then "Pending" else "Unknown" - Ensure every
ifhas a correspondingthen, and the entire statement ends with a finalelse. - Click OK to apply the transformation.
Method 3: The "Mapping Table" Approach (Best for Large Lists)
When you have 20 or more conditions, a switch statement becomes hard to maintain. The professional alternative is a Merge.
- Create a separate small table (either in your source file or via Enter Data in Power Query) that contains two columns: the Key (the value you are switching on) and the Value (the result).
- Load this mapping table into Power Query.
- Select your main data table and go to the Home tab.
- Click Merge Queries.
- Select the mapping table from the dropdown and click on the matching columns in both previews to link them.
- Click OK. This creates a column of "Tables".
- Click the Expand icon (two arrows) in the top right of the new column and select only the "Value" column from the mapping table.
- Uncheck "Use original column name as prefix" and click OK.
Common Mistakes
- Case Sensitivity: This is the most frequent error. In the M language,
ifis not the same asIF. Similarly, if your data contains "Active" and you search for "active", Power Query will not find a match and will jump straight to theelseresult. - Data Type Mismatches: Trying to match a number to a text string. If your column is set to "Decimal Number" and you write
if [Column] = "1" then...(with quotes), the statement will fail because you are comparing a number to text. Remove the quotes for numeric values. - Missing the Final Else: In a standard Excel formula, you can omit the final value. In Power Query M code, an
ifstatement must end with anelse. If you don't have a specific default, useelse null. - Incorrect Order of Operations: When using operators like "greater than" or "less than" in a conditional column, the order of the clauses matters. Power Query evaluates from top to bottom. If your first rule is "Value > 0" and your second is "Value > 100", the second rule will never be reached because 100 is also greater than 0.
Tips
- Trim Your Data: Before applying a switch statement, right-click the source column and select Transform > Trim. This removes accidental leading or trailing spaces that would cause an "equals" match to fail.
- Use the Mapping Table for Scalability: If your business logic changes frequently (e.g., changing a category name), updating a mapping table in an Excel sheet is much faster than editing a complex M formula in the Power Query editor.
- Combine Logic: You can use
and/orwithin your custom column switch statements to handle multiple conditions at once. For example:if [Region] = "North" and [Sales] > 1000 then "Top North" else ... - Check Data Types First: Always ensure your column types are explicitly set (Text, Whole Number, etc.) before adding conditional columns to avoid unexpected
nullresults.
Troubleshooting
If your switch statement is not producing the expected results, follow these steps in order:
- Check for Nulls: If the input column contains
nullvalues, the switch statement may return an error or a blank. Add a step to Replace Values (replacenullwith "Unknown") before the conditional column step. - Verify Case Consistency: If you suspect case issues, create a temporary step to transform the source column to Uppercase or Lowercase. Then, update your switch statement to match that casing.
- Inspect the Formula Bar: If the Conditional Column UI is behaving strangely, look at the formula bar. Ensure the syntax follows
if ... then ... else if ... then ... else .... Check for missing commas or brackets. - Test with a Single Row: Filter your table to one specific row that you know should match a certain condition. If it still doesn't match, the issue is almost certainly a hidden character (like a non-breaking space) or a data type mismatch.
- Refresh Preview: Sometimes the Power Query preview doesn't update immediately. Click Refresh Preview in the Home tab to ensure you are seeing the latest logic applied to the data.
Frequently Asked Questions
Can I use a switch statement to change values in an existing column?
Power Query is designed to be additive. You cannot "overwrite" a column using the Conditional Column tool; it always creates a new one. To achieve the effect of overwriting, create the new conditional column and then right-click and Remove the original source column. Finally, rename the new column to the original name.
Is there a limit to how many conditions I can add?
There is no hard-coded limit to the number of else if clauses you can add. However, performance will degrade if you have hundreds of conditions in a single formula. For more than 20-30 conditions, the mapping table (Merge) method is significantly faster and easier to manage.
Why is my conditional column returning "null" instead of my "Else" value?
This usually happens if the logic is interrupted by an error in one of the previous clauses or if the else statement itself was left blank. Ensure you have a value entered in the final Else box of the dialog or a final else "Value" in your M code.
Can I switch based on multiple different columns?
Yes. While the Conditional Column dialog usually focuses on one column, the Custom Column (M code) allows you to reference any column in the table. You can write if [ColumnA] = "X" and [ColumnB] = "Y" then "Result" else ... to create complex multi-column logic.
How do I handle ranges (like 0-10, 11-20) in a switch statement?
For ranges, you cannot use the "equals" operator. In the Conditional Column dialog, change the operator to "is less than" or "is greater than". Remember to order these from the most restrictive to the least restrictive (e.g., check for > 100 first, then > 50, then > 0).