Forum Discussion

sailorman's avatar
sailorman
Frequent Visitor
4 years ago
Solved

Rolling calculation for previous 12 months

I have a cumulative hours (last 12 months) caculator DAX as follows:

 

Cumulative HOURS = CALCULATE( SUM('Unit'[Hours Worked]),
DATESINPERIOD('Unit'[Month],
LASTDATE('Unit'[Month]),-12,MONTH
))
 

This will give me the cumulative value for last 12 months, say June 2021 to May 2022.

 

I now want to plot this agaist cumulative of previous 12 months, that is July 2020 - June 2021. How should I modify DAX to get these values. I want both line graphs in the same plot for comparison. How do I go about doing that?

 

Thanks in advance!

  • sailorman , if you want to 12 months on axis

    Rolling 12 Sales =
    var _max = maxx(allselcted(date),date[date]) // or today()
    var _min = date(year(_max), month(_max)-12,1)
    return
    CALCULATE(SUM(Sales[Sales Amount]),filter(date, date[date] <=_max && date[date] >=_min))

     

    in case you select one month and want to display 12 months, you need an independent date table


    //Date1 is independent Date table, Date is joined with Table
    new measure =
    var _max = maxx(allselected(Date1),Date1[Date])
    var _min = eomonth(_max, -12) +1
    return
    calculate( sum(Table[Value]), filter('Date', 'Date'[Date] >=_min && 'Date'[Date] <=_max))

     

    Need of an Independent Date Table:https://www.youtube.com/watch?v=44fGGmg9fHI

  • sailorman's avatar
    sailorman
    4 years ago

    Hi Thanks. I figured out a way to avoid using seperate date table by using following variables

    Cumulative HOURS previous year =
    VAR CurrentDate =  
        MAX('Unit'[Month])
    VAR StartMonth =
        CurrentDate - 365
    Return
        CALCULATE( SUM('Unit'[Hours Worked]),
            DATESINPERIOD('Unit'[Month],
            StartMonth,-12,MONTH
            )
            )

2 Replies

  • sailorman , if you want to 12 months on axis

    Rolling 12 Sales =
    var _max = maxx(allselcted(date),date[date]) // or today()
    var _min = date(year(_max), month(_max)-12,1)
    return
    CALCULATE(SUM(Sales[Sales Amount]),filter(date, date[date] <=_max && date[date] >=_min))

     

    in case you select one month and want to display 12 months, you need an independent date table


    //Date1 is independent Date table, Date is joined with Table
    new measure =
    var _max = maxx(allselected(Date1),Date1[Date])
    var _min = eomonth(_max, -12) +1
    return
    calculate( sum(Table[Value]), filter('Date', 'Date'[Date] >=_min && 'Date'[Date] <=_max))

     

    Need of an Independent Date Table:https://www.youtube.com/watch?v=44fGGmg9fHI

    • sailorman's avatar
      sailorman
      Frequent Visitor

      Hi Thanks. I figured out a way to avoid using seperate date table by using following variables

      Cumulative HOURS previous year =
      VAR CurrentDate =  
          MAX('Unit'[Month])
      VAR StartMonth =
          CurrentDate - 365
      Return
          CALCULATE( SUM('Unit'[Hours Worked]),
              DATESINPERIOD('Unit'[Month],
              StartMonth,-12,MONTH
              )
              )