Ravindra BagaleCourses & study guides मराठी Track your progress

13. DAX: Data Analysis Expressions

13.7 IF, SWITCH and DIVIDE in Power BI DAX with Example

IF and SWITCH in Power BI DAX with example — in 30 seconds

What: IF picks one of two answers. SWITCH(TRUE(), …) checks rules from top to bottom and stops at the first TRUE.

Why: Managers want words like "Within 10 min", not raw minutes.

Tiny example: 8 min, then Within 10 min. 14, then 11-20 min. 25, then Over 20 min. Blank, then Not delivered.

First look at the data (before, then after):

IF and SWITCH in Power BI DAX with example: before and after tables, fictional Blinkit Maharashtra data

Same before / after tables as text

Before — Delivery Time Mins

Order ID Delivery Time Mins
BLK-1 8
BLK-2 14
BLK-3 25
BLK-4 (blank)

About this data: This table uses columns: Order ID, Delivery Time Mins. This table uses columns: Order ID, Mins, Delivery Band. Values shown are the lesson examples already on this page (not a new dataset).

Formula / click path

Delivery Band = SWITCH(TRUE(),
  ISBLANK(Orders[Delivery Time Mins]), "Not delivered",
  Orders[Delivery Time Mins] <= 10, "Within 10 min",
  Orders[Delivery Time Mins] <= 20, "11-20 min",
  "Over 20 min")

After — New calculated column

Order ID Mins Delivery Band
BLK-1 8 Within 10 min
BLK-2 14 11-20 min
BLK-3 25 Over 20 min
BLK-4 (blank) Not delivered

Do it step by step:

  1. Select the Orders table. Click Table tools › New column.
  2. Type the SWITCH formula. Put the ISBLANK line first.
  3. Press Enter. Open Table view and check the four rows.
  4. Use Delivery Band in a slicer or a bar chart.

IF

Delivery Target Status =
IF([% Orders Delivered Under 10 Mins] >= 0.8, "On Target", "Below Target")

-- Calculated column in Orders
Has Discount = IF(Orders[Discount] > 0, "Yes", "No")

If the third argument is omitted, IF returns BLANK when the condition is false. (The 80% target above is only an example value.)

AND, OR, NOT

AND() and OR() accept exactly two arguments; for more conditions use the operators && and ||.

-- Calculated columns in Orders
Late Cash Order =
IF(AND(Orders[Delivery Time Mins] > 20, Orders[Payment Mode] = "Cash on Delivery"), "Yes", "No")

Big Basket Flag = IF(Orders[Quantity] >= 5 || Orders[Amount] >= 500, "Big", "Normal")

SWITCH

SWITCH replaces long nested IFs.

-- Simple form: compare one expression to values
Payment Group = SWITCH(Orders[Payment Mode],
    "UPI", "Digital",
    "Card", "Digital",
    "Wallet", "Digital",
    "Cash on Delivery", "Cash",
    "Other")

-- SWITCH(TRUE()) pattern: first condition that is TRUE wins
Delivery Band = SWITCH(TRUE(),
    ISBLANK(Orders[Delivery Time Mins]), "Not delivered",
    Orders[Delivery Time Mins] <= 10, "Within 10 min",
    Orders[Delivery Time Mins] <= 20, "11-20 min",
    "Over 20 min")

-- Time-of-day slot from the Order Hour column
Day Part = SWITCH(TRUE(),
    Orders[Order Hour] < 6, "Late Night",
    Orders[Order Hour] < 12, "Morning",
    Orders[Order Hour] < 17, "Afternoon",
    Orders[Order Hour] < 21, "Evening",
    "Night")

DIVIDE

Lakshat theva, always use DIVIDE for division in measures. It handles division by zero safely and returns BLANK (or an alternate result) instead of an error.

AOV (Average Order Value) = DIVIDE([Total Sales], [Delivered Orders])

Discount % = DIVIDE([Total Discount], [Total Sales] + [Total Discount], 0)

Ravindra Bagale's Tip

Friends, many students write [Total Sales] / [Total Orders] and get errors or infinity when a city has no orders. Use DIVIDE(numerator, denominator), which returns blank (or an alternate value) when the denominator is zero. Prefer SWITCH(TRUE(), …) over deeply nested IFs for readability. Never forget this.

Loading DAX practice tasks...

  1. Big deal flag (Easy): Calculated column on Subscriptions: Big Deal = IF Seats is 25 or more then "Yes" else "No".
  2. Is it churned? (Easy): Column on Subscriptions: Lost = IF Status = "Churned" then 1 else 0.
  3. MRR status (Easy): Measure MRR Status = "On track" if [MRR] is above 5000, else "Below".
  4. Plan level number (Medium): Column: Plan Level = 1 for "Basic", 2 for "Pro", 3 for "Enterprise".
  5. Seat band with SWITCH(TRUE()) (Medium): Column Seat Band: "Large" if Seats >= 50, "Mid" if Seats >= 20, else "Small".
  6. Active Pro MRR (AND) (Medium): Measure Active Pro MRR = [MRR] for rows where Status = "Active" AND Plan = "Pro". Use FILTER with &&.
  7. Needs attention (OR) (Medium): Column Attention = TRUE if Tickets >= 3 OR Status = "Churned".
  8. Not churned seats (NOT) (Hard): Measure Live Seats = SUM of Seats for rows where NOT (Status = "Churned").
  9. Health label by country (Hard): Row per Accounts Country. Measure Health = "Good" if [MRR] >= 1000, "Low" if it is less, and BLANK() when there is no MRR.
  10. Count of risky subscriptions (Hard): Measure Risky = number of Subscriptions rows with Tickets >= 3 and Plan is not "Basic".

Ravi Sir's Cards for this lesson

मराठी cards