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.
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.
Solved! Go to Solution.
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
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
Thanks @v-lili6-msft - that seems to do just what I wanted. Thanks for your help.
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
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.
so, the issue in the very first post is still actual?
Yes, it works when I use an actual integer, but when I use the variable it does not.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
114 | |
97 | |
85 | |
70 | |
61 |
User | Count |
---|---|
151 | |
121 | |
104 | |
87 | |
67 |