Skip to content

Creating conditional columns

A conditional column fills each row with a value chosen by a set of if-then rules. Power Query has a dialog for building these rules, so a common need such as grouping values into bands takes no M at all.

This page adds an age band to a customer table, using these bands:

A list of six groups: 18 and under, over 18 to 30, over 30 to 40, over 40 to 50, over 50 to 60, and over 60

A banded column like this works well as a slicer or a grouping in any report built on the data.

The data is the customer table from Microsoft’s AdventureWorks sample database, loaded into Power Query. Conditional Column sits in the General group of the Add Column tab:

The Add Column tab of the Power Query Editor, with Conditional Column at the top of the General group, above the DimCustomer table

The bands need an age to test, so the first job is to work one out from the birth date:

  • Select the Birth Date column, then on the Add Column tab choose Date, then Year, then Year. This adds a column holding the year of birth, and keeps the original Birth Date column.

The Date menu on the Add Column tab, with Year and then Year selected for the Birth Date column

  • Edit the new step’s code in the formula bar. Power Query writes something like this:
= Table.AddColumn(_Customers, "Custom", each Date.Year([BirthDate]), type number)
  • Change it to this, which subtracts the year of birth from the current year and names the column Age:
= Table.AddColumn(#"Removed Columns", "Age", each Date.Year(DateTime.LocalNow()) - Date.Year([Birth Date]), Int64.Type)

The result is only approximate, because it ignores whether the customer has had this year’s birthday yet, but it is close enough for this example.

  • Click Conditional Column, which opens the Add Conditional Column dialog:

The empty Add Conditional Column dialog, with a New column name box, one If row of Column Name, Operator, Value and Output, an Add Clause button and an Else box

  • Name the column Age Band, and use Add Clause to build one rule for each band, choosing the column, the operator, the value and the output for each:

The dialog filled in for Age Band, testing Age from the top down: greater than 60 gives 60+, then greater than 50, 40, 30 and 18 give their bands, and less than or equal to 18 gives 18 and Under

The order of the rules matters. Power Query tests them from the top down and stops at the first one that is true, so each rule only has to set the lower limit of its band. Starting from the highest band means that by the time a row reaches is greater than 50, any age over 60 has already been caught.

Click OK, and the new Age Band column appears beside Age:

The customer table with an Age column and an Age Band column, showing bands such as >40 and <=50 and >50 and <=60

The same column could always have been written by hand as a custom column, with if … then … else in M. The dialog writes that M for you, which makes the technique open to anyone, whether or not they know the language.