Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

12 Months rolling average

Hello Community,

 

I hav a table (Case) with :

1. Case ID

2. Casedatevalue which contains date and is of date type

 

I want to have count of case id by month and also rolling average for 12 months.

Kindly assist with a to how to calculate the 12 month rolling average and plot it on the graph.

 

For example : If Month is March,2023 it should take last 12 month rolling average.

 

I have tried with quick measure it's not working. 

 

 

Your input will be of greata help !

 

20 Replies

  • mircealitoiu's avatar
    mircealitoiu
    Frequent Visitor

    Hi Anonymous ,

     

    I have done a similar thing. Have a look at the below and let me know if it solves your issue. ( it might need some adaptation).

     

    1.Create a calculated table with the dates that will be used as a slicer
    SlicerTable = VALUES(SourceTable[Date])

     

    2.Do a measure in your IDs table.

    12MonthsAverage =
    VAR SelectedDate = SELECTEDVALUE(SlicerTable[Date])
    VAR CurrentDate = SELECTEDVALUE(IDsTable[Date])
    RETURN IF(CurrentDate <= SelectedDate &&
    CurrentDate > EOMONTH(SelectedDate,-12),
    Count(IDsTable[CaseId]),0)

  • Hi , Anonymous 

    According to your description, you want to get the "12 months rolling average".

    Here are the steps you can refer to :
    (1)This is my test data:

    To simplify the complexity of creating data, I'm using MAX instead of your COUNT here.

    I create a measure to put on the visual:

    Max Value(Like your Count of Value) = MAX('Table'[Value])

     

    (2)We can add a measure like this to get it:

    12 month rolling average = var _cur_date= MAX('Date'[Date])
    var _last_12_month = EOMONTH(_cur_date,-12)+1
    var _last_12_month_yearMonth = YEAR(_last_12_month)*100+MONTH(_last_12_month)
    var _cur_year_month = MAX('Date'[Year_month])
    var _t = SUMMARIZE(ALLSELECTED('Date'),'Date'[Year_month] , "value",[Max Value(Like your Count of Value)])
    var _t2 = FILTER(_t ,[Year_month]>= _last_12_month_yearMonth && [Year_month]<= _cur_year_month)
    return
    AVERAGEX(_t2,[value])

     

     

     

    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.

    Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

     

    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

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi,

       

      1212rollingaverage = var currdate = MAX('Case'[CaseDateValue])
      VAR last12months = EOMONTH(currdate, -12)+1
      VAR last12monthyearmonth = YEAR(last12months)*100 + MONTH(last12months)
      VAR curryearmonth = MAX('Case'[Yearmonth])
      var t = SUMMARIZE(ALLSELECTED('Case'),'Case'[Yearmonth],"CaseId",[Countcaseid])
      var t2 = FILTER(t,'Case'[Yearmonth] >= last12monthyearmonth && 'Case'[Yearmonth] <= curryearmonth)
      return
      AVERAGEX(t2, [CaseId])
       
      The data that has been shown after the measure is blank. 
      • v-yueyunzh-msft's avatar
        v-yueyunzh-msft
        Community Support

        Hi , Anonymous 

        You need to add a dimension date table like this:

         

        Date = ADDCOLUMNS( CALENDAR(FIRSTDATE('Table'[Date]) , LASTDATE('Table'[Date])) ,"Year_month", YEAR([Date])*100+  MONTH([Date]))

         

        Then update the dax to this:

         

        1212rollingaverage = var currdate = MAX('Date'[Date])
        VAR last12months = EOMONTH(currdate, -12)+1
        VAR last12monthyearmonth = YEAR(last12months)*100 + MONTH(last12months)
        VAR curryearmonth = MAX('Date'[Year_month])
        var t = SUMMARIZE(ALLSELECTED('Date'),'Case'[Year_month],"CaseId",[Countcaseid])
        var t2 = FILTER(t,[Year_month] >= last12monthyearmonth && [Year_month] <= curryearmonth)
        return
        AVERAGEX(t2, [CaseId])

         

         

         

         

        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 also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )

        Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

         

        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

         

    • Anonymous's avatar
      Anonymous
      Not applicable

      I need to use casedatevalue as a slicer and when I select year for ex 2023. It should do the 12 month rolling average from the selected year i.e in this case 2023 and the current month. Example : If I select 2023 from slicer, it should do the rolling average from the current selected year taking into account the current date/month

    • Anonymous's avatar
      Anonymous
      Not applicable
      CaseIdCaseDateValue
      2226312/24/2022 0:00
      223292/13/2023 0:00
      2235912/5/2022 0:00
      7256812/31/2022 0:00
      7706812/21/2022 0:00
      7822912/6/2022 0:00
      7884512/7/2022 0:00
      7886512/12/2022 0:00
      8106512/8/2022 0:00
      827793/1/2023 0:00
      827901/1/2023 0:00
      851451/30/2023 0:00
      876731/20/2023 0:00
      909301/1/2023 0:00
      911376/1/2023 0:00
      936341/30/2023 0:00
      937851/1/2023 0:00
      946051/1/2023 0:00
      9567212/8/2022 0:00
      9619512/21/2022 8:31
      962841/1/2023 0:00
      964121/1/2023 0:00
      964161/1/2023 0:00
      964211/1/2023 0:00
      964221/1/2023 0:00
      964231/1/2023 0:00
      964241/1/2023 0:00
      964261/1/2023 0:00
      964271/1/2023 0:00
      964291/1/2023 0:00
      964311/1/2023 0:00
      964321/1/2023 0:00
      964341/1/2023 0:00
      964351/1/2023 0:00
      964361/1/2023 0:00
      964371/1/2023 0:00
      968421/1/2023 0:00
      9684712/27/2022 10:34
      968751/1/2023 0:00
      969901/1/2023 0:00
      9709612/1/2022 0:38
      9709912/1/2022 0:00
      9710412/1/2022 11:59
      9710612/1/2022 0:00
      9710912/1/2022 12:51
      9712812/1/2022 0:00
      9714612/1/2022 13:00
      9715212/1/2022 12:12
      9715812/1/2022 18:54
      9719912/1/2022 0:00
      9720012/1/2022 0:00
      9720112/1/2022 0:00
      9720212/2/2022 0:00
      9720412/2/2022 10:08
      9721412/2/2022 8:01
      9725112/2/2022 11:50
      9725212/2/2022 17:00
      9725712/2/2022 0:00
      9726612/2/2022 0:00
      9726912/2/2022 14:09
      9727012/2/2022 0:00
      9727112/2/2022 14:21
      9728012/2/2022 15:45
      9728312/2/2022 0:00
      9728712/1/2022 0:00
      9730812/2/2022 0:00
      9732212/4/2022 22:58
      9732812/2/2022 11:00
      9733112/5/2022 0:00
      9733912/12/2022 0:00
      9734112/19/2022 0:00
      9734212/1/2022 0:00
      9735412/5/2022 15:28
      9735712/5/2022 15:32
      9736012/1/2022 0:00
      9738112/5/2022 0:00
      9740312/1/2022 0:00
      9740912/5/2022 0:00
      9741512/1/2022 15:38
      9741612/5/2022 0:00
      9741712/5/2022 0:00
      9741812/5/2022 0:00
      9742212/5/2022 16:58
      9742312/5/2022 17:05
      9742412/5/2022 0:00
      9743312/5/2022 0:00
      9743612/6/2022 6:41
      9743712/6/2022 6:44
      9743812/2/2022 0:00
      9743912/2/2022 0:00
      9744012/2/2022 0:00
      9744312/1/2022 7:02
      9744412/1/2022 7:05
      9744512/6/2022 11:35
      9744712/1/2022 0:00
      9744812/1/2022 7:16
      9744912/6/2022 7:20
      9745112/6/2022 7:34
      9745612/3/2022 0:00
      9746412/6/2022 0:00
    • Anonymous's avatar
      Anonymous
      Not applicable

      I need to use casedatevalue as a slicer and when I select year for ex 2023. It should do the 12 month rolling average from the selected year i.e in this case 2023 and the current month. Example : If I select 2023 from slicer, it should do the rolling average from the current selected year taking into account the current date/month.

    • Anonymous's avatar
      Anonymous
      Not applicable

      I need to have the rolling 12 month rolling average.I need to use casedatevalue as a slicer and when I select year for ex 2023. It should do the 12 month rolling average from the selected year i.e in this case 2023 and the current month. Example : If I select 2023 from slicer, it should do the rolling average from the current selected year taking into account the current date/month.