Forum Discussion

JonathanJohns's avatar
JonathanJohns
Helper III
9 years ago
Solved

Running Average Calculation

Good morning,

 

I have a question about the calculation of a running average. I know how to calculate a running average every three weeks with this DAX formula : 

 

Running Average = IF(NewTable[Index]=1 || NewTable[Index]=2;0;(NewTable[Average]+LOOKUPVALUE(NewTable[Average];NewTable[Index];NewTable[Index]-1)+LOOKUPVALUE(NewTable[Average];NewTable[Index];NewTable[Index]-2))/3)

 

But know I get this table :

 

RecetteWeekAverageIndex
120017-W0659,922
120017-W0760,41253
120017-W0860,244
120017-W0959,746666675
120017-W1160,637
120017-W1259,996666678
120017-W1360,1249
130017-W0559,931935481
130017-W0660,18567012
130017-W0760,03415733
130017-W0859,941530614
130017-W0960,138372095
130017-W1059,910909096
130017-W1159,9881257
130017-W1260,097850478
130017-W1359,948444449
140017-W0562,233333331
140017-W0662,160833332
140017-W0962,098888895
140017-W1361,8439

 

I would like to do the same calculation but for each Recette in my column Recette. Do you know how to do please ?

 

Thank you for your help.

 

Jonathan

  • v-qiuyu-msft's avatar
    v-qiuyu-msft
    9 years ago

    Hi JonathanJohns,

     

    In your scenario, you can create calculated columns below:

     

    WeekNum = VALUE(RIGHT('Table1'[Week],2))
    Rank = RANKX(
             FILTER(
             ALLSELECTED(Table1),Table1[Recette]=EARLIER(Table1[Recette])
                       ),
             'Table1'[WeekNum],
         ,1)
    RunningTotal = CALCULATE(SUM('Table1'[Average]),ALLEXCEPT(Table1,'Table1'[Recette]),'Table1'[Rank]<=EARLIER(Table1[Rank]))
    LastVal = var l=LOOKUPVALUE(Table1[RunningTotal],'Table1'[Index Recette],'Table1'[Index Recette],'Table1'[Rank],'Table1'[Rank]-3)
    return 
    
    IF('Table1'[Rank]>3,('Table1'[RunningTotal]-l)/3,BLANK())

     

    Measure = IF(MAX('Table1'[Rank])<3,0,IF(MAX('Table1'[Rank])=3,CALCULATE(AVERAGE(Table1[Average]),FILTER(ALL(Table1),'Table1'[Recette]<=MAX('Table1'[Recette])),'Table1'[Rank]<=3),MAX('Table1'[LastVal])))

     

     

     

    Best Regards,
    Qiuyun Yu

9 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      You are going to need to specify a bit more clearly what you need.  Most of us will be ... uncomfortable... that you want an average of averages, but regardless... do you always want the most recent *3* values averaged together?  Or all of them within 1 Recette?

       

      Best to give us some "sample results"

      • JonathanJohns's avatar
        JonathanJohns
        Helper III

        Sorry if I was not enough clear...

         

        I would like to get a table like this one :

         

        RecetteWeekAverageIndex WeekIndex RecetteRunning Average
        100017-W1560,1529464710
        100017-W1660,2357426810
        100017-W1759,95293239160,1138738
        120017-W1160,63320
        120017-W1259,9966667420
        120017-W1360,1245260,2502222
        130017-W0960,2085227130
        130017-W1059,9109091230
        130017-W1159,9881253360,0358523
        130017-W1260,09785054359,9989615
        130017-W1359,885359,9886585
        130017-W1459,68117656359,8863423

         

        I know how to calculate the two columns "Index". But I don't know how to make the DAX expression to calculate my running average to get this table.

         

        For example for the recette 10, because I want to calculate a running average from the three last weeks, I put a 0 for the two first weeks and I do this calculation for the third week : (60,1529464+60,2357426+59,9529323)=60,1138738. This value is my running average from the last three weeks.

         

        That is the same calculation for the recette 12.

         

        For the recette 13, that is the same calculation at the beginning but from the  Week 12, the calculation is : (Average W10+Average W11 + Average W12)/3 = 59,9989615

        And for the Week 13, the calculation is : (Average W11 + Average W12 + Average W13)=59,9886585

         

        I hope you see what I mean.

         

        Thank you for your help.

         

        Jonathan