Forum Discussion

dbadmin's avatar
dbadmin
Icon for Advocate V rankAdvocate V
10 years ago
Solved

How to calculate Moving Average based on a Rolling 10 hours

 I have a table that lists the Date, Time and SPMs. I need to calculate the MOVING Average for the SPMs column. I have tried all the different sites - but can't seem to find one that will work for wh...
  • Greg_Deckler's avatar
    10 years ago

    Just in case you missed this one:

     

    http://www.daxpatterns.com/time-patterns/

     

    Look at the Aggregation Pattern information as this should be basically what you need. Probably a little more complicated since you are working with time instead of date though. Probably will have to use EARLIER and the example from the EARLIER page might get you what you want if you modify the formula's FILTER to give you everything from the last 10 hours and use AVERAGE or AVERAGEX instead of COUNTROWS.

     

    https://support.office.com/en-US/article/EARLIER-Function-DAX-90e5c2b0-50e2-417d-b4c7-7528febcef97

     

    Scroll all the way to the end and read the explanation very carefully. Essentially, if your EARLIER grabbed the 3rd column value, you modified the filter to give you everything within the past 10 hours. Here is what I came up with in trying this:

     

    MovingAverage = CALCULATE(AVERAGE([Value]),FILTER(MovingAverage, EARLIER([Time])>=[Time]))

     

    Obviously, I haven't tweaked this to account for within 10 hours, on the same date, etc. but the results I came up with were:

     

    MovingAverage

    181.75722
    162.2699
    218.727775
    223.8
    276.6
    252.9037

     

    Same order as your rows. So, the earliest time value (6:05) gets 276.6, the average of its single row. The 6:16 value gets 223.8, the average of 276.6 and 171 and so on.

     

    Is this what you are looking for basically?

     

    One note, I imported your values into Desktop and I got rid of the single quotes and such so that the dates came in as dates, the time values came in as time values and the "value" column came in as a decimal number.

  • Greg_Deckler's avatar
    Greg_Deckler
    10 years ago

    Happy to help, and for the record, EARLIER is a dumb name for that function IMHO considering how it is most often used. I would have gone with something like CURRENT.