Forum Discussion

Ethanhunt123's avatar
Ethanhunt123
Icon for Helper IV rankHelper IV
6 years ago
Solved

Rolling last months / Weeks

I have two tables ( A calendar table (4/4/5 logic), Sales table) I want to calculate the rolling average of last 3/6/12 Months and also last 13/26/52 weeks. For example, If I would calculate for April 2020 it would be March 2020 + Feb 2020 + Jan 2020 / 3 For First 3 Months of the date table ( Nov 2019, Dec 2019, Jan 2020 it would be 0 because I will not be having the last 3 months for those). Sample Output would be 

 

Month Sales Avg 3 Months
April 2020 10 13.3
March 2020 10 16.6
Feb 2020 20 13.3
Jan 2020 10 0
Dec 2019 20 0
Nov 2019 10 0

 

  • Hi Ethanhunt123 ,

     

    Refer to:

    Measure =
    IF (
        MAX ( Sales[Date] ) <= MAX ( DimDate[Date] )
            && MAX ( Sales[Date] ) >= EDATE ( MAX ( DimDate[Date] ), -3 ),
        CALCULATE (
            AVERAGE ( Sales[Sales] ),
            FILTER (
                ALL ( Sales ),
                Sales[Date] <= MAX ( DimDate[Date] )
                    && Sales[Date] >= EDATE ( MAX ( DimDate[Date] ), -3 )
            )
        )
    )

    Sample .pbix

     

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

2 Replies