Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ArchStanton
Impactful Individual
Impactful Individual

IF Statement or Switch

I've read that Switch statements are more efficient and less costly than IF statements so I'm trying to convert this code to Switch

but I'm not sure how I can avoid the IF at the beginning?


Days to Validation Bins = 
IF(AND('Cases'[Created On] < DATEVALUE("01/04/2020"), 'Cases'[statecode] = "Resolved" || "2"),BLANK(),
    IF(ISBLANK('Cases'[Days to Validation]),BLANK(),
        IF( 'Cases'[Days to Validation] <= 30 , "0-1 Mth",
            IF('Cases'[Days to Validation] <= 60, "1-2 Mths",
                IF('Cases'[Days to Validation] <= 90, "2-3 Mths", 
                    "Older")
))))

 

1 ACCEPTED SOLUTION
mh2587
Super User
Super User

Days to Validation Bins = //Try this
VAR _Created = 'Cases'[Created On]
VAR _State   = 'Cases'[statecode]
VAR _Days    = 'Cases'[Days to Validation]
RETURN
SWITCH(
    TRUE(),
    AND(_Created < DATE(2020,4,1), OR(_State = "Resolved", _State = "2")), BLANK(),
    ISBLANK(_Days), BLANK(),
    _Days <= 30, "0-1 Mth",
    _Days <= 60, "1-2 Mths",
    _Days <= 90, "2-3 Mths",
    "Older"
)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @ArchStanton 

One thing's for sure: SWITCH is less verbose and easier to read

Days to Validation Bins =
SWITCH(
    TRUE(),
    ISBLANK('Cases'[Days to Validation]) ||
    (
        'Cases'[Created On] < DATE(2020, 4, 1) &&
        'Cases'[statecode] IN {"Resolved", "2"}
    ), BLANK(),

    'Cases'[Days to Validation] <= 30, "0-1 Mth",
    'Cases'[Days to Validation] <= 60, "1-2 Mths",
    'Cases'[Days to Validation] <= 90, "2-3 Mths",
    "Older"
)




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Thanks for this!

mh2587
Super User
Super User

Days to Validation Bins = //Try this
VAR _Created = 'Cases'[Created On]
VAR _State   = 'Cases'[statecode]
VAR _Days    = 'Cases'[Days to Validation]
RETURN
SWITCH(
    TRUE(),
    AND(_Created < DATE(2020,4,1), OR(_State = "Resolved", _State = "2")), BLANK(),
    ISBLANK(_Days), BLANK(),
    _Days <= 30, "0-1 Mth",
    _Days <= 60, "1-2 Mths",
    _Days <= 90, "2-3 Mths",
    "Older"
)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.