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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply

Problem with variables

Does anybody know why these measure are showing different data? I think it is because the variable doesn't recalculate in the second step.

 

Bedrag PFY =
Var Calculateddmeasure =
    [Bedrag (c)]

Return

IF (
HASONEVALUE ( _Boekperiod[Bedrijf] );
//For Single Company
 
CALCULATE (
Calculateddmeasure;
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[tdboekper_boekper]
= SELECTEDVALUE ( _Boekperiod[tdboekper_boekper] )
&& _Boekperiod[Bedrijf]
=SELECTEDVALUE(_Boekperiod[Bedrijf]))
 
);
//For All Companies (total)

CALCULATE (
Calculateddmeasure;
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[tdboekper_boekper]
= SELECTEDVALUE ( _Boekperiod[tdboekper_boekper] )
)
)
)
 
Bedrag PFY2 =
Var Calculateddmeasure =
    [Bedrag (c)]

Return

IF (
HASONEVALUE ( _Boekperiod[Bedrijf] );
//For Single Company
 
CALCULATE (
[Bedrag (c)];
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[tdboekper_boekper]
= SELECTEDVALUE ( _Boekperiod[tdboekper_boekper] )
&& _Boekperiod[Bedrijf]
=SELECTEDVALUE(_Boekperiod[Bedrijf]))
 
);
//For All Companies (total)

CALCULATE (
[Bedrag (c)];
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[tdboekper_boekper]
= SELECTEDVALUE ( _Boekperiod[tdboekper_boekper] )
)
)
)
5 REPLIES 5
Greg_Deckler
Super User
Super User

I would say that is likely, you generally do not re-calculate a VAR. I'm actually sort of surprised it lets you do it and you don't get a syntax error. Tough to say really without sample data. Is the 2nd one giving you the correct result that you are looking for?


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Yes the second one is doing the job. But there i do not use the variable.

Is the second one using a measure? Just curious. 

 

But, yes, once you set a VAR it doesn't change in value, unless you get into nesting like in my Runge-Kutta Quick Measure but that's a very specialized case. 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

The second one also uses measures. See next

This calculates the amount to pay to to the workforces for the previous Fiscal year. Because these Fiscal years are not always from jan untill dec and change per yaer. Also it is not the same for every company in my dataset. So actualy it is the same as calculate([_personeel];previousyear(_Boekperiod[Date])) but because i need to look at so many different factors i need to calculate it like this one underneath. This measure does work.

 

Personeel PFY =

IF (
HASONEVALUE ( _Boekperiod[tdboekper_boekper] );
IF (
HASONEVALUE ( _Boekperiod[Bedrijf] );
//For Single Company
CALCULATE (
[_Personeel];
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[tdboekper_boekper]
= SELECTEDVALUE ( _Boekperiod[tdboekper_boekper] )
&& _Boekperiod[Bedrijf] = SELECTEDVALUE ( _Boekperiod[Bedrijf] )
)
);
//For All Companies (total)
CALCULATE (
[_Personeel];
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[tdboekper_boekper]
= SELECTEDVALUE ( _Boekperiod[tdboekper_boekper] )
)
)
);
IF (
HASONEVALUE ( _Boekperiod[Bedrijf] );
//For Single Company
CALCULATE (
[_Personeel];
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
&& _Boekperiod[Bedrijf] = SELECTEDVALUE ( _Boekperiod[Bedrijf] )
)
);
//For All Companies (total)
CALCULATE (
[_Personeel];
FILTER (
ALL ( _Boekperiod );
_Boekperiod[Boekjaar]
= SELECTEDVALUE ( _Boekperiod[Boekjaar] ) - 1
)
)
)
)

 

 

 

Hi @jvangrunderbeec

 

Yes, once you declare a variable its contents are immutable, a constant actually.

In [Bedrag PFY] you're using the variable Calculateddmeasure  in the first argument of the CALCULATE. Its contents will not change, it is not affected by the filter arguments. It's like if you did  CALCULATE( 3, FILTER( .....)), you'd always get a 3

 

In [Bedrag PFY2], on the contrary, you are using the measure [Bedrag (c)] directly instead of the variable and that gets recalculated within the filter context created by CALCULATE, as usual.

  

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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