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
dphillips
Helper IV
Helper IV

Variable not working correctly in measure

I am using a variable in a measure to find which item is selected in a slicer. The variable is called ResultGroupNumber. I want this to be calculated based on the value in the slicer. If I test the ResultGroupNumber and calculate it and then add it to a card I get the number 9 which is correct for the current value in the slicer. Here is my formula I am using below. Basically, I am trying to find the average of a students results for all resultgroups which are less than the current one selected

 

LPAveLongTerm = 
    var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),allexcept('Result Group Table','Result Group Table'[Result Group]))
RETURN
calculate(AVERAGE(uncRedshift_Studentresults[LPResult]),filter(all('Result Group Table'),'Result Group Table'[Sort Order] < ResultGroupNumber),allexcept(uncRedshift_Studentresults,uncRedshift_Studentresults[NameNum],uncRedshift_Studentresults[ResultType]))

 

 When I use the variable I get 

ResultAve1.png

 

 

 

 

 

 

 

However, if I remove the variable and use the constant 9 like this

 

LPAveLongTermV2 = 
    var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),allexcept('Result Group Table','Result Group Table'[Result Group]))
RETURN
calculate(AVERAGE(uncRedshift_Studentresults[LPResult]),filter(all('Result Group Table'),'Result Group Table'[Sort Order] < 9),allexcept(uncRedshift_Studentresults,uncRedshift_Studentresults[NameNum]))

 

I get the second column which is what I am after.

ResultAve2.pngHow can I get the slicer to determine the variable rather than hard coding a number in?

 

Any help would be appreciated.

1 ACCEPTED SOLUTION
v-lili6-msft
Community Support
Community Support

HI  @dphillips 

For your case, it likes measure totals problem. Very common. See this post about it
https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

or
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

 

In your case, you want to use the total of ResultGroupNumber as a variable in the measure,

but for different rowcontext in the visual, it won't use the total of ResultGroupNumber,

so for your case, you could adjust the formula as below:

LPAveLongTerm =
var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),all('Result Group Table'))
RETURN
calculate(AVERAGE(uncRedshift_Studentresults[LPResult]),filter(all('Result Group Table'),'Result Group Table'[Sort Order] < ResultGroupNumber),allexcept(uncRedshift_Studentresults,uncRedshift_Studentresults[NameNum],uncRedshift_Studentresults[ResultType]))

 

or

LPAveLongTerm =
var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),allselected('Result Group Table'))
RETURN
calculate(AVERAGE(uncRedshift_Studentresults[LPResult]),filter(all('Result Group Table'),'Result Group Table'[Sort Order] < ResultGroupNumber),allexcept(uncRedshift_Studentresults,uncRedshift_Studentresults[NameNum],uncRedshift_Studentresults[ResultType]))

 

 

Regards,

Lin

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

6 REPLIES 6
v-lili6-msft
Community Support
Community Support

HI  @dphillips 

For your case, it likes measure totals problem. Very common. See this post about it
https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

or
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

 

In your case, you want to use the total of ResultGroupNumber as a variable in the measure,

but for different rowcontext in the visual, it won't use the total of ResultGroupNumber,

so for your case, you could adjust the formula as below:

LPAveLongTerm =
var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),all('Result Group Table'))
RETURN
calculate(AVERAGE(uncRedshift_Studentresults[LPResult]),filter(all('Result Group Table'),'Result Group Table'[Sort Order] < ResultGroupNumber),allexcept(uncRedshift_Studentresults,uncRedshift_Studentresults[NameNum],uncRedshift_Studentresults[ResultType]))

 

or

LPAveLongTerm =
var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),allselected('Result Group Table'))
RETURN
calculate(AVERAGE(uncRedshift_Studentresults[LPResult]),filter(all('Result Group Table'),'Result Group Table'[Sort Order] < ResultGroupNumber),allexcept(uncRedshift_Studentresults,uncRedshift_Studentresults[NameNum],uncRedshift_Studentresults[ResultType]))

 

 

Regards,

Lin

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

Thanks @v-lili6-msft  - that seems to do just what I wanted. Thanks for your help.

az38
Community Champion
Community Champion

@dphillips 

First, try to debug your variable like

LPAveLongTermV2 = 
    var ResultGroupNumber = calculate(max('Result Group Table'[Sort Order]),allexcept('Result Group Table','Result Group Table'[Result Group]))
RETURN
ResultGroupNumber 

The issue is not in variable usage but in its formula 


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Yep - I did test that the variable was correct by using the formula for the variable on its own like you have done. I put this measure in a card and it gives me a whole number which represents the result group I am looking for. When I change the slicer, the variable also changes so that seems to be all correct. 

az38
Community Champion
Community Champion

@dphillips 

so, the issue in the very first post is still actual?


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Yes, it works when I use an actual integer, but when I use the variable it does not.

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.