Forum Discussion

Kleine285's avatar
Kleine285
New Member
3 years ago
Solved

Moving average

Hello everyone, 

 

I've already looked at and tried some of your examples here, but they don't really help me.

I need to calculate an average that keeps changing/shifting.

 

Example: Average of January '22 = (December 2021 + January 2021)/2)
average February '22 = (December 2021 + February 2021)/2)
March '22 average = (December 2021 + March 2021)/2).

 

So I need to have the average of each month with December of the previous year.

 

 

I hope the attached table gives an idea of my data. 

  • Hi , Kleine285 

    I download this .pbix file , the filed in the visual is wrong, youo need to use the [Maand] filed in the 'Measures Mndst' table and [14.0] measure you created before not in the 'Table2'.

    For your need , you should update the measure to this :

    Moving average = var _date =SELECTEDVALUE('Mndst'[Maand])
    var _this_year_month =MAXX( FILTER( 'Table 2' , [Maand] = DATE( YEAR( _date)  ,MONTH(_date),1)) , [14.0])
    var _last_year_dec = MAXX( FILTER( 'Table 2' , [Maand] = DATE( YEAR( _date) -1 ,12,1)) , [14.0])
     return
    IF(_last_year_dec=BLANK(),BLANK(),DIVIDE(_last_year_dec+_this_year_month,2))

    The result is follows , i think that is your need :

     

    Best Regards,

    Aniya Zhang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

9 Replies

  • tackytechtom's avatar
    tackytechtom
    Most Valuable Professional

    Hi Kleine285 ,

     

    How about this:

     

    And here the code:

    RollingMeasure = 
    VAR _prevYear = YEAR ( SELECTEDVALUE ( 'Table'[Date] ) ) - 1
    RETURN
    ( 
        CALCULATE ( 
            SELECTEDVALUE ( 'Table'[Value] ), 
            ALL   ( 'Table' ),
            YEAR  ( 'Table'[Date] ) = _prevYear, 
            MONTH ( 'Table'[Date] ) = 12
        ) + 
        SELECTEDVALUE ( 'Table'[Value] ) 
    ) / 2

     

    Let me know if this solves your issue 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/ 

    • Kleine285's avatar
      Kleine285
      New Member

      I think it isn't working form me because the value for me is already a measure. In my example 14.0 = SUMX( Table., table col1 + table col 2 + ....).

       How can the solution be amended?

    • Kleine285's avatar
      Kleine285
      New Member

      Hi Tom, 

       

      Is there any possibility for me to send the files for you? Maybe by email.

       

       

        

      Please find attacted examples of my data and measures. 

      • v-yueyunzh-msft's avatar
        v-yueyunzh-msft
        Community Support

        Hi , Kleine285 

        Here are the steps you can refer to :

        (1)This is my test data :

        (2)We need to click "New Table" to create a table because you have a measure [14.0]:

        Table 2 = ADDCOLUMNS( SUMMARIZE( ALLSELECTED('Table') , 'Table'[Mannd]) , "14.0" , [14.0])

        (3)Then we need to create a measure :

        Moving average = var _date =SELECTEDVALUE('Table'[Mannd])
        var _last_year_month =MAXX( FILTER( 'Table 2' , [Mannd] = DATE( YEAR( _date) -1 ,MONTH(_date),1)) , [14.0])
        var _last_year_dec = MAXX( FILTER( 'Table 2' , [Mannd] = DATE( YEAR( _date) -1 ,12,1)) , [14.0])
         return
        DIVIDE( _last_year_month + _last_year_dec , 2)

        (4)We can put the filed we need and the measure on the visual and we will meet your need :

         

        If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.(You can upload your file to OneDrive , then you can share the OneDrive link to share your file)

         

        Best Regards,

        Aniya Zhang

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly