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 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/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

6 Replies

  • v-lili6-msft's avatar
    v-lili6-msft
    Icon for Community Support rankCommunity 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

  • az38's avatar
    az38
    Icon for Community Champion rankCommunity 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 

    • dphillips's avatar
      dphillips
      Icon for Helper IV rankHelper 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. 

      • az38's avatar
        az38
        Icon for Community Champion rankCommunity Champion

        dphillips 

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