Stop 07 · DAX FundamentalsTopics · 9/10

IF and SWITCH in DAX

Part of the Power BI Roadmap

In short

Label stores as High, Medium or Low performers, show a warning when stock is low, pick a measure from a slicer: all of these need decisions, and IF and SWITCH are how DAX makes them.

IF returns one of two results based on a condition, and SWITCH handles many cases cleanly. SWITCH ( TRUE (), ... ) is a common pattern for ranges.

The jaadu pulls a big lever at a railway junction where one track splits into several, sending a toy train down the chosen line.

Think of it like this

A railway points switch

A train reaches a junction. The pointsman checks its destination and sets the track: to Chennai this way, to Hyderabad that way, everything else to the yard. IF is a two-way junction. SWITCH is a junction with many tracks, checked in order.

Watch it happen

SWITCH ( TRUE (), ... ) for a 22% margin

>= 30%
>= 15%
>= 0%
else

1/4Is 22% >= 30%? No. Move to the next condition.

The big ideas

What you need to know

IF ( test, then, else )

If you leave out else, DAX returns blank when the test is false.

SWITCH for exact values

SWITCH ( Store[Region], "North", 1, "South", 2, 0 ) matches a value against a list.

SWITCH ( TRUE (), ... ) for ranges

Each condition is checked in order and the first true one wins. Cleaner than nested IFs.

Blanks in comparisons

Blank compared as a number acts like 0, so a store with no sales can fall into a low band. Check ISBLANK first if that matters.

See it in code

Walk through the code

dax
1Margin Band =2VAR m = [Margin %]3RETURN4    SWITCH (5        TRUE (),6        ISBLANK ( m ), BLANK (),7        m >= 0.30, "Excellent",8        m >= 0.15, "Healthy",9        m >= 0, "Thin",10        "Loss"11    )12 13Low Stock Flag =14IF ( [Stock Qty] < 20, "Reorder" )

1/5Store the margin once in a variable so it isn't recalculated for each test.

A banding measure with SWITCH ( TRUE () ) and a simple IF with no else.

Save yourself the pain

Common mistakes

Oops

Nesting five IFs inside each other.

Do this instead

Use SWITCH ( TRUE (), ... ). It reads top to bottom and is easier to change.

Oops

Ordering range conditions from smallest to largest, like m >= 0 first.

Do this instead

The first true condition wins, so m >= 0 would catch everything positive. Put the highest threshold first.

Your mission

Band your stores

~20 min

Quick check

What does IF ( [Sales] > 100000, "Top" ) return for a store with ₹50,000 of sales?

Go deeper

  1. 1Create a measure that labels sales as Above or Below Target.
  2. 2Rewrite a nested IF with SWITCH ( TRUE (), ... ).
  3. 3Use the result for conditional formatting later.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge