Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and a 50 percent discount on exams.
Get startedEarn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
It seems to be the common practice to declare all variables first and then use them in the measure, but too optimise measures should you declare variables even if you might not need them in an IF statement or avoid Variables and simply put the code from the variables in the IF statement?
So as an example,
Variable_Measure =
VAR Variable1 = Blah1, Blah, Blah, Blah
VAR Variable2 = Blah2, Blah, Blah, Blah
VAR Variable3 = Blah3, Blah, Blah, Blah
Return
IF(Variable1 = True, Variable2, Variable3)
The above Variables could be complex calculations, using lots of system resources even before the IF statement and then depending on the IF statement result, the variable might not be used, so unnecassary declaring a variable and useing system resources.
NonVariable_Measure =
IF(Blah1, Blah, Blah, Blah = True, Blah2, Blah, Blah, Blah, Blah3, Blah, Blah, Blah)
In the above, no variables are pre-loaded/calculated and calculated only when needed depending on the IF statement result. Surely this is a more efficient way than using variables?
I hope that makes sense.
Solved! Go to Solution.
It is important to know why using variables in DAX is so common. You shouldn't just create variables for the sake of it. Instead, use them to break down complex problems into simpler chunks, make your formulas easier to manage, and make sure each part is only calculated once. But, keep an eye on performance too. It’s a good idea to play around with different approaches and test their performance in DAX Studio to see what works best.
Hi @StuartSmith
This is a dialectical question.
Just as @Alex87 said, the using of variables is important.
Variables can enhance performance by avoiding redundant calculations. When you declare a variable, its value is computed once and reused throughout the measure. This can significantly reduce query time, especially for complex expressions.
For example, if the outcome is
IF(Variable1 = Variable2, Variable2, Variable3)
The Variable1,2,3 only calculate once.
But this formula will calculate Blah2, Blah, Blah twice.
IF(Blah1, Blah, Blah, Blah = Blah2, Blah, Blah, Blah, Blah2, Blah, Blah, Blah, Blah3, Blah, Blah, Blah)
If the expression is more complex, there will be more redundant calculations。
Well-named variables make your DAX formulas more readable. They act as placeholders for intermediate results, making the measure easier to understand.
Variables allow you to isolate specific parts of your formula for testing. You can temporarily modify the RETURN expression to output the variable value during debugging.
While variables offer benefits, there are scenarios where avoiding them might be more efficient, for straightforward calculations, directly using expressions within the measure can be more concise.
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thanks for the input and sort of confirmed the approach I am taking.
Hi @StuartSmith
This is a dialectical question.
Just as @Alex87 said, the using of variables is important.
Variables can enhance performance by avoiding redundant calculations. When you declare a variable, its value is computed once and reused throughout the measure. This can significantly reduce query time, especially for complex expressions.
For example, if the outcome is
IF(Variable1 = Variable2, Variable2, Variable3)
The Variable1,2,3 only calculate once.
But this formula will calculate Blah2, Blah, Blah twice.
IF(Blah1, Blah, Blah, Blah = Blah2, Blah, Blah, Blah, Blah2, Blah, Blah, Blah, Blah3, Blah, Blah, Blah)
If the expression is more complex, there will be more redundant calculations。
Well-named variables make your DAX formulas more readable. They act as placeholders for intermediate results, making the measure easier to understand.
Variables allow you to isolate specific parts of your formula for testing. You can temporarily modify the RETURN expression to output the variable value during debugging.
While variables offer benefits, there are scenarios where avoiding them might be more efficient, for straightforward calculations, directly using expressions within the measure can be more concise.
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
It is important to know why using variables in DAX is so common. You shouldn't just create variables for the sake of it. Instead, use them to break down complex problems into simpler chunks, make your formulas easier to manage, and make sure each part is only calculated once. But, keep an eye on performance too. It’s a good idea to play around with different approaches and test their performance in DAX Studio to see what works best.
User | Count |
---|---|
92 | |
87 | |
77 | |
73 | |
68 |
User | Count |
---|---|
116 | |
107 | |
88 | |
65 | |
63 |