Variable and Calculate Context Question

Hi all,

This is just a quick question on how variables work in calculate and why you get different results. I know that Sam covered part of it in his Master Dax course, which I have finished.

This is a test I did just so I can learn.

Inside calculate, I don’t use the variable in the expression and I get 39
image

If I use a variable in the expression, I then get 77

Just wondering on how variables work in the expression in calculate and I did want to use it, how would I go about doing it?

Thanks for your help.

@ysherriff Variables declared inside CALCULATE can be modified by the filter applied in CALCULATE, but anything declared outside CALCULATE can’t be changed by CALCULATE.

So unlike other programming languages in DAX you can’t do x = x + 1

In the first code your COUNTA is evaluated in the filter context created by the visual plus the variable FilterCostLessThan20Dollars.

In the second code your CountPrice variable is evaluated in a context where there is no filtering from the DAX Code so it is evaluated in the context of the visual only.

2 Likes

Thank you sir.