Sub topic 15
Adding custom and conditional columns
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. You should also know the basics of M from the previous chapter: steps are named, and each step refers to the one before it.
Two ways to add a column
Power Query gives you two main ways to build a new column from existing ones.
- Conditional Column is a dialog on the Add Column tab. You pick a column, a comparison, a value, and a result. It suits simple if/else rules.
- Custom Column takes an M expression. It suits calculations, text building, and logic that the dialog cannot express.
Both create one Table.AddColumn step. The dialog only writes the M for you.
A conditional column
Suppose the cleaned sales table has an Amount column. You want a Size label: "Large" for 1000 or more, "Medium" for 100 or more, and "Small" otherwise.
In the Conditional Column dialog you add each rule in order and set the Else value. Power Query tests the rules from top to bottom and stops at the first match. This is why you list the strictest rule first.
The step it creates looks like this:
let
Source = Excel.CurrentWorkbook(){[Name="SalesClean"]}[Content],
Sized = Table.AddColumn(
Source,
"Size",
each if [Amount] >= 1000 then "Large"
else if [Amount] >= 100 then "Medium"
else "Small",
type text
)
in
SizedIn each ... [Amount], each means "run this for every row", and [Amount] reads that column in the current row.
A custom column
For a custom column, choose Add Column, then Custom Column. Type a name and an M expression. The column list on the right lets you double-click a name to insert it.
let
Source = Excel.CurrentWorkbook(){[Name="SalesClean"]}[Content],
Label = Table.AddColumn(Source, "Label", each [Region] & " - " & [Product], type text)
in
LabelHere & joins text. If Product held numbers, the join would fail, so you would wrap it in Text.From([Product]).
Rules to remember
- M is case-sensitive. Write
if,then, andelsein lowercase, and match column names exactly. - Every
ifneeds anelse. M has no one-armedif. - Use
nullfor "no value", not an empty string, unless you need empty text. - A comparison involving
nulldoes not behave like Excel.null >= 1000returnsnull, notfalse, so a nullAmountmakes the wholeiffail with an error. Handle nulls first, for exampleif [Amount] = null then "Unknown" else ....
Set the type
The last argument of Table.AddColumn is the column type. If you leave it out, the column gets type any and shows the "ABC123" icon. Always set it. In the Custom Column dialog, Power Query does not set it for you, so change the type afterward or add the argument yourself.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Token Comma expected" | Missing comma or wrong bracket | Check the syntax of the expression |
| "The name 'amount' wasn't recognized" | Wrong letter case | Match the column name exactly |
| `Error` in some cells | A null or text value in a number test | Test for `null` first, or use `try ... otherwise` |
| Column shows type `any` | No type argument | Set the type in the step or with the header icon |
Exercise
A conditional column returns an error for some rows. The rule is if [Amount] >= 100 then "High" else "Low". The rows with errors have a blank Amount. Why does this happen, and how do you fix it?
Show answer
A blank cell is null, and comparing null with >= returns null instead of true or false, so the if fails. Test for null first, for example if [Amount] = null then "Unknown" else if [Amount] >= 100 then "High" else "Low". Setting Amount to a number type before the step also helps.