Forum Discussion

JonathanJohns's avatar
JonathanJohns
Helper III
9 years ago

Running Average for three weeks

Good afternoon,

 

I have a problem to do a formula with Power BI Desktop. I had a look on the forum to find my answer but I was not able to do the operation.

 

I have a table like this one with data from 01/02/2017 to 31/03/2017. I would like to calculate  a running average for 3 weeks.

 

DATEValueDayWeekMonth
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/201760,10017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/201760,310017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/201760,320017-03-010017-W0917-mars
01/03/201760,890017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/2017 NA0017-03-010017-W0917-mars
01/03/201759,890017-03-010017-W0917-mars

 

I know how to calculate an average for one week but I don't know how to do a running average. For an example I would like a table like that :

 

WeekAverageRunning Average
0017-W0960NA
0017-W1061NA
0017-W116261
0017-W126061
0017-W136161
0017-W146160,666666
0017-W156261,333333

 

With for the week 11 the calculation : (60+61+62)/3=61

For the week 12 : (61+62+60)=61

For the week 14 : (60+61+61)=60,66

...

 

I know it won't be possible to calculate for the 2 first weeks but do you know how to do for the others please ?

 

Thanks for your help.

10 Replies

    • JonathanJohns's avatar
      JonathanJohns
      Helper III

      I read your article but I didn't understand how did it in my case... Sorry

       

      How is it possible to calculate a running average for three weeks please ?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi JonathanJohns,

    In your scenario, you can firstly create new table using the DAX below.

    NewTable = SUMMARIZE('Datatable','Datatable'[Week],"Average",AVERAGE('Datatable'[Value]))

    Then create a index column in the new table using the following formula. 

    Index = CALCULATE(COUNT(NewTable[Week]),ALL(NewTable),FILTER(NewTable,NewTable[Week]<=EARLIER(NewTable[Week])))


    And create Running Average using DAX below.

    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)


    Thanks,
    Lydia Zhang

    • JonathanJohns's avatar
      JonathanJohns
      Helper III

      Hi Anonymous

       

      Thanks for your answer. I have done the script you did and I got this table :

       

       

      Concerning the column "Running Average", I wanted to know how it's possible to get a column like that please :

       

      WeekAverageIndexRunning Average
      0017-W0960,3741711NA
      0017-W1060,3221052NA
      0017-W1160,224279360,30685167
      0017-W1260,26567460,27068467
      0017-W1360,153548560,214499

       

      The value 60,306851167 is the average of the 3 values from the weeks W09,W10,W11.

      The value 60,27068467 is the average of the 3 values from the weeks W10,W11, W12.

      The value 60,214499 is the average of the 3 values from the weeks W11,W12,W13.

       

       

      I hope you know what I mean, I tried all my laste friday but it looks a little bit confused for me.

       

      Thank you for your help.

       

      Jonathan

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi JonathanJohns,

        Do you create newtable using the first formula? And do you right click the newTable and select "New Column" to apply the third formula?

        Thanks,
        Lydia Zhang