Forum Discussion

ZubinB's avatar
ZubinB
Frequent Visitor
1 year ago
Solved

4wk moving average per group

hello, I am looking to caluclate 4 week moving average on this data set. I want to get the moving average per platform (group)

 can someone pls help me how i can do this?

you can see the  excel table below with the 4wma calculated per platform from wk44 to wk06. how can i do this in powerbi?

 

Thanks for your help. Appreciate it...

 

YYWW*ValuePlatform4WMA
24441.587302A1.587302
24450.198413A0.892857
24461.388889A1.058201
24470.992063A1.041667
24480A0.644841
24490A0.595238
24500A0.248016
24510A0
24520A0
25010A0
25020A0
250318.60119A4.650298
25045.803571A6.10119
250513.07292A9.36942
25062.752976A10.05766
25071.264881A5.723586
24440B0
24450.297619B0.14881
24460B0.099206
24470B0.074405
24480B0.074405
24490B0
24500B0
24510B0
24520B0
25010B0
25020B0
25032.083333B0.520833
25045.555556B1.909722
25050B1.909722
25062.380952B2.50496
24440C0
24450C0
24460C0
24470.297619C0.074405
24480C0.074405
24490C0.074405
24500C0.074405
  • ZubinB 

     

    you can also try to this

     

    1. create an order column

    order = CALCULATE(COUNTROWS('Table'),FILTER('Table','Table'[Platform]=EARLIER('Table'[Platform])&&'Table'[YYWW*]<=EARLIER('Table'[YYWW*])))
     
    2.  create 4WMA column
     
    Column =
    var _start=if('Table'[order]-3<1,1,'Table'[order]-3)
    return AVERAGEX(FILTER('Table','Table'[Platform]=EARLIER('Table'[Platform])&&'Table'[order]>=_start&&'Table'[order]<=EARLIER('Table'[order])),'Table'[Value])
    pls see the attachment below
     

9 Replies

  • Hi ZubinB

    Assuming that the four weeks can cross between two years, you will need a separate weeks table that has a column of the weeks' chronological order as you can't simply subtract 3 from the current week. Assuming also that there are only 52 weeks in a year, create this calculated table and relate it to your fact.

    Weeks = 
    VAR _Weeks =
        SELECTCOLUMNS ( GENERATESERIES ( 1, 52, 1 ), "Week", [Value] )
    VAR _year =
        SELECTCOLUMNS ( { 2024, 2025 }, "Year", [Value] )
    VAR _crossjoined =
        ADDCOLUMNS (
            CROSSJOIN ( _year, _Weeks ),
            "YYWW", VALUE ( RIGHT ( [Year], 2 ) & FORMAT ( [Week], "00" ) )
        )
    RETURN
        ADDCOLUMNS (
            _crossjoined,
            "Order", RANKX ( _crossjoined, [YYWW],, asc, DENSE )
        )
    

     

    Create this measure:

    4 WMA = 
    VAR _period =
        FILTER (
            ALL ( Weeks ),
            Weeks[Order]
                >= MAX ( Weeks[Order] ) - 3
                && Weeks[Order] <= MAX ( Weeks[Order] )
        )
    VAR _AVG =
        AVERAGEX ( _period, CALCULATE ( SUM ( 'Table'[Value] ) ) )
    RETURN
        _AVG
    

     

    Please see the attached sample pbix.

     

    If this isn't what you're looking for, please provide more details as we'll just end up with assumptions.

     

    • ZubinB's avatar
      ZubinB
      Frequent Visitor

      Thanks danextian  for the solution. Let me try this over and get back to you. 

  • ZubinB 

     

    you can also try to this

     

    1. create an order column

    order = CALCULATE(COUNTROWS('Table'),FILTER('Table','Table'[Platform]=EARLIER('Table'[Platform])&&'Table'[YYWW*]<=EARLIER('Table'[YYWW*])))
     
    2.  create 4WMA column
     
    Column =
    var _start=if('Table'[order]-3<1,1,'Table'[order]-3)
    return AVERAGEX(FILTER('Table','Table'[Platform]=EARLIER('Table'[Platform])&&'Table'[order]>=_start&&'Table'[order]<=EARLIER('Table'[order])),'Table'[Value])
    pls see the attachment below
     
    • ZubinB's avatar
      ZubinB
      Frequent Visitor

      Thanks a lot ryan_mayu Ryan. I was exactly trying to do that... create an index first and create moving average .. but i was struggling. thanks for the solution.. i really appreciate it..

  • Hi ZubinB ,

    Can you check with below DAX function


    4WMA =
    VAR CurrentWeek = SELECTEDVALUE('Table'[YYWW]) -- To Get current YYWW
    VAR CurrentPlatform = SELECTEDVALUE('Table'[Platform]) -- To Get current Platform

    RETURN
    CALCULATE(
    AVERAGE('Table'[Value]),
    FILTER(
    ALL('Table'),
    'Table'[Platform] = CurrentPlatform &&
    'Table'[YYWW] <= CurrentWeek &&
    'Table'[YYWW] > CurrentWeek - 4
    )
    )

  • ZubinB's avatar
    ZubinB
    Frequent Visitor

    Hi Abhilash_P  

    Thank you for taking the time in answering the question.

    It didn't return any error but it dint give any numbers. it created 4WMA column with blanks.

    what am i doing wrong? sorry im a beginner in powerbi..

     

     

    Thanks

  • Hi ZubinB please try this

    VAR CurrentWeek = SELECTEDVALUE(Sheet1[yyww])
    VAR CurrentPlatform = SELECTEDVALUE(Sheet1[Platform])

    VAR MovingAverage =
        AVERAGEX(
            FILTER(
                ALLSELECTED(Sheet1),
                Sheet1[Platform] = CurrentPlatform &&
                Sheet1[yyww] <= CurrentWeek &&
                Sheet1[yyww] >= CurrentWeek - 3
            ),
            Sheet1[Value]
        )
    RETURN
    IF(ISBLANK(MovingAverage), 0, MovingAverage)
    • ZubinB's avatar
      ZubinB
      Frequent Visitor

      Thanks techies  for helping.. I tried your solution. it is returning me all zeroes. Dont know why. Thanks for the help. I already got the solution from ryan_mayu