Skip to the workspace
Pebellslearn by building
M Language Basics  ›  Capstone Project

Conditional and Custom Columns

Custom Columns 13 minConditional columns test conditions from top to bottom and stop at the first true one, while custom columns compute any M formula from the current row's columns.

power bi / Conditional and Custom Columns

Explore each scene at your own pace.

01 · The rule

The rule

Conditional and Custom Columns

Meera asks two new questions: how much money did each order line bring in, and how many orders were large ones? The till export has neither a revenue column nor a size label. Power Query can build both — one through a dialog, one with a short formula.

The rule

Conditions are tested top to bottom and the first true one wins.

Conditional Column (Add Column → Conditional Column) opens a dialog of If … Then … rows, Else If rows and a final Else. Behind the dialog it writes an M if expression:

= Table.AddColumn(#"Changed Type", "Order Size", each
    if [Qty] >= 5 then "Large"
    else if [Qty] >= 3 then "Medium"
    else "Small", type text)

Custom Column (Add Column → Custom Column) accepts any M formula. Columns of the current row are written in square brackets:

  • [Qty] * [Price] — the line's revenue.
  • [City] & " - " & [Product] — joins text with &.
  • "Qty: " & Text.From([Qty]) — M never converts types silently, so a number must become text before & can join it.
  • try Number.From([Qty]) otherwise null — attempts a conversion and falls back to null if it fails.

M's keywords are lower-case: if, then, else, and, or, not. Equality is = and inequality is <>. A custom column starts with the type Any (ABC123), so set its type straight away, or give the type as the last argument, as type text does above.

Applied to Leaf & Cup's six January orders:

OrderID City Product Qty Price Line Revenue Order Size
1001 Pune Masala Chai 2 30 60 Small
1002 Mumbai Samosa 2 20 40 Small
1003 Pune Green Tea 2 40 80 Small
1004 Mumbai Masala Chai 4 30 120 Medium
1005 Pune Samosa 5 20 100 Large
1006 Mumbai Green Tea 5 40 200 Large

These six orders are the ones you will model and measure for the rest of the course.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.