Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
smko
Helper I
Helper I

Getting previous 3 months sales based on MTD Sales column

Greetings people, I'm trying to get previous 3 months sales amount in current row, however due to the raw data is in Month-To-Date structure, MTD sales column will always reset to 0 at the beginning of the month, I find it hard find the correct solution. Here is how the raw data looks like, do note the date will not end at last day of the month.

 

Date MTD Sales Excepted Result 
29/06/2021 100  
30/07/2021 120100=0+0+100
30/08/2021 200220=0+100+120
05/09/2021 50420=100+120+200
12/09/2021 70420=100+120+200
29/09/2021 300420=100+120+200
03/10/2021 30620=120+200+300

 

I tried the code but it will sum up all sales if there are multiple dates in a month. My idea is to get the last non blank date of each 3 previous month then maybe tweak the logic in my code. Appreciate anyone can provide better code and logic

 

 

Last3monthsSales = 
VAR Prev3months = STARTOFMONTH(DATEADD('Calendar'[Date],-3,MONTH))
VAR SOmonth = STARTOFMONTH('Calendar'[Date])
VAR Result = 
    CALCULATE(
        SUM(Sales[MTD Sales]),
        FILTER(
            ALL('Calendar'[Date]),
            'Calendar'[Date] < SOmonth && 'Calendar'[Date] >= Prev3months
        )
    )
RETURN Result

 

1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Please try to create a new column to calculate sales on the last date of each month.

last_sales = 
var max_date = CALCULATE(MAX('Table'[Date]),ALLEXCEPT('Table','Table'[year_month]))
return IF('Table'[Date]=max_date,'Table'[ MTD Sales ])

Vlianlmsft_0-1634890177227.png

 

View solution in original post

3 REPLIES 3
V-lianl-msft
Community Support
Community Support

Please try to create a new column to calculate sales on the last date of each month.

last_sales = 
var max_date = CALCULATE(MAX('Table'[Date]),ALLEXCEPT('Table','Table'[year_month]))
return IF('Table'[Date]=max_date,'Table'[ MTD Sales ])

Vlianlmsft_0-1634890177227.png

 

amitchandak
Super User
Super User

@smko , Takes sales base measure

 

Rolling 3 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date ],eomonth(MAX('Date'[Date ]),0) ,-3,MONTH))

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi there @amitchandak , there is no Sales Amount column, only has MTD Sales column

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.