Forum Discussion

StephaneMA's avatar
StephaneMA
Frequent Visitor
6 years ago
Solved

Moving average based on prior N rows

I am trying to calculate a moving average based on the prior N rows of a table. Something that would match the table below.

 
 

 

I have tried the following but it does not provide the desired result:

 

MOVING Average submissions by N week =
 
AVERAGEX(
                FILTER(
                            TOPN(
                                        6,'Normalized Calendar', [Normalized Week],DESC), ('Normalized Calendar'[Normalized Week]
                                      )
                           ),
                  'Opportunity'[Opportunity Count])
 
Any help pointing me in the right direction would be greatly appreciated.
 
Thank you!
  • mahoneypat's avatar
    mahoneypat
    6 years ago

    The comma goes before the All().  Try doing just All('Normalized Calendar') instead (remove the column reference).

    Regards,

    Pat

     

  • StephaneMA colum type is whole number/decimal. Can you copy the table which i shared above and create the same measure (not calculated column) without any  edits. Also share the snapshot.?

12 Replies

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    Prior 5 Weeks =
    VAR currentweeknumber =
    SELECTEDVALUE ( Table[Normalized Week Number] )
    RETURN
    CALCULATE (
    AVERAGE ( Table[Subs])
    ALL ( Table[Normalized Week Number] ),
    Table[Normalized Week Number] <= currentweeknumber,
    Table[Normalized Week Number] >= currentweeknumber - 5
    )

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

    • StephaneMA's avatar
      StephaneMA
      Frequent Visitor

      Thank you Pat,

       

      I appreciate the quick reply.

       

      When I enter the code as sent, I get the following:

       

      If I add a coma before the ALL function, the error disappears but the result is not what I expect (see below)

       

       

      Can you think of what is creating the issue?

       

      Again, I really appreciate your assistance.

       

      SL

      • mahoneypat's avatar
        mahoneypat
        Icon for Microsoft Employee rankMicrosoft Employee

        The comma goes before the All().  Try doing just All('Normalized Calendar') instead (remove the column reference).

        Regards,

        Pat

         

  • nandukrishnavs's avatar
    nandukrishnavs
    Icon for Community Champion rankCommunity Champion

    StephaneMA 

     

    Try this DAX measure.

    Moving Avg =
    VAR n = 5
    VAR current =
        SELECTEDVALUE ( Table[weeknumber] )
    VAR avg =
        CALCULATE (
            AVERAGE ( Table[Subs] ),
            FILTER (
                ALL ( Table[weeknumber] ),
                Table[weeknumber] <= current
                    && Table[weeknumber] >= current - n
            )
        )
    RETURN
        avg

     



    Did I answer your question? Mark my post as a solution!
    Appreciate with a kudos
    🙂

    • StephaneMA's avatar
      StephaneMA
      Frequent Visitor

      Thank you nandukrishnavs,

       

      I appreciate the suggestion. I get exactly the same result than I did with Pat's suggestion above.

       

      • nandukrishnavs's avatar
        nandukrishnavs
        Icon for Community Champion rankCommunity Champion

        StephaneMA 

         

        WeeknumberSubs
        1282
        2508
        3540
        4518
        5717
        6599
        7622
        8416
        9603
        10622
        11730
        12617
        13618

         

         

        Moving Avg = 
        VAR n = 5
        VAR _selectedweekno =
            SELECTEDVALUE ( 'Table'[Weeknumber] )
        VAR result =
            CALCULATE (
                AVERAGE ( 'Table'[Subs] ),
                FILTER (
                    ALL ( 'Table'[Weeknumber] ),
                    'Table'[Weeknumber] <= _selectedweekno
                        && 'Table'[Weeknumber] >= _selectedweekno - n
                )
            )
        RETURN
           IF(_selectedweekno>=n,result,BLANK())

         



        Did I answer your question? Mark my post as a solution!
        Appreciate with a kudos
        🙂