Forum Discussion
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
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.
How can I get the slicer to determine the variable rather than hard coding a number in?
Any help would be appreciated.
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/63376or
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907In 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
6 Replies
- v-lili6-msft
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/63376or
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907In 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
- dphillips
Helper IV
Thanks v-lili6-msft - that seems to do just what I wanted. Thanks for your help.
- az38
Community Champion
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 ResultGroupNumberThe issue is not in variable usage but in its formula
- dphillips
Helper IV
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.