To create an if statement in Power Query M, use the syntax if [Condition] then [Value if True] else [Value if False]. This logic allows you to create new columns based on existing data, categorize values, or handle nulls by defining specific outcomes for different scenarios. Unlike Excel formulas, M is case-sensitive, meaning "if", "then", and "else" must be written in lowercase to function.

What You'll Need

  • Microsoft Excel or Power BI Desktop installed on your machine.
  • A dataset loaded into the Power Query Editor.
  • Basic familiarity with the Power Query interface (the Ribbon and the Formula Bar).

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
  2. Access the Custom Column Dialog: Navigate to the Add Column tab on the top ribbon. Select Custom Column. This opens a dialog box where you can write your M code manually, which provides more flexibility than the basic "Conditional Column" wizard.
  3. Name Your Column: In the New column name field, enter a descriptive name (e.g., "Priority Level" or "Tax Category").
  4. Write the Basic If Statement: In the Custom column formula box, start typing your logic. For example, if you want to flag high sales, you would write: if [Sales] > 1000 then "High" else "Low". Ensure you use the square brackets to reference your existing column names.
  5. Implement Nested If Statements (Else If): If you have more than two possible outcomes, you must use an "else if" chain. The structure looks like this: if [Sales] > 1000 then "High" else if [Sales] > 500 then "Medium" else "Low". Power Query evaluates these in order from left to right; once a condition is met, it stops and returns that value.
  6. Handle Null Values: To prevent errors when dealing with empty cells, add a check for nulls at the beginning of your statement: if [Sales] = null then "No Data" else if [Sales] > 1000 then "High" else "Low".
  7. Validate the Formula: Check the bottom of the Custom Column dialog box. It should say "No syntax errors have been detected." If you see a red error message, check for capitalization or missing brackets.
  8. Apply and Close: Click OK to generate the column. You will see the new data appear in your table. Finally, go to the Home tab and click Close & Apply to load the results back into your spreadsheet or report.

Common Mistakes

The most frequent error is using uppercase letters for the keywords. Writing "IF" instead of "if" will result in a syntax error because the M language is strictly case-sensitive. Another common mistake is forgetting the "else" clause. In Excel, you can omit the value if false, but in Power Query M, every "if" must have a corresponding "else". If you don't want anything to happen in the false scenario, you must explicitly write else null.

Data type mismatches are also common. If your "then" result is a text string (e.g., "High") but your "else" result is a number (e.g., 0), Power Query may assign the column a "Any" data type, which can cause issues in later calculation steps. Always ensure that both the true and false outcomes return the same type of data (either both text, both numbers, or both dates).

Users often confuse the "Conditional Column" GUI with the "Custom Column" M code. While the GUI is easier for simple logic, it cannot handle complex operations like comparing two different columns using a mathematical formula or calling other M functions within the logic. When you need advanced logic, you must switch to the Custom Column formula box.

Tips

To keep your code readable, especially with long nested if statements, you can use the Advanced Editor to add line breaks. While the Custom Column box is a single line, the Advanced Editor allows you to format your code like this:

if [Status] = "Complete" then 1 else if [Status] = "Pending" then 2 else if [Status] = "Delayed" then 3 else 4

For scenarios where you have a very long list of conditions (e.g., mapping 50 different state codes to state names), avoid using a massive if statement. Instead, create a separate mapping table in Excel or a CSV file and use a "Merge Queries" operation. This is much easier to maintain than a 50-line if statement and allows you to update the mappings without editing the M code.

Combine your if statement with other M functions for more power. For example, you can use inside an if statement to categorize products based on keywords: .

Troubleshooting

If your column is filled with "Error" values instead of your expected results, the first step is to click in the white space next to the word "Error" in a cell. A detailed error message will appear at the bottom of the screen. If it says "We cannot convert the value... to type Logical", it means your condition (the part between "if" and "then") is not returning a True or False result. Check that you are using comparison operators like =, >, <, >=, or <=.

If the formula is syntactically correct but the results are wrong, check for leading or trailing spaces in your text data. For example, if a cell contains "Complete " (with a space), the condition if [Status] = "Complete" will return False. To fix this, apply a "Trim" transformation to the column before adding the custom if statement.

If you encounter a "Token Literal expected" error, you likely have a typo in your keywords or a missing quote mark around a text string. Ensure every opening quote " has a closing quote " and that all column names are enclosed in square brackets [ ].

Frequently Asked Questions

Can I use AND/OR logic in an M if statement?

Yes, but you don't use a function like AND(). Instead, you use the lowercase operators and and or. For example: if [Sales] > 1000 and [Region] = "North" then "Bonus" else "No Bonus".

Is there a SWITCH function in Power Query M?

M does not have a function named "SWITCH" like DAX does. The equivalent is using the nested "else if" structure described in the step-by-step section, or creating a custom function/mapping table for more complex lookups.

How do I handle case-insensitive text comparisons?

Since M is case-sensitive, "Apple" does not equal "apple". To make your if statement case-insensitive, wrap the column reference in a text transformation function: .

Can I reference the same column multiple times in one statement?

Yes, you can reference the same column as many times as needed across different "else if" branches to create ranges, such as checking if a number is between 10 and 20: if [Value] >= 10 and [Value] <= 20 then "In Range" else "Out of Range".

What happens if I leave out the 'else' part?

The formula will fail and return a syntax error. The M language requires a final "else" to ensure that every possible input has a defined output. If no specific value is needed for the false condition, use else null.