Forum Discussion

alexpetersen's avatar
alexpetersen
Frequent Visitor
3 years ago
Solved

Moving Average by Ranked Index

Dear Community, 

 

I am trying to calculate the 13 Week Moving Average.

 

My data model is very simple. 

 

Please see below for my current attempt and error message. 

 

 

 

 

 

 

Greatly appreciate any and all help with this issue.

Thank you in advance!

  • BrianConnelly's avatar
    BrianConnelly
    3 years ago

    Here is your code...

     

     

    90 Day Average = 
    VAR sDate = MAX('Table2'[Date])
    VAR bDate = sDate-90
    VAR sValue = CALCULATE(AVERAGE('Table2'[Count]),FILTER(ALL('Table2'[Date]),'Table2'[Date] >= bDate && 'Table2'[Date] <= sDate))
    RETURN sValue

     

     

     

    Change the "Calculate(Average" to a Max or Min for the other two measures and get the MAX Date.

    90 Min = 
    VAR sDate = MAXX(ALL('Table2'[Date]),'Table2'[Date])
    VAR bDate = sDate-90
    VAR sValue = CALCULATE(MIN('Table2'[Count]),FILTER(ALL('Table2'[Date]),'Table2'[Date] >= bDate && 'Table2'[Date] <= sDate))
    RETURN sValue

     

     

10 Replies

  • Change MyIndex to 

    VAR MyIndex = SELECTEDVALUE('Proposals'[Index],<A Default Value>)

     

    A default value can be something like MAX()

    MAX('Proposals'[Index])

     

    Change your filter to

     

    FILTER(ALL('Proposals')....

     

    • alexpetersen's avatar
      alexpetersen
      Frequent Visitor

      Thank you so much for your quick response. 

       

      I have updated the code to the following:

       

      Moving Average = 
      VAR MyIndex = SELECTEDVALUE('Proposals'[Index],MAX('Proposals'[Index]))
      VAR myResult = 
          AVERAGEX(
              FILTER(ALL( 'Proposals'),
                  'Proposals'[Index] > MyIndex-13  && 
                  'Proposals'[Index] <= MyIndex
                 ),'Proposals'[Proposals]
                 )
      RETURN FIXED(myResult,2) 

       

      Unfortunately, the moving average column is all coming back as "0.00"

       

      Is there an easy way to share my Power BI file?

       

      • BrianConnelly's avatar
        BrianConnelly
        Icon for Resolver III rankResolver III

        You have to upload to GIT or Onedrive and share Publicly the link.  You could also past sample data and image of the dataset.

  • alexpetersen's avatar
    alexpetersen
    Frequent Visitor

    Please see below for sample data:

     

     

    Date                 Proposals

    1/3/202156
    1/10/2021101
    1/17/202197
    1/24/2021145
    1/31/2021205
    2/7/2021125
    2/14/2021123
    2/21/2021143
    2/28/2021156
    3/7/2021127
    3/14/2021141
    3/21/2021132
    3/28/2021135
    4/4/2021162
    4/11/2021175
    4/18/2021167
    4/25/2021209
    5/2/2021182
    5/9/2021205
    5/16/2021154
    5/23/2021218
    5/30/2021158
    6/6/2021248
    6/13/2021140
    6/20/2021145
    6/27/2021112
    7/4/2021107
    7/11/2021168
    7/18/2021156
    7/25/2021146
    8/1/2021163
    8/8/2021161
    8/15/2021163
    8/22/2021196
    8/29/2021193
    9/5/2021156
    9/12/2021217
    9/19/2021262
    9/26/2021243
    10/3/2021217
    10/10/2021232
    10/17/2021267
    10/24/2021246
    10/31/2021191
    11/7/2021332
    11/14/2021262
    11/21/202170
    11/28/2021197
    12/5/202187
    12/12/202173
    12/19/202121
    12/26/20217
    1/2/2022111
    1/9/2022219
    1/16/2022219
    1/23/2022210
    1/30/2022293
    2/6/2022261
    2/13/2022212
    2/20/2022148
    2/27/2022203
    3/6/2022225
    3/13/2022201
    3/20/2022184
    3/27/2022199
    4/3/2022155
    4/10/2022200
    4/17/2022235
    4/24/2022213
    5/1/2022286
    5/8/2022135
    5/15/2022198
    5/22/2022106
    5/29/2022134
    6/5/2022170
    6/12/2022221
    6/19/2022163
    6/26/2022121
    7/3/2022124
    7/10/2022149
    7/17/2022164
    7/24/2022136
    7/31/2022213
    8/7/2022209
    8/14/2022186
    8/21/2022208
    8/28/2022173
    9/4/2022141
    9/11/2022221
    9/18/2022266
    9/25/2022216
    10/2/2022279
    10/9/2022243
    10/16/2022240
    10/23/2022216
    10/30/2022230
    11/6/2022238
    11/13/2022287
    11/20/202255
    11/27/2022285
    12/4/2022290
    12/11/2022141
    12/18/202231
    12/25/202212
    1/2/202375
    1/9/2023116
    1/16/2023167
    1/23/2023196
    1/30/2023208
    2/6/2023183
    2/13/2023271
    2/20/2023196
    2/27/2023195