To create an "if" statement in Power Query, the fastest method is to use the Conditional Column tool in the user interface, which allows you to define if/then/else logic without writing code. For more complex logic involving multiple criteria or mathematical calculations, you can use the Custom Column feature to write an if...then...else expression using the M language. This functionality allows you to categorize data, flag errors, or calculate values based on specific triggers within your dataset.

What You'll Need

  • Microsoft Excel (with Power Query installed) or Power BI Desktop.
  • A dataset loaded into the Power Query Editor.
  • A clear understanding of the logic you want to apply (e.g., "If Sales are greater than 100, then 'High', otherwise 'Low'").

Step-by-Step

Method 1: Using the Conditional Column Interface

This is the most intuitive way to implement basic logic. It provides a fill-in-the-blank style interface that generates the M code for you automatically. Note that menu labels may differ slightly depending on whether you are using Excel or Power BI.

  1. Open the Power Query Editor by going to the Data tab in Excel and selecting Get Data > Launch Power Query Editor, or by clicking Transform Data in Power BI.
  2. Navigate to the Add Column tab on the top ribbon.
  3. Select Conditional Column from the Columns group. A dialog box will appear.
  4. In the New column name field, type the name you want for your resulting column (e.g., "Priority Level").
  5. In the Column Name dropdown menu, select the existing column you want to evaluate (e.g., "Order Value").
  6. Choose an Operator from the dropdown. Common options include "is equal to", "is greater than", "begins with", or "contains".
  7. In the Value field, enter the threshold or text you are comparing against (e.g., "1000").
  8. In the Output field, enter the value that should appear if the condition is true (e.g., "High Priority").
  9. To add more conditions (the "Else If" part of the logic), click Add Clause. Repeat the process of selecting the column, operator, value, and output.
  10. In the final Else box at the bottom, enter the value that should be applied if none of the previous conditions are met (e.g., "Standard Priority").
  11. Click OK. Power Query will generate the new column at the end of your table.

Method 2: Using Custom Columns for Advanced Logic

When you need to compare two different columns against each other or perform a calculation within the result, the Conditional Column dialog is too limited. You must use a Custom Column.

  1. Go to the Add Column tab and select Custom Column.
  2. Give your column a name in the header box.
  3. In the Custom column formula box, you must write the logic using the M language. The syntax is strictly case-sensitive. The basic structure is: if [ColumnName] > 100 then "High" else "Low".
  4. If you need multiple conditions, use else if. For example: if [Sales] > 1000 then "Platinum" else if [Sales] > 500 then "Gold" else "Silver".
  5. If you are comparing two columns, use their names in brackets: if [Actual] > [Budget] then "Over Budget" else "Under Budget".
  6. Click OK. If there is a syntax error, Power Query will notify you immediately; ensure every if has a corresponding then and ends with a final else.

Common Mistakes

The most frequent error when using "if" logic in Power Query is ignoring case sensitivity. In the M language, if, then, and else must all be written in lowercase. If you write IF or Then, the formula will return a syntax error and the column will not be created.

Another common mistake is mixing data types. If your then result is a text string (e.g., "High") but your else result is a number (e.g., 0), Power Query will set the column type to "Any". This can cause issues when you try to perform calculations or sort the data later. Always ensure that all possible outputs of your if statement share the same data type.

Users often forget that Power Query evaluates conditions from top to bottom. If you have a condition for "Greater than 10" and another for "Greater than 100", and you put the "Greater than 10" condition first, any value over 100 will be caught by the first rule and labeled as such, never reaching the "Greater than 100" rule. Always order your conditions from the most specific (strictest) to the most general.

Tips

To keep your Power Query steps clean, avoid creating five different conditional columns to categorize one piece of data. Instead, use a single Custom Column with multiple else if statements. This reduces the number of steps in your Applied Steps pane and improves the performance of the query refresh.

If you find yourself writing a massive list of if...else if statements (for example, mapping 50 different state codes to state names), stop using if statements. Instead, create a separate "Mapping Table" in Excel or a CSV file and use the Merge Queries feature to perform a left outer join. This is much easier to maintain than a long formula; if a mapping changes, you simply update the source table rather than editing the M code.

When dealing with null values, remember that null is not the same as an empty string. If you want to check if a cell is empty, use if [Column] = null then "Empty" else "Not Empty". If you use "", it will only catch cells that contain a zero-length string, not true nulls.

Troubleshooting

If your column is filled with Error cells instead of your expected values, it is usually due to a data type mismatch. For example, if you try to check if a column is "Greater than 100" but that column is currently formatted as "Text", Power Query cannot perform the mathematical comparison. To fix this, go to the step immediately preceding your conditional column and change the data type of the source column to "Decimal Number" or "Whole Number".

If the formula refuses to save and says there is a syntax error despite looking correct, check for missing brackets. Every column reference must be enclosed in square brackets, such as [Amount]. Also, ensure that every if has a matching then and that the entire expression ends with an else. An if statement in Power Query cannot exist without the else part; it must always have a fallback value.

If your results are not appearing as expected (e.g., a value of 150 is being labeled as "Low" instead of "High"), check the order of your clauses. Move the most restrictive condition (the highest number or the most specific text) to the top of the list. If that does not work, check for trailing spaces in your text data. A value of "Completed " (with a space) will not equal "Completed", causing the if statement to skip to the else result. Use the Transform > Trim feature on your source column to remove these invisible spaces before applying the logic.

Frequently Asked Questions

Can I use AND/OR logic in a Power Query if statement?

Yes. In a Custom Column, use the lowercase keywords and and or. For example: if [Status] = "Active" and [Balance] > 0 then "Current" else "Inactive".

Is there a limit to how many "else if" clauses I can add?

There is no hard-coded limit, but performance will degrade and the formula will become difficult to manage if you have dozens of conditions. In those cases, a Merge Query with a lookup table is the professional alternative.

Why does my conditional column show "Error" for some rows?

This usually happens when the operation is performed on a null value or an incompatible data type (e.g., trying to compare a word to a number). Ensure your source columns are typed correctly before adding the conditional column.

Can I use an if statement to change a value in the same column?

Power Query does not "overwrite" cells like Excel formulas do. It creates a new column. To "replace" the original, create the conditional column, then right-click the original column and select Remove. You can then rename the new column to match the original name.

Does the conditional column work with dates?

Yes, but you must ensure the column is set to the Date or Date/Time type. You can then use operators like "is before", "is after", or "is equal to" in the conditional dialog box.