Reply
raymondpocher
Advocate III
Advocate III

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
avatar user
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.

Pragati11
Super User
Super User

Hi @raymondpocher ,

 

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


MVP logo


LinkedIn | Twitter | Blog YouTube 

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

Appreciate your Kudos!!

Proud to be a Super User!!

avatar user

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors (Last Month)
Top Kudoed Authors (Last Month)