Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Why using the VARiable changes the result

Can someone please explain to me the expected difference in these two DAX measures.  One contains a variable (VAR) "SpendSum" and the other does not.  Needless to say I was getting different results with my associated visuals.  They are both one of twelve Monthly values to be displayed for the selected year, but the one using the variable unexpectedly resulted in a table with all monthly results being the same annual total (for each respective row).  The one without the variable displayed each respective individual monthly value as desired.  I thought the VAR just took the place of the function for ease of coding, but there was a different result.   Why it worked that way?  Just trying to understand how VARs work and their limitations.  Thank you in advance for your explanations.

MEASURE 1 =

 

VAR SelectYear = SELECTEDVALUE('Calendar Table 2'[Year])

RETURN

 

Calculate(SUMX('Spend',' Spend'[Amount USD]), (DATESBETWEEN('Calendar Table 2'[DATE].[Date], DATE(SelectYear,1,1), DATE(SelectYear,1,31))), ('Spend'[CATEGORY]<>"Spend Delta"))

 

------------------------------------------------------------------

 

MEASURE 2 =

 

VAR SelectYear = SELECTEDVALUE('Calendar Table 2'[Year])

VAR SpendSum = SUMX('Spend','Spend'[Amount USD])

RETURN

 

Calculate(SpendSum,(DATESBETWEEN('Calendar Table 2'[DATE].[Date], DATE(SelectYear,1,1), DATE(SelectYear,1,31))), ('Spend'[CATEGORY]<>"Spend Delta"))

 

2 REPLIES 2
Corey_M
Resolver II
Resolver II

  • In Measure 1, SUMX is inside CALCULATE, so it's evaluated under the filter context created by CALCULATE. The context is the specific month set by DATESBETWEEN.

  • In Measure 2, SUMX is calculated outside of CALCULATE (in the variable SpendSum), so it sums over the entire table without the month-specific filter context. Then, CALCULATE doesn't affect the value of SpendSum since it's already computed. It's like having a constant value.

Anonymous
Not applicable

Thanks, for the explanation.  In this case, the formula was small enough that the VAR wasn't necessary.  So, if I have a filter to apply, I may need to include that in the VAR instead of in the main body of the measure.  I cannot just simply consider my VAR to be an exact substitute for all contexts.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors