Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Variables not working with Calculate

Hi Guys!

 I created the following Variable:

 

EUR =

var summa = SUM(Sheet1[amount])

 

return

 

CALCULATE(summa;Sheet1[currency]="EUR")

 

I want to return the sum of EUR from my list.

The Calculate function does not want to work, it does not want to filter for EUR at all. The result is the same every time (the "summa" what I defined as a variable), no matter what I am filtering for in Calculate. 

 

If I am Calculating the sum of EUR separately with 2 measures, than it works.

 

No idea what is going wrong.

Do you have any advice?

THank you

S.

 

 

 

 

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Sturla!

    Thank you for your answer.

    I have 3 other measure combinations for such calculation. I just wanted to try another way + getting familiar with the concept of variables in dax. That is why I was surprised when I did not get the expected result in the return section of the code.

     

    I still not get it why it is so. I believed that the return section was for combining, altering all the variables defined above. 

     

    Thank you

    S

     

  • sturlaws's avatar
    sturlaws
    6 years ago

    Yes, the variables are a bit confusing, they are kind of static once they are calculated, so the name variables is a bit counterintuitive

3 Replies

  • sturlaws's avatar
    sturlaws
    Icon for Resident Rockstar rankResident Rockstar

    Hi, Anonymous,

     

    what you experience is by design. The variable is calculated before the context of the measure, Currency="Eur", is applied.

     

    For such a simple measure, there is nothing to gain by using a variable, your measure should be:

     

    EUR =
    CALCULATE (
        SUM ( Sheet1[amount] );
        FILTER ( SHeet1; Sheet1[currency] = "EUR" )
    )

     

     

    Cheers,
    Sturla

    If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Sturla!

      Thank you for your answer.

      I have 3 other measure combinations for such calculation. I just wanted to try another way + getting familiar with the concept of variables in dax. That is why I was surprised when I did not get the expected result in the return section of the code.

       

      I still not get it why it is so. I believed that the return section was for combining, altering all the variables defined above. 

       

      Thank you

      S

       

      • sturlaws's avatar
        sturlaws
        Icon for Resident Rockstar rankResident Rockstar

        Yes, the variables are a bit confusing, they are kind of static once they are calculated, so the name variables is a bit counterintuitive