cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

Performance and IF statement

Hi,

 

I have a surprising performance issue in DAX.

I have a slow measure that I use to calculate future expiry of product per month(based on current stock by expiry month and sales forecast). This measure takes about 10 seconds to be calculated for the next 24 months. For many "Product-Future Month", this measure is not required as no product are expirying on the specific month.


I therefore tried to put some IF statement in the measure to prevent this slow measure to run if no stock is expirying in the context month.

My measure is of that type:
"optimized measure" = IF("test measure"=0,0,"slow measure")


According to the performance I noticed, even if "test measure"=0, PowerBI is still calculating the slow measure! As I only need to calculate "slow measure" for 5% of my data, it's a significant loss of performance...

Would anyone be aware of this issue and have idea how to work around it?

Thanks!

Julien

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Microsoft
Microsoft

Re: Performance and IF statement

 
Because measures are calculated on the fly, the [test measure] expression is calculated twice. [test measure] is first calculated for the condition check and then for the true condition expression.
Instead of calculating the same expression multiple times, you can store the resulting measure value in a variable. You can use a variable reference wherever required.
 
optimized measure =
  var test_measure = [test measure]
  return IF(test_measure=0,0,[slow measure])
 
For more details, please refer to this document:https://www.sqlbi.com/articles/optimizing-if-conditions-using-variables/ 
 
Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Highlighted
Microsoft
Microsoft

Re: Performance and IF statement

 
Because measures are calculated on the fly, the [test measure] expression is calculated twice. [test measure] is first calculated for the condition check and then for the true condition expression.
Instead of calculating the same expression multiple times, you can store the resulting measure value in a variable. You can use a variable reference wherever required.
 
optimized measure =
  var test_measure = [test measure]
  return IF(test_measure=0,0,[slow measure])
 
For more details, please refer to this document:https://www.sqlbi.com/articles/optimizing-if-conditions-using-variables/ 
 
Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
Super Users of the Quarter - Q2 2020

Super Users of the Quarter - Q2 2020

Who are our Super User Superstars? Who made it to the top of the leaderboards? Get the answers!

June 2020 Community Highlights

June 2020 Community Highlights

Featured community members, changes to the Community, and more! Read up on recent Power BI community news.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Kudoed Authors