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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
domtrump
Helper I
Helper I

CASE Statement replacement for DAX

I don't know when/if the CASE syntax will ever be a part of DAX but I was just wondering if there is a better approach than nested, upon nested, upon nested inline IF syntax? Sure, for very simple things like replacing numeric codes with lables, SWITCH works nicely, but when there are calculations involved, the IF's get very hard to read. Is there some other syntax I'm not aware of? I've seen some examples of making a new column in PQ using an If Then Else construct. Is that the better way to go? If so, what would be the PQ syntax to replace SQL Server's 

CASE WHEN x THEN y ELSE z END ? 

1 ACCEPTED SOLUTION
djurecicK2
Super User
Super User

Hi @domtrump ,

 I think Switch is what a lot of people use. If the calculations are cluttering up the formula, you might consider using variables to separate those from the switch statement- something like below:

 

Measure =
VAR ComplexCondition1 = longcomplexformula1
VAR ComplexResult2 = longcomplexformula2
RETURN
    SWITCH ( TRUE (), [ComplexCondition1], [ComplexResult1] )

etc...

View solution in original post

2 REPLIES 2
djurecicK2
Super User
Super User

Hi @domtrump ,

 I think Switch is what a lot of people use. If the calculations are cluttering up the formula, you might consider using variables to separate those from the switch statement- something like below:

 

Measure =
VAR ComplexCondition1 = longcomplexformula1
VAR ComplexResult2 = longcomplexformula2
RETURN
    SWITCH ( TRUE (), [ComplexCondition1], [ComplexResult1] )

etc...

I did NOT know you could use FORMULAS in SWITCH! Awesome. Thanks so much. -Dom

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors