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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
jay_patel
Helper IV
Helper IV

DAX for Rolling Sales for Current 12 Month & Previous 12 Month

Hello All,

I have used dax to get sales for current 12 months:- 

Rolling sales  current 12 month =
CALCULATE([sales],DATESINPERIOD('Date'[Invoice date],MAX('Date'[Invoice date]),-11,MONTH))

and this is getting sales from Sept 2021 to Aug 2022.

to get rolling sales for previous 12 months:-
Rolling Sales for previous 12 months =
var x = CALCULATE([sales]),DATESINPERIOD('Date'[Invoice date],MAX('Date'[Invoice date]),-11,MONTH))
var pre = CALCULATE([sales],DATESINPERIOD('Date'[Invoice date],MAX('Date'[Invoice date]),-23,MONTH))
var final = pre - x
returnfinal

and this is getting sales from Sept 2020 to Aug 2021.

which is working fine for those customers who's having sales for both i.e., Current 12 months & previous 12 months.

But for those customer who don't have any sales in these current 12 month's the dax is giving sales of previous 12 month's instead of zero.

I think it is just calculating sales for any 12 month's in which the sales is done.

Please correct the dax if it is wrong.

Thanks in advance


1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You should create a Calendar Table with a relationship (Many to One and Single) from the Invoice Date column to the Date column of the Calendar Table.  In the Calendar Table, write calculated column formulas for Year, Month Name and Month number.  Sort the Month name column by the Month number column.  Create slicers for Year and Month name and select 2022 and August.  Write these measures:

Rolling sales  current 12 month =
CALCULATE([sales],datesbetween('Calendar'[Date],edate(min(calendar[Date]),-11),max(calendar[Date])))

 

Rolling Sales for previous 12 months = calculate([sales],datesbetween(Calendar'[Date],edate(min(Calendar[Date]),-23),edate(max(calendar[Date]),-12)))

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

You should create a Calendar Table with a relationship (Many to One and Single) from the Invoice Date column to the Date column of the Calendar Table.  In the Calendar Table, write calculated column formulas for Year, Month Name and Month number.  Sort the Month name column by the Month number column.  Create slicers for Year and Month name and select 2022 and August.  Write these measures:

Rolling sales  current 12 month =
CALCULATE([sales],datesbetween('Calendar'[Date],edate(min(calendar[Date]),-11),max(calendar[Date])))

 

Rolling Sales for previous 12 months = calculate([sales],datesbetween(Calendar'[Date],edate(min(Calendar[Date]),-23),edate(max(calendar[Date]),-12)))

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

DATESINPERIOD may not be the best option here.  Easier to use either DATEADD or SAMEPERIODLASTYEAR (the latter twice)

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.