Forum Discussion
SWITCH vs IF (increase performance?)
- 1 year ago
Hi fjjpeeters1976 ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.
Thank you.
- 1 year ago
hi, thanks very much for your help. It made it indeed faster. Still it is rather slow but I think that is more due to the size of the report and the complicated formulars behind it. I will focus more on those now.
Hi fjjpeeters1976 ,
Thanks for reaching out to the Microsoft fabric community forum.
I used a regular SWITCH statement in my DAX measure to handle values from 1 to 27. For values 1 to 20, I called some complex measures (using SUMX, FILTER, etc.), and for 21 to 27, I returned simple constants like 0.25, 0.5, and 1.
However, the report visuals became very slow, especially when filters were applied or pages were changed. Switching to nested IF statements made the code harder to read and didn’t help with performance.
I then tried using SWITCH(TRUE(), ...) instead of SWITCH(column, ...), updating my logic so that each condition checked if alloc equaled a certain value. For example, alloc = 1 would return [Heavy Measure 1], and alloc = 22 would return 0.25, and so on.
This change significantly improved performance, with visuals loading much faster. The reason seems to be that the regular SWITCH might evaluate all branches, especially when returning measures, while SWITCH(TRUE()) stops at the first match, reducing unnecessary calculations.
After testing both approaches, the results were the same, but SWITCH(TRUE()) was clearly faster.
Please find the attached PBIX and Screenshort file for your reference.
Best Regards,
Tejaswi.
Community Support
Hi fjjpeeters1976 ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.
Thank you.
- fjjpeeters19761 year agoHelper III
hi, thanks very much for your help. It made it indeed faster. Still it is rather slow but I think that is more due to the size of the report and the complicated formulars behind it. I will focus more on those now.