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:
- One clear test →
IF ( condition, then, else ). - Map a few literal values →
SWITCH ( column, val1, res1, … ). - Many ordered conditions →
SWITCH ( TRUE(), cond1, res1, … ). - First TRUE wins in SWITCH(TRUE()).
- Nested IF past three levels → rewrite.
- 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 |
- Why IF: two-way status. “Is this city High or Normal?”
IF ( [Total Sales] >= 100, "High", "Normal" ).- Mumbai 140 → High. Nashik 80 → Normal.
- Why SWITCH(TRUE()): more than two bands without nested IF hell.
- Put Elite / High / Mid / Starter in order. First true wins.
- 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 picks a then/else result from a logical test — great for simple measure branches and thresholds.
मित्रांनो — IF picks a then/else result from a logical test — great for simple measure branches and thresholds.
मित्रों — IF picks a then/else result from a logical test — great for simple measure branches and thresholds.
What do I need before this guide?
- A working
[Total Sales]measure. - Course: Logical functions.
Before and after (look at the tables first)
Before
आधी (Before)
पहले (Before)
After
नंतर (After)
बाद में (After)
IF for simple branches
Sales Band Simple =
IF (
[Total Sales] >= 100,
"High",
"Normal"
)
- Great for two-way status.
- Else can be blank if you want quiet visuals.
- 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(), … ) evaluates conditions in order and returns the first match — cleaner than deep nested IFs.
मित्रांनो — SWITCH ( TRUE(), … ) evaluates conditions in order and returns the first match — cleaner than deep nested IFs.
मित्रों — 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"
)
- Conditions are tested top to bottom.
- Put the strictest band first.
- Far clearer than IF inside IF inside IF.
- On the sample, Mumbai 140 → High; Nashik 80 → Mid.
Interview map: one or two branches → IF; many bands or labels → SWITCH / SWITCH(TRUE()).
मित्रांनो — Interview map: one or two branches → IF; many bands or labels → SWITCH / SWITCH(TRUE()).
मित्रों — 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!
Ravindra Bagale's Tip – मराठी
बरेच students पाच IFs nest करतात आणि प्रार्थना करतात. Interviewers ला आवडते: “मी दोन branches साठी IF वापरतो; bands साठी SWITCH(TRUE()).” Short, mature, hireable. लक्ष ठेवा!
Ravindra Bagale's Tip – हिंदी
बहुत students पांच IFs nest करते हैं और प्रार्थना करते हैं. Interviewers को ये पसंद है: “मैं दो branches के लिए IF इस्तेमाल करता हूँ; bands के लिए SWITCH(TRUE()).” Short, mature, hireable. ध्यान रखो!
Practice task
- Write Sales Band Simple with IF (Mumbai should say High on the sample).
- Rewrite as Sales Band with SWITCH(TRUE()).
- Put both on a City table.
- Change thresholds; confirm order still makes sense.
- Delete the nested-IF version from your model notes.
Got it? IF for simple forks; SWITCH / SWITCH(TRUE()) for maps and bands. Next: DIVIDE for safe percentages. Let's continue.
समजलं का? IF for simple forks; SWITCH / SWITCH(TRUE()) for maps and bands. Next: DIVIDE for safe percentages. Aata pudhe jaauya.
समझ में आया? IF for simple forks; SWITCH / SWITCH(TRUE()) for maps and bands. Next: DIVIDE for safe percentages. आगे बढ़ते हैं.
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.