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
StuartSmith
Power Participant
Power Participant

Discussion: To Variable or not Variable in IF statements, that is the question.

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.

2 ACCEPTED SOLUTIONS
Alex87
Resolver III
Resolver III

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.

View solution in original post

v-zhengdxu-msft
Community Support
Community Support

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.

 

 

View solution in original post

3 REPLIES 3
StuartSmith
Power Participant
Power Participant

Thanks for the input and sort of confirmed the approach I am taking.

v-zhengdxu-msft
Community Support
Community Support

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.

 

 

Alex87
Resolver III
Resolver III

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.

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.