Forum Discussion

dphillips's avatar
dphillips
Icon for Helper IV rankHelper IV
6 years ago
Solved

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 R...
  • v-lili6-msft's avatar
    6 years ago

    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