Forum Discussion

aslee's avatar
aslee
Helper I
9 years ago
Solved

Moving average over non-date values

Hi All,

 

I need to calculate a moving average over time, however the time is set as numerical iterations, not dates. 

 

The average needs to be over the 2 iterations - for example, the current iteration being 5, the calculation needs to take the average cycle time of this iteration and previous iteration 4. Then I also need the average of iteration 4 and previous iteration 3, and so on and so forth moving back in time. I eventually want a line chart tracking this moving average as we move forward in iterations. 

 

Some sample data:

 

 

IterationCycle Timeid
120cc-1
117cc-2
122cc-3
223cc-4
25cc-5
215cc-6
210cc-7
211cc-8
218cc-9
321cc-10
420cc-11
414cc-12
425cc-13
59cc-14
511cc-15
512cc-16
527cc-17
520cc-18

 

 

The resulting chart data should be similar to the below:

 

IterationAvg
517.3
420.0
314.7
2

15.7

 

Can anyone assist? I have tried adapting a previous question of mine HERE but this code won't adapt to when I change sum to average. 

  • Hi aslee

     

    Please try the following calculated measure but change Table4 to the name of your table.

     

    Avg = FORMAT(
    		CALCULATE(
    			AVERAGE('Table4'[Cycle Time]),
    			FILTER(ALL('Table4'),
    					'Table4'[Iteration]>=MAX(Table4[Iteration])-1
    					&& 'Table4'[Iteration]<=MAX('Table4'[Iteration])
    			)
    			),
    			"##.0")

3 Replies

  • Phil_Seamark's avatar
    Phil_Seamark
    Microsoft Employee

    Hi aslee

     

    Please try the following calculated measure but change Table4 to the name of your table.

     

    Avg = FORMAT(
    		CALCULATE(
    			AVERAGE('Table4'[Cycle Time]),
    			FILTER(ALL('Table4'),
    					'Table4'[Iteration]>=MAX(Table4[Iteration])-1
    					&& 'Table4'[Iteration]<=MAX('Table4'[Iteration])
    			)
    			),
    			"##.0")
    • aslee's avatar
      aslee
      Helper I

      Thank you Phil_Seamark! This has worked and looks great in a table - but for some reason I get this error when I go to put it in a line chart

       

      My x-axis is Iteration, which is set to numerical integer format.

       

      Anyone have any ideas what this means? 

      • aslee's avatar
        aslee
        Helper I

        Phil_Seamark

         

        Nevermind - worked it out! I took out the formatting section and it fixed it.

         

        Thanks again!