Sub topic 11
Adding conditional and custom columns
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. It also assumes you have seen basic M syntax in the formula bar. Here you will add new columns that are calculated from existing ones, first with the dialog and then by editing the M code.
Two ways to add a column
The Add Column tab offers two common choices.
- Conditional Column opens a dialog where you pick a column, an operator, a value, and a result. You can add
Else ifbranches and a finalElse. - Custom Column opens a formula box where you write an M expression yourself.
Both generate a Table.AddColumn step. Conditional Column is quicker for simple labels. Custom Column is needed when the logic uses several columns, text functions, or calculations.
Reading the generated M
The example below splits a full name and then labels each order as Large or Small. The last step is the one a conditional column creates.
let
Source = #table({"FullName", "Amount"}, {{"Ada Lovelace", 1500}, {"Alan Turing", 300}}),
SplitName = Table.SplitColumn(Source, "FullName", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"FirstName", "LastName"}),
AddedSize = Table.AddColumn(SplitName, "OrderSize", each if [Amount] >= 1000 then "Large" else "Small", type text)
in
AddedSizeTable.AddColumn takes four arguments:
- The table to add to, here the previous step
SplitName. - The new column name,
"OrderSize". - A function that runs once per row. The
eachkeyword starts that function, and[Amount]reads the Amount value of the current row. - An optional type,
type text.
The result looks like this:
| FirstName | LastName | Amount | OrderSize |
|---|---|---|---|
| Ada | Lovelace | 1500 | Large |
| Alan | Turing | 300 | Small |
Common errors
- Missing type argument. Without
type text, the new column has typeany. Set a type, either in the formula or with a later type-change step. - Case sensitivity. M is case sensitive.
if,then, andelsemust be lowercase, and[amount]does not match[Amount]. - Comparing text to numbers. If Amount is still text,
[Amount] >= 1000raises an error. Set data types first. - Nulls. A comparison such as
null >= 1000returnsnull, notfalse, so theelsebranch is not what you might expect. Handle nulls explicitly, for exampleif [Amount] = null then "Unknown" else if [Amount] >= 1000 then "Large" else "Small". - Step order. The new column can only use columns that exist in the previous step. If you rename or remove a column earlier in the query, a later formula that refers to it fails.
Writing a custom column
In the Custom Column dialog you type only the expression, not the whole Table.AddColumn call. For example, this expression builds a label from two columns:
[LastName] & ", " & [FirstName]If you work with a number and text together, convert the number first, for example Text.From([Amount]).
Exercise
A query has an Amount column of type number. You want a column named Tier that is "High" when Amount is at least 1000, "Mid" when it is at least 100, and "Low" otherwise. Write the Table.AddColumn step, assuming the previous step is named ChangedType.
Show answer
AddedTier = Table.AddColumn(ChangedType, "Tier", each if [Amount] >= 1000 then "High" else if [Amount] >= 100 then "Mid" else "Low", type text)