Ravindra BagaleCourses & study guides Track your progress

Guides

IF and SWITCH in Power BI DAX Measures

IF returns one of two results from a logical test; SWITCH (and SWITCH ( TRUE() )) keeps multi-branch labels and bands readable — prefer them over deep nested IFs in Power BI measures.

Chala mitrano! Logic in DAX is where reports start talking. Why? Thresholds, bands and payment labels show up in every FreshBasket (fictional) dashboard. How? We write a simple IF, then graduate to SWITCH and SWITCH(TRUE()). Stay with me — vertical examples, no spaghetti.

Quick answer

Vertical chooser:

  1. One clear test → IF ( condition, then, else ).
  2. Map a few literal values → SWITCH ( column, val1, res1, … ).
  3. Many ordered conditions → SWITCH ( TRUE(), cond1, res1, … ).
  4. First TRUE wins in SWITCH(TRUE()).
  5. Nested IF past three levels → rewrite.
  6. Test on a table visual beside the inputs.
IF ( test, then, else )
SWITCH ( TRUE(), cond1, result1, cond2, result2, else )

Why IF / SWITCH, what comes out

City totals from our tiny shop:

City Total Sales
Mumbai 140
Nashik 80
  1. Why IF: two-way status. “Is this city High or Normal?”
  2. IF ( [Total Sales] >= 100, "High", "Normal" ).
  3. Mumbai 140 → High. Nashik 80 → Normal.
  4. Why SWITCH(TRUE()): more than two bands without nested IF hell.
  5. Put Elite / High / Mid / Starter in order. First true wins.
  6. Mumbai 140 might land in High; Nashik 80 in Mid — depending on your thresholds.

The function does not invent sales. It reads the measure number and returns a label.

IF in a measure IF returns one branch when the test is true, else the other. Test IF Then Else branch

IF picks a then/else result from a logical test — great for simple measure branches and thresholds.

What do I need before this guide?

Before and after (look at the tables first)

Before IF SWITCH City totals without band labels.

Before

After IF bands Mumbai High, Nashik Normal.

After

IF for simple branches

Sales Band Simple =
IF (
    [Total Sales] >= 100,
    "High",
    "Normal"
)
  1. Great for two-way status.
  2. Else can be blank if you want quiet visuals.
  3. Threshold belongs in a comment — keep it obvious while learning.

Phone-bill analogy: IF is “if bill ≥ 1000 then Over budget else OK”.

SWITCH for value maps

-- column example (adjust names)
Payment Group =
SWITCH (
    Sales[Payment Mode],
    "UPI", "Digital",
    "Card", "Digital",
    "Cash", "Cash",
    "Other"
)

Use when the input is a known set of labels.

SWITCH ( TRUE() ) for bands

SWITCH TRUE pattern SWITCH(TRUE(), …) picks the first true condition. SWITCH ( TRUE() )first TRUE wins Cond 1 → A Cond 2 → B Else → C Result switch

SWITCH ( TRUE(), … ) evaluates conditions in order and returns the first match — cleaner than deep nested IFs.

Sales Band =
SWITCH (
    TRUE (),
    [Total Sales] >= 200, "Elite",
    [Total Sales] >= 100, "High",
    [Total Sales] >= 50, "Mid",
    "Starter"
)
  1. Conditions are tested top to bottom.
  2. Put the strictest band first.
  3. Far clearer than IF inside IF inside IF.
  4. On the sample, Mumbai 140 → High; Nashik 80 → Mid.
IF vs SWITCH Nested IF gets messy; SWITCH (or SWITCH TRUE) stays readable. Nested IF / IF / IFhard to read SWITCH or SWITCH(TRUE())clearer bands choose

Interview map: one or two branches → IF; many bands or labels → SWITCH / SWITCH(TRUE()).

Mistakes and calm fixes

Symptom Likely cause Fix
Always first band Order wrong / always true Reorder; check thresholds
Blank everywhere Test never true / blank inputs Add else; inspect base measure
Unreadable measure Nested IF tree SWITCH(TRUE())
Works in column, weird in measure Context mix-up Remember measures use filter context

Ghabru naka 😅 — readable logic is a skill, not decoration.

Ravindra Bagale's Tip

Many students nest five IFs and pray. Interviewers prefer: “I use IF for two branches; SWITCH(TRUE()) for bands.” Short, mature, hireable. Pay attention!

Practice task

  1. Write Sales Band Simple with IF (Mumbai should say High on the sample).
  2. Rewrite as Sales Band with SWITCH(TRUE()).
  3. Put both on a City table.
  4. Change thresholds; confirm order still makes sense.
  5. Delete the nested-IF version from your model notes.

Learn it properly

Course lessons:

Related guides: DIVIDE safe percent · CALCULATE

Got it? IF for simple forks; SWITCH / SWITCH(TRUE()) for maps and bands. Next: DIVIDE for safe percentages. Let's continue.

Frequently asked questions

What does IF return?

The then-value when the test is true; otherwise the else-value (which can be blank or another expression).

What is SWITCH?

A function that returns a result matching an expression’s value — or, with SWITCH(TRUE()), the first true condition.

IF vs SWITCH?

IF is fine for one or two branches. SWITCH (and SWITCH TRUE) stays readable when you have many cases.

Can IF work in calculated columns?

Yes — columns and measures both use IF/SWITCH; measures recompute under filter context.

Blank vs "Normal"?

Decide whether else should be blank (hides on some visuals) or an explicit label.

Course lesson?

Logical functions in the DAX chapter.