cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

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
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!!

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors