Forum Discussion

arutsjak90's avatar
arutsjak90
Helper I
5 years ago
Solved

Rolling average with filter

Hi, I am trying to create 3months back rolling average with filter, becaue it needs to be rolling average per Plant. What I have created (or edited)  below.

*The table I'm working on was created with the Summarize function

COGSTotal = SUMMARIZE(COGS,COGS[TotalByPlantKey],COGS[Plant],COGS[FixedDate],"Total COGS",SUM(COGS[ CostOfSale]))

 

Total COGSFixedDatePlant3monthsRollingAverage
201/01/20201 
302/01/20201 
203/01/202012.33333
504/01/202013.33333
401/01/20202 
202/01/20202 
303/01/202023
304/01/202024
*A circular error appears
3MonthsRollingAverage =
VAR LastDate_ =
LASTDATE ( COGSTotal[FixedDate])
RETURN
CALCULATE (
AVERAGEX ( VALUES (COGSTotal[FixedDate]), CALCULATE ( SUM ( COGSTotal[Total COGS] ) ) ),
FILTER (
COGSTotal,
COGSTotal[Plant]=EARLIER(COGSTotal[Plant]) &
[FixedDate] <= LastDate_ && [FixedDate] > DATEADD ( LastDate_, -3, MONTH ) ) )
  • Hi arutsjak90 -

     

    Make sure you have a calendar table, linked to the "Fixed Date" column in your table, and then try this code

     

    Rolling 3 Mo Avg =
    VAR __ThisMonth =
        MAX ( COGS[FixedDate] )
    VAR __3MoBack =
        EDATE ( __ThisMonth, -3 ) + 1
    RETURN
        CALCULATE (
            SUM ( COGS[Total COGS] ),
            ALLEXCEPT ( COGS, COGS[Plant] ),
            DATESBETWEEN ( DateTab[Date], __3MoBack, __ThisMonth )
        ) / 3
    

     

     

    Hope this helps

    David

     

     

9 Replies

  • dedelman_clng's avatar
    dedelman_clng
    Community Champion

    Hi arutsjak90 -

     

    Make sure you have a calendar table, linked to the "Fixed Date" column in your table, and then try this code

     

    Rolling 3 Mo Avg =
    VAR __ThisMonth =
        MAX ( COGS[FixedDate] )
    VAR __3MoBack =
        EDATE ( __ThisMonth, -3 ) + 1
    RETURN
        CALCULATE (
            SUM ( COGS[Total COGS] ),
            ALLEXCEPT ( COGS, COGS[Plant] ),
            DATESBETWEEN ( DateTab[Date], __3MoBack, __ThisMonth )
        ) / 3
    

     

     

    Hope this helps

    David

     

     

    • arutsjak90's avatar
      arutsjak90
      Helper I

      Thank you for your time, but it is not working.. 
      after entering this code, I get the sum of the whole Total COGS column divided by 3, not splited into plants and not rolling, very strange