Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hello,
Is it possible to use a variable within a DAX measure expression?
For example, the following measure isn't working (it always returns 0):
Measure = VAR ThisMonth = CALCULATE ( ABS ( SUM ( 'Table'[Saldo] ) ); FILTER ( Table; Table[Conta] = 71 ) ) VAR PreviouzMonth = CALCULATE ( ThisMonth; PREVIOUSMONTH ( 'Calendário'[Date] ); FILTER ( ALL ( 'Calendário'[Mês] ); MAX ( 'Calendário'[Mês] ) > 1 ) ) RETURN ThisMonth-PreviouzMonth
But if the two variables above are calculated separetely - ie as two different measures - the calculation works fine.
Thanks for supporting!
Solved! Go to Solution.
Hi @webportal,
No. We cannot use nested variables in such a scenario. When the second variable referrs to the first variable, the first one always pass the static value to it. In your scenario, if variable ThisMonth returns 100, then, second variable will receive 100 as its result, regardless of its own calculation. That is why the measure always return 0.
To resolve this problem, rather than using the first variable inside second one, please input the actual calculation.
Measure = VAR ThisMonth = CALCULATE ( ABS ( SUM ( 'Table'[Saldo] ) ); FILTER ( Table; Table[Conta] = 71 ) ) VAR PreviouzMonth = CALCULATE ( CALCULATE ( ABS ( SUM ( 'Table'[Saldo] ) ); FILTER ( Table; Table[Conta] = 71 ) ); PREVIOUSMONTH ( 'Calendário'[Date] ); FILTER ( ALL ( 'Calendário'[Mês] ); MAX ( 'Calendário'[Mês] ) > 1 ) ) RETURN ThisMonth-PreviouzMonth
Best regards,
Yuliana Gu
Yes, that should be possible. However, it is likely taking into account the context of the measure and so something is going wonky versus with separate measures it sounds like they are getting calculated in a different context.
Yes, I guess it has to do with the context of the calculation.
Notice however that the second variable is using the first variable in its expression, is that ok?
Hi @webportal,
No. We cannot use nested variables in such a scenario. When the second variable referrs to the first variable, the first one always pass the static value to it. In your scenario, if variable ThisMonth returns 100, then, second variable will receive 100 as its result, regardless of its own calculation. That is why the measure always return 0.
To resolve this problem, rather than using the first variable inside second one, please input the actual calculation.
Measure = VAR ThisMonth = CALCULATE ( ABS ( SUM ( 'Table'[Saldo] ) ); FILTER ( Table; Table[Conta] = 71 ) ) VAR PreviouzMonth = CALCULATE ( CALCULATE ( ABS ( SUM ( 'Table'[Saldo] ) ); FILTER ( Table; Table[Conta] = 71 ) ); PREVIOUSMONTH ( 'Calendário'[Date] ); FILTER ( ALL ( 'Calendário'[Mês] ); MAX ( 'Calendário'[Mês] ) > 1 ) ) RETURN ThisMonth-PreviouzMonth
Best regards,
Yuliana Gu
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
71 | |
70 | |
43 | |
31 | |
26 |
User | Count |
---|---|
89 | |
49 | |
44 | |
38 | |
37 |