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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dedelman_clng
Community Champion
Community Champion

calculate "around" a variable

I am attempting to use variables within a measure, but not getting the desired results.  I've looked around at all kinds of DAX reference material and can't figure out why this doesn't work.  I know there are other ways to accomplish why I'm trying to do, but I'd like to know specifically why this doesn't work.

 

Basically, I'm setting a variable and then trying to recalculate that variable within the same measure.  The recalculation doesn't work.

 

Data (table name is PD):

 

DtValue
1/1/2018100
1/2/2018200
1/3/2018250
1/4/2018300
1/5/2018

400

 

Dt has a relationship to a Date table (DateTab)

 

Creating a measure that finds the difference in Value from the previous day:

 

Delta = 
  var __TodayVal = SUM(PD[Value])
  var __PrevVal = CALCULATE(__TodayVal, PREVIOUSDAY(DateTab[Date]))
  var __Delta = __TodayVal -__PrevVal

return __Delta

Here is what I would expect

 

DtValueDelta
1/1/2018100 
1/2/2018200100
1/3/201825050
1/4/201830050
1/5/2018400100

 

But this is what I get

 

2018-12-19 11_44_01-new scratchpad - Power BI Desktop.png

I'm sure it has something to do with evaluation context inside the measure, but I'm wracking my brain to be able to explain it. I know I can work around it by creating multiple measures, but I'd like to know a concrete reason for why this can't be done in a single measure.

 

Thanks,

David

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @dedelman_clng

A variable in DAX is actually a constant. Unlike variables in other programming languages, once you declare a variable in DAX by assigning it a value, that value is immutable. Your variable __TodayVal in the CALCULATE for __PrevVal will not be affected at all by the filter argument.

   

View solution in original post

11 REPLIES 11

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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