An if statement in Power Query uses the syntax if [Column] = "Value" then "Result" else "Other". You type it either in the formula bar of Power Query Editor or through Add Column > Custom Column, and it is written in the M language, so every condition is followed by then and closed with else. If you skip the else, the query does not run. That one rule explains most of the errors beginners hit. Labels in the ribbon can differ slightly between Power BI Desktop and Excel, and between versions, but the Custom Column dialog is the same idea in both.
What You'll Need
- Power BI Desktop, or Excel with Power Query (Data > Get Data), or another tool that hosts Power Query.
- A table already loaded into the Power Query Editor, meaning you have clicked Transform Data rather than closing straight to the worksheet.
- Basic idea of what a column name looks like in your data. Column headers appear in square brackets in the formula, so a column called Order Date becomes
[Order Date]. - Text values wrapped in double quotes, numbers without quotes. Mixing these up is the other common source of errors.
Step-by-Step
- Open the editor. In Power BI Desktop, select Home > Transform Data. In Excel, go to Data > Get Data > Launch Power Query Editor. Confirm your table appears in the left pane and its columns show in the main grid.
- Check the data types first. Click the small icon to the left of each column header and confirm the type is correct. A number column stored as text will not compare properly against a number in your condition, and Power Query will either error or give wrong results. Set the type before you write the logic.
- Add a custom column. Go to Add Column > Custom Column. A dialog opens with a name field and a formula box. Name the new column something readable, like "Order Size".
- Write a simple condition. In the formula box, type:
if [Quantity] > 10 then "Large" else "Small"Note the square brackets around the column name, straight double quotes around text, and the requiredelse. - Handle a second condition. Chain another
ifinside theelse. This is how Power Query does multiple branches, since it has noelseifkeyword:if [Quantity] > 100 then "Huge" else if [Quantity] > 10 then "Large" else "Small"Each branch must still resolve to a value. - Test for blanks before comparing. If a column can be null, guard it early:
if [Quantity] = null then "Missing" else if [Quantity] > 10 then "Large" else "Small"Without the null check, comparisons on null values behave inconsistently depending on the type. - Use and, or, and not for combined logic. Combine conditions with the keywords, not with symbols like &&:
if [Region] = "West" and [Quantity] > 10 then "Priority" else "Standard"Wrap each comparison clearly so the order of evaluation is obvious. - Click OK. A new step appears in the Applied Steps list on the right, and the new column shows in the grid. Scroll through the values to confirm the results look correct before moving on.
- Edit the logic later. Select the step in Applied Steps and click the gear icon next to it to reopen the Custom Column dialog. If you edited the formula in the formula bar instead, the change applies to the selected step only.
- Check the updated query. Use Home > Close & Apply (Excel) or Close & Apply in Power BI to push the new column back to the workbook or model. Nothing appears in your report until you close and apply.
Common Mistakes
- Missing the else. Power Query requires both branches. Even when you want nothing to happen, write
else null. - Curly or smart quotes. Text pasted from a document or email often carries curly quotation marks. Power Query only accepts straight quotes, so retype them.
- Comparing text to a number.
[Amount] > 100fails if Amount is typed as text. Fix the data type in the step before, not inside the formula. - Forgetting square brackets. Referring to a column without brackets, or using the header name with spaces and no brackets, gives a syntax error.
- Assuming the case matters. Text comparisons in Power Query are generally case-insensitive, so "west" and "West" match. If you need an exact case match, compare with a function such as Text.Upper or Text.Lower on both sides.
- Writing nested ifs as separate columns. Chaining inside the
elsekeeps everything in one column and one step, which is easier to read and edit than several custom columns stacked on each other.
Tips
- Build the first condition as a simple two-way if, get it working, then add branches one at a time. Debugging a single condition is far easier than hunting through five nested ones.
- Keep the logic in the Custom Column dialog while learning. It validates syntax as you type, while the formula bar is less forgiving.
- Press the format option in the formula bar (or use line breaks with the shift key in the dialog) to spread long nested conditions across several lines for readability.
- Use
and,or, andnotas words. Power Query is not Excel, soAND()andOR()functions do not exist here. - For simple two-value flags, consider a conditional column instead. Add Column > Conditional Column builds the same if/then/else without typing M code, though it is less flexible for complex logic.
- Name results consistently. Returning "Large" in one branch and "large" in another creates two categories that look the same in a report but count separately.
Troubleshooting
If the formula will not accept, read the error message carefully. "Token Eof expected" usually means a missing else or an unclosed quote. "Expression.SyntaxError" points to a stray bracket, a comma, or a smart quote. Retype the problem line rather than patching it.
If the column returns errors for every row, the data type is almost certainly wrong. Step back to the type icon in the header, set the column to the type you expect, and refresh the preview.
If results look correct but some rows show null unexpectedly, check for blanks and whitespace in the source values. A text value with a trailing space will not equal "West". Trim the column first using Transform > Trim before applying the condition.
If the new column fails to appear after closing, confirm the step was actually added in Applied Steps and that the query was not left unapplied. In Power BI, check the model refresh completed without errors in the bottom status bar.
If a previously working condition now errors after a source refresh, the source schema probably changed, such as a renamed column or a type shift. Fix the missing column name in the formula and reapply the type step.
Frequently Asked Questions
Does Power Query have an else if keyword?
No. You nest a second if inside the else branch: . Each nested if still needs its own else.
What is the difference between a Custom Column and a Conditional Column?
A Conditional Column builds if/then/else logic through a dialog with no code. A Custom Column lets you type any M formula, including nested conditions and functions, so it is more flexible.
Can I use if statements to replace values in an existing column?
Yes. Use Transform > Replace Values for simple mapping, or add a custom column and then remove the original. Avoid editing the source if other steps depend on the original values.
Why does my if statement return an error on blank cells?
Comparisons against null do not always behave as expected. Add an explicit if [Column] = null then ... check as the first branch.
Is the syntax the same in Excel and Power BI?
Yes. Both use the same Power Query engine and M language, so the formula is identical. Only the ribbon location and the close-and-apply button wording differ.