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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Power BI Header Change

Power BI Header Update

Read about the exciting updates for the Power BI forum.