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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
webportal
Impactful Individual
Impactful Individual

Use DAX variable withing measure

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!

1 ACCEPTED 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

Community Support Team _ Yuliana Gu
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

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

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.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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