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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
hylosko
Helper III
Helper III

how to optimize my dax

hello i have 3 measure

1. it is max month = MAX('Calendar'[Date].[Month number])

this measure give me the max number of month in year

 

2. Last value = calculate([max month],filter( 'Table1',Table1[Value] <> blank()))

this measure give me the number of last month with value ( for example if last value was for may it show me 5, if last value was for march it show me 3 etc)

 

3. Measure = if([Last value] = 7, (calculate(Table1[Value], filter('Calendar','Calendar'[Date].[Month number] = 6))), if([Last value] = 6, (calculate(Table1[Value], filter('Calendar','Calendar'[Date].[Month number] = 5))), if([Last value] = 5, (calculate(Table1[Value], filter('Calendar','Calendar'[Date].[Month number] = 4))), if([Last value] = 4, (calculate(Table1[Value], filter('Calendar','Calendar'[Date].[Month number] = 3))), if([Last value] = 3, (calculate(Table1[Value], filter('Calendar','Calendar'[Date].[Month number] = 2))),"xd")))))

that measure give us value for month before last month with value (for example if in may we have value 50 and in april 30, it will show 30 etc)


it any possibilty to optimize this dax ?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @hylosko ,

 

Here is an example.

 

-------- IF form ------------
SizeDesc =
IF ( Product[Size] = "S", "Small",
IF ( Product[Size] = "M", "Medium",
IF ( Product[Size] = "L", "Large",
IF ( Product[Size] = "XL", "Extra Large", "Other" ) ) ) )

-------- SWITCH equivalent ------------
SizeDesc =
SWITCH (
Product[Size],
"S", "Small",
"M", "Medium",
"L", "Large",
"XL", "Extra Large",
"Other"
)

 

 

Please try this measure.

 

Measure = 
SWITCH(
    [Last value],
    7,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 6),
    6,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 5),
    5,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 4),
    4,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date])= 3),
    3,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 2),
    "xd"
)

 

If this doesn't work for you, consider providing a sample file with no private data.

How to provide sample data in the Power BI Forum

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly -- How to provide sample data

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @hylosko ,

 

Here is an example.

 

-------- IF form ------------
SizeDesc =
IF ( Product[Size] = "S", "Small",
IF ( Product[Size] = "M", "Medium",
IF ( Product[Size] = "L", "Large",
IF ( Product[Size] = "XL", "Extra Large", "Other" ) ) ) )

-------- SWITCH equivalent ------------
SizeDesc =
SWITCH (
Product[Size],
"S", "Small",
"M", "Medium",
"L", "Large",
"XL", "Extra Large",
"Other"
)

 

 

Please try this measure.

 

Measure = 
SWITCH(
    [Last value],
    7,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 6),
    6,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 5),
    5,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 4),
    4,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date])= 3),
    3,CALCULATE(MAX('Table1'[Value]),MONTH('Calendar'[Date]) = 2),
    "xd"
)

 

If this doesn't work for you, consider providing a sample file with no private data.

How to provide sample data in the Power BI Forum

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly -- How to provide sample data

NickolajJessen
Solution Sage
Solution Sage

First of, instead of using multiple IF-statements, the better opstion is to use SWITCH.

Secondly, for a query like this, you should probably be looking towards some of power bi's built in time intelligence functions

Edit:

NickolajJessen_0-1655982632387.png

 

Can you tell me more about that switch functions ? 

NickolajJessen_0-1655989210986.png

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Solution Authors
Top Kudoed Authors