Forum Discussion

Jodallen123's avatar
Jodallen123
Icon for Helper I rankHelper I
1 year ago
Solved

Rolling average with blanks present

Hi, 

 

I am trying to create a 4 week rolling average measure. 

 

Below is what I have so far and it works on my sales data, but for some reason I can't seem to make it work on my budget data (monthly value split out over all weekdays), the budget-table is connected to the master time table on the date-column. 

 

( I made the measure a 10 day average just because it was easier to check the result)

 

4davg_ratioBU =
VAR MaxVisibleDate = MAX('Master Time Table'[Date])
VAR RollingWindow =
    DATESINPERIOD('Master Time Table'[Date], MaxVisibleDate, -10, DAY)
RETURN
    AVERAGEX(
        RollingWindow,
        COALESCE([Budget], 0)
    )
 
The data looks like this:
 
DateBudgetCurrent measureSum 10 days BUActual 10 day avgImplied denominator
2025-01-0160.61   
2025-01-0261.22   
2025-01-0361.84   
2025-01-0401.84   
2025-01-0501.84   
2025-01-0662.45   
2025-01-0763.06   
2025-01-0863.67   
2025-01-0964.29   
2025-01-1064.90484.89.80
2025-01-1104.29424.29.80
2025-01-1203.67363.69.80
2025-01-1363.67363.69.80
2025-01-1464.29424.29.80
2025-01-1564.90484.89.80
2025-01-1664.90484.89.80
2025-01-1764.90484.89.80
2025-01-1804.29424.29.80
2025-01-1903.67363.69.80
2025-01-2063.67363.69.80
2025-01-2164.29424.29.80
2025-01-2264.90484.89.80
2025-01-2364.90484.89.80
2025-01-2464.90484.89.80
2025-01-2504.29424.29.80
2025-01-2603.67363.69.80
2025-01-2763.67363.69.80
2025-01-2864.29424.29.80
2025-01-2964.90484.89.80
2025-01-3064.90484.89.80
2025-01-3164.90484.89.80
2025-02-0104.29424.29.80
2025-02-0203.67363.69.80
2025-02-03114.20414.19.77
2025-02-04115.33525.29.75
2025-02-05116.47636.39.74
2025-02-06116.99686.89.72
2025-02-07117.52737.39.71
2025-02-0806.90676.79.70
2025-02-0906.29616.19.70
2025-02-10116.81666.69.68
2025-02-11117.95777.79.68
2025-02-12119.09888.89.68
2025-02-13119.09888.89.68
2025-02-14119.09888.89.68
2025-02-1507.95777.79.68
2025-02-1606.81666.69.68
2025-02-17116.81666.69.68
2025-02-18117.95777.79.68
2025-02-19119.09888.89.68
 
Any ideas why my measure give me incorrect values? Does it have to do with the 0:es on weekends? I tried the measure with no 0es on the weekends but it made no difference. 
 
Any help is much appreciated!  
  • Update:
    I ran a test with the sample data you provided, and your 10 days rolling average measure actually provides the right values. Or do you want a different beviour, for example exclude non working days? .
    Anyway, there is no need to use AVERAGEX here, so for performance I would use code like below.

    4davg_ratioBU= 
    VAR MaxVisibleDate = MAX('Master Time Table'[Date])
    VAR RollingWindow =
        DATESINPERIOD('Master Time Table'[Date], MaxVisibleDate, -10, DAY)
    RETURN
        DIVIDE(CALCULATE([Budget], RollingWindow), COUNTROWS(RollingWindow))

     

9 Replies

  • Jodallen123 , Try using

    4davg_ratioBU =
    VAR MaxVisibleDate = MAX('Master Time Table'[Date])
    VAR RollingWindow =
    FILTER(
    'Master Time Table',
    'Master Time Table'[Date] <= MaxVisibleDate &&
    'Master Time Table'[Date] > MaxVisibleDate - 10
    )
    RETURN
    AVERAGEX(
    RollingWindow,
    COALESCE([Budget], 0)
    )

    • Jodallen123's avatar
      Jodallen123
      Icon for Helper I rankHelper I

      Thanks for the reply! Unfortunately that does not give me the correct values, I get the result (Forum) below: 

       

       

  • sjoerdvn's avatar
    sjoerdvn
    Icon for Solution Sage rankSolution Sage

    Hard to say what's going on in your Model. [Current measure Sum 10 days] is also not returning the right values for the first 10 days. I assume [Budget] is a measure. Can you provide that definition?

  • sjoerdvn's avatar
    sjoerdvn
    Icon for Solution Sage rankSolution Sage

    Update:
    I ran a test with the sample data you provided, and your 10 days rolling average measure actually provides the right values. Or do you want a different beviour, for example exclude non working days? .
    Anyway, there is no need to use AVERAGEX here, so for performance I would use code like below.

    4davg_ratioBU= 
    VAR MaxVisibleDate = MAX('Master Time Table'[Date])
    VAR RollingWindow =
        DATESINPERIOD('Master Time Table'[Date], MaxVisibleDate, -10, DAY)
    RETURN
        DIVIDE(CALCULATE([Budget], RollingWindow), COUNTROWS(RollingWindow))

     

    • Jodallen123's avatar
      Jodallen123
      Icon for Helper I rankHelper I

      Hi, that gives me the same value as my current measure, but maybe your version is more efficient. However I don't think it's right? In my dataset above if you look at 2025-01-10, should the value not be 4.8? Not 4.9?

       

       

       

      I don't think I need to exclude non-working days. 

      • sjoerdvn's avatar
        sjoerdvn
        Icon for Solution Sage rankSolution Sage

        Yes, that value should be 4.80. In my model it is, using your measure and your dates and budget values; so clearly something else is happening.
        So I would check the definition of that [Budget] measure, and the relationship between date table and budget data table.