Forum Discussion
calculate "around" a variable
- 7 years ago
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.
Hi AlB - so what you are saying is that once __TodayVal is evaluated it is the value itself, not the code that populated it. And applying a different filter context to a constant does nothing.
Thanks!
David
- AlB7 years ago
Community Champion
dedelman_clng wrote:Hi AlB - so what you are saying is that once __TodayVal is evaluated it is the value itself, not the code that populated it. And applying a different filter context to a constant does nothing.
Thanks!
David
Yep. Exactly
You would need a measure instead for what you were trying to do.
- AlB7 years ago
Community Champion
MFelix
Good point. I guess it comes down to nomenclature, to what exactly your definition of a constant entails.
What I said, or meant to say, is that once the variable is assigned a value (at creation) its contents are immutable. I am referring here to each execution instance of a variable. An instance of that variable exists between the point where it is created/assigned a value (which happens simultaneously) and the point where it is "used up" through RETURN.When in a calculated column we have something like:
VAR CurrentID = 'Table'[ID] RETURN CALCULATE ( SUM ( 'Table'[Sales] ); FILTER ( 'Table'; 'Table'[ID] < CurrentID ) )
we'll have, for each row, a different instance of the variable CurrentID. I am limiting my definition of constant to what happens within a row, which is the effective existence of each instance of CurrentID. And I emphasized that aspect because it was the culprit
in the issue presented by dedelman_clng
You are extending the definition to encompass all execution instances. If you consider it so, it is true that each instance will (possibly) be created with a different value. But that value will be immutable during a particular instance's life.- MFelix7 years ago
Super User
Hi AlB,
I agree with you and what you say, but one of the good things in DAX is that we can make changes of context within the measures themselves and not only considering the row context but also the table context, or any other, and you can create your own context that is different from the model or the visual you are using.
This make it's very dynamic and more flexible. But good remarks.
Regards,
MFelix