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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Replace repeated SUM calculation with VAR

Hello,
 
I have the following measure to workout the difference between a value in month and the previous month:

status_change=
VAR Prior_month =
    CALCULATE ( SUM('Table'[Value]), PARALLELPERIOD ( 'Dates'[Date], -1, MONTH ) )
VAR difference = ( Prior_month - SUM ( 'Table'[Value] ) )
Return
difference
 
I have tryed to replace the 'SUM('Table'[Value])' with a Variable at the start (as below) but it does not return the expected results. Would anyone be able to tell me why this is the case? Also is there a better method for working out the difference?
 
status_change=
VAR this_month = SUM('Table'[Value])
VAR Prior_month =
    CALCULATE ( this_month, PARALLELPERIOD ( 'Dates'[Date], -1, MONTH ) )
VAR difference = this_month - Prior_month
Return
difference
1 ACCEPTED SOLUTION
Bubble4502
Resolver III
Resolver III

Hi @Anonymous ,

 

Approve with @amitchandak , the variables are evaluated once in the code or calculation for each context. This means that the result of the measure won't change if we manipulate the filter context in the expression later in the code. Due to this reason, the result of 

CALCULATE ( SUM('Table'[Value]), PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) ) is different from 
CALCULATE ( this_month, PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) )
For example:
I have a table like this
Bubble4502_0-1660034925894.png

Here are the two measures:

 

Prior Month without var = 
VAR Prior_month =
    CALCULATE ( SUM('Table'[Value]), PARALLELPERIOD ('Date'[Date], -1, MONTH ) )
Return
Prior_month

Prior Month with var = 
VAR this_month = SUM('Table'[Value])
VAR Prior_month =
    CALCULATE ( this_month, PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) )
Return Prior_month

 

 

Output:

Bubble4502_1-1660035041663.png

Kind Regards,

Bubble

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

2 REPLIES 2
Bubble4502
Resolver III
Resolver III

Hi @Anonymous ,

 

Approve with @amitchandak , the variables are evaluated once in the code or calculation for each context. This means that the result of the measure won't change if we manipulate the filter context in the expression later in the code. Due to this reason, the result of 

CALCULATE ( SUM('Table'[Value]), PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) ) is different from 
CALCULATE ( this_month, PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) )
For example:
I have a table like this
Bubble4502_0-1660034925894.png

Here are the two measures:

 

Prior Month without var = 
VAR Prior_month =
    CALCULATE ( SUM('Table'[Value]), PARALLELPERIOD ('Date'[Date], -1, MONTH ) )
Return
Prior_month

Prior Month with var = 
VAR this_month = SUM('Table'[Value])
VAR Prior_month =
    CALCULATE ( this_month, PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) )
Return Prior_month

 

 

Output:

Bubble4502_1-1660035041663.png

Kind Regards,

Bubble

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@Anonymous ,

https://community.powerbi.com/t5/Community-Blog/DAX-Why-and-how-to-use-variables/ba-p/2351817

https://towardsdatascience.com/three-things-you-need-to-know-when-using-variables-in-dax-c67724862b57

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors