1
votes

I am working in POWER BI and trying to calculate a DAX expression for the rolling total of the previous month. I have a filter where I select a certain month, I would like to calculate the rolling total for the previous month.

Below is the calculation that works perfectly to calculate the rolling total for the selected date range.

How can I calculate the previous months rolling total?

Rolling_Total_Current_Month = CALCULATE(
        SUM(SalesInvoice[Sales])
        ,FILTER(ALLSELECTED(SalesInvoice), (SalesInvoice[Date]) <= MAX(SalesInvoice[Date])))

Here is a sample of my data, I have sales per day, for multiple categories, (in fact i have a couple more columns of details but this is simplified)

Date    Day Amount  Category
1/1/2016    1   100 A
1/1/2016    1   120 B
1/1/2016    1   90  C
1/2/2016    2   500 A
1/2/2016    2   321 B
1/2/2016    2   143 C

So far I have come up with an equation to solve the rolling total, but when I try to slice is and view the rolling total of a single category it does not work for the previous month. I just keeps the original rolling total for the previous month.

Here is the equation for the rolling total previous month that works. But does not recalculate a rolling total for the previous month once sliced based on category.

PREVIOUS_MONTH_ROLLING_TOTAL = 
CALCULATE(
    [Current Sales]
    ,FILTER(
        ALL( Orders )
        ,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
            && Orders[Day] <= MAX( Orders[Day] )
    )
)
3

3 Answers

1
votes

I have solved how to get the previous months rolling total. You must do three things. First create a Month Number column in your data sheet (this is used as an integer to subtract 1 month from). You must also create a days column as well.

Then create a measure for Current Sales or whatever your value is.

Create a measure for the current month sales

Current Sales = SUM(Orders[Amount])

Then this equation.

PREVIOUS_MONTH_ROLLING_TOTAL = 
CALCULATE(
    [Current Sales]
    ,FILTER(
        ALL( Orders )
        ,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
            && Orders[Day] <= MAX( Orders[Day] )
    )
)

The Idea of this equation is to be able to display the previous months rolling total on a chart with the X axis as "DAY" (so 1-31) Then you can view the current month, previous month, same period last year all on the same chart or table.

1
votes

Try something along these lines:

Rolling_Total_Previous_Month =
    VAR CurrentMonth = MAX(SalesInvoice[Date])
    VAR PreviousMonth = EOMONTH(CurrentMonth,-1)
    RETURN CALCULATE(SUM(SalesInvoice[Sales]), SalesInvoice[Date] <= PreviousMonth)
1
votes

To start of, I have a data like this in a table called as Orders-

Date            Amount
12/12/2017      100
12/12/2017      200
12/12/2017      300
1/1/2018        400
1/1/2018        500

I first create a calculated column called as Year & Month by using the formula:-

Year = YEAR(Orders[Date])
Month = FORMAT(Orders[Date],"mmmm")

Then I create a column called as Month Number which will be helpful for sorting when more than one year is involved in the table and as well as to Identify the Previous Months.

MonthNumber = DATEDIFF(Min(Orders[Date]),Orders[Date],MONTH) 

Current Month Sales can be a measure or a Calculated column. Qn the Question, you had your answer for current month sales via a calculated column and if you want to go for a measure then something like this would work on a summary table.

Current Month Sales = SUm(Orders[Amount])

I would also create a column called as Key:-

Key = Orders[MonthNumber] & Orders[Category]

Now, for the Previous Month Sales, I would create a measure that looks for selected MonthNumber that we created.

Previous Month Sales = 
         Var SelectedCategory = SELECTEDVALUE(Orders[Category])                                                                                                                                                           
         Var SelectedMonthNumberr = SELECTEDVALUE(Orders[MonthNumber]) - 1                                                                                                                                                
         Var ReqKey = SelectedMonthNumberr & SelectedCategory  

   Return 
         IF(ISBLANK(SelectedCategory) <> True(), 
         CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[Key] = ReqKey)),
         CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[MonthNumber] = SelectedMonthNumberr)))                                                                                                                                                                    

or to your Measure

PREVIOUS_MONTH_ROLLING_TOTAL = 
CALCULATE(
    [Current Sales]
    ,FILTER(
        ALL( Orders )
        ,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
            && Orders[Day] <= MAX( Orders[Day] )
    )
)

You can just add another filtering item as && Orders[Category] = SELECTEDVALUE(Orders[Category]) but won't work when you don't have any categories selected or on a table or visual which doesn't have categories. So, you would need to define an if else logic here as I have quoted on my measure formula.

Summary Tables

Do let me know, if this helps or not.