Power Query's if-then logic lets you create custom columns that change values based on conditions, such as categorizing sales data, flagging outliers, or cleaning inconsistent entries. This is done using M language syntax within the Add Column feature and requires no coding experience — just familiarity with your data structure. You must have data loaded into Power Query in Excel or Power BI before you begin.

What You'll Need

  • Microsoft Excel 2016 or later, or Microsoft Power BI Desktop
  • A table or query already loaded into Power Query Editor
  • Basic understanding of column names and data types in your dataset

Step-by-Step

  1. Open your Excel workbook or Power BI file and navigate to the Data tab (Excel) or Home tab (Power BI), then select Get Data or Transform Data to open Power Query Editor.
  2. Locate the query you want to modify in the Queries pane on the left. Click it once to load its data into the main grid.
  3. Decide which column you want to evaluate and what condition you’re testing. For example, if you have a SalesAmount column and want to label rows as High, Medium, or Low based on thresholds, note the values: over 1000 = High, 500–1000 = Medium, under 500 = Low.
  4. Go to the Add Column tab in the ribbon. Click Custom Column. A dialog box will appear.
  5. In the New column name field, type a clear name like SalesCategory.
  6. In the Custom column formula box, enter the M language if-then expression. For the example above, type: if [SalesAmount] > 1000 then "High" else if [SalesAmount] >= 500 then "Medium" else "Low". Be precise: use square brackets around column names, double quotes around text values, and ensure all then and else keywords are lowercase.
  7. Click OK. Power Query will generate the new column with the categorized values based on your condition. The column will appear at the far right of your table.
  8. Review the new column for accuracy. If you see errors or unexpected results, click on the column header and check the data type (it should be Text for labels). If the column shows Expression.Error, revisit your formula.
  9. Once satisfied, click Close & Load (Excel) or Close & Apply (Power BI) to return the updated table to your worksheet or report.

Common Mistakes

  • Using parentheses instead of square brackets around column names — e.g., (SalesAmount) instead of [SalesAmount] — causes the formula to fail because Power Query expects field references in square brackets.
  • Forgetting to quote text values with double quotes. Writing else High instead of else "High" triggers a syntax error because Power Query treats unquoted text as a column name, not a literal string.
  • Mixing case in keywords. If or Else with capital letters will not work. M language is case-sensitive; only if, then, and else are valid.
  • Not handling null values. If your source column contains blank or null entries and your formula doesn’t account for them, the result may be blank or throw an error. Use if [SalesAmount] = null then "Missing" else ... to manage these cases.
  • Using commas instead of semicolons in some regional Excel settings. If your system uses semicolons as list separators, the formula must use if [SalesAmount] > 1000 then "High"; else if ... — but this is rare in modern versions.

Tips

  • Test your logic on a small subset first. Duplicate your query (right-click in the Queries pane > Duplicate) and experiment on the copy to avoid breaking your original data.
  • Use the Formula Bar at the top of Power Query Editor to edit long or complex conditions. It shows syntax highlighting and helps spot missing quotes or brackets.
  • For multiple conditions, nest else if statements rather than using and or or unless necessary. Nested logic is easier to read and debug.
  • Always convert the result column to Text type if you’re returning labels. Right-click the column header > Change Type > Text.
  • Save your workbook or report frequently. Power Query transformations are applied on load, so if you close without saving, your changes are lost.

Troubleshooting

If your custom column doesn’t appear, shows errors, or returns blank values, follow these steps:

  1. Check for typos in the formula. Even a single missing quote or bracket breaks the entire expression. Compare your formula to working examples.
  2. Verify the column name in your formula matches the exact spelling and case in your data. Column names are case-sensitive and must match exactly — including spaces or underscores.
  3. Look for data type mismatches. If you’re comparing a text column to a number (e.g., [Status] = 1), it will fail. Convert the column to Number or Text as required using the Transform tab before applying the condition.
  4. If you see Expression.Error, click the error icon next to the row to see details. It often says something like Cannot convert value null to type Text — this means your condition didn’t account for nulls.
  5. Try simplifying the formula. Start with a basic if [Column] > 0 then "Yes" else "No" to confirm the feature works, then build complexity gradually.
  6. Restart Excel or Power BI if the editor freezes or behaves erratically. Sometimes the Power Query engine needs a fresh start.

Frequently Asked Questions

Can I use if-then with date columns?

Yes. Use the same syntax: if [OrderDate] < #date(2024,1,1) then "Old" else "New". Always use the #date(year,month,day) format for literal dates.

Can I reference another custom column in my if-then formula?

Yes. Once a custom column is created, it can be used in subsequent custom columns. Just use its name in square brackets. However, you cannot reference a column created later in the same step.

Does if-then work with numbers, text, and boolean values?

Yes. You can compare numbers (>=, <), text (=, <>), and boolean values (true, false). For example: if [IsActive] = true then "Active" else "Inactive".

Why does my formula work in one row but not others?

This usually happens when data types are inconsistent across rows. One row might be text, another numeric. Use the Transform tab to change the entire column’s data type before applying the condition.

Can I use if-then to delete rows instead of adding a column?

No, if-then only creates or modifies values. To filter rows, use the Filter tool in the column header or use Table.SelectRows in Advanced Editor with a condition like each [SalesAmount] > 500.

What if I have more than three conditions?

Power Query supports unlimited nested conditions. Just keep adding else if [Condition] then [Value] before the final else. Keep formatting clean with line breaks in the Formula Bar for readability.