cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Referencing to Measure or rather working with variables?

Hello Community,

Usually I create a lot of measures and even use nested measures as well. But now I am wondering if thats a best practice approach. Should I use variables rather than nesting or referencing to other measures?

An example for calculating a simple ratio.

Approach 1 with nestes measures:

• Measure 1 = SUM( value 1 )
• Measure 2 = SUM( value 2 )
• Measure 3 = DIVIDE( Measure 2, Measure 1 )

Approach 2 with variables:

• Measure 1:
var 1 = SUM( value 1 )
var 2 = SUM( value 2 )
RETURN
DIVIDE( 2, 1 )

What do you think?

2 REPLIES 2
Anonymous
Not applicable

As usual, IT DEPENDS. If you have defined meaningful business measures, then you should reuse them as much as possible because code duplication is the first step to hell. You should catch measure values into variables most of the time and give them good names. Remembering at that that a measure's value stored in a variable will never change. This is helpful when used correctly. Also, using variables is very beneficial when debugging code. The rule I go by is: If your expression is short, you don't need to catch everyching into variables because the short code (with well named measures) speaks for itself. If your expression is complex, use variables with good names.

Super User

For me Approach 2 works better. There are few reasons for using variables rather than multiple measures:

• Using variables make my DAX calculation simpler and easy to understand.
• If I have everything in a single DAX expression, it is easy to maintain, specially in the cases where the DAX is not working as expected then debugging is easier. You need to check one expression for debugging rather than checking multiple measures used to create this calculation.
• Based on my previous experience with writing DAX, I have seen that use of variables improves performance as well. You can check this for example on the two visuals displaying calculation output using Approach 1 and Approach 2.

This is my view, but I am sure others will have more to add to this.

Thanks,

Pragati

Best Regards,

Pragati Jain

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Proud to be a Super User!!