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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
bimagty
Frequent Visitor

Calculate the previous month value with the same date range

Hello,

 

I have problem with defining dax for calculating the sum of previous month, the conditions:

- This month is February and the data is only available until 19 February, I have calculated this month ongoing sum which is from 1-19 February as selected month measure.

- I want to calculate the same period in previous month but with the same date range as I have now, i.e. sum of sales 1-19 February vs. sum of 1-19 January.

 

Ive tried to use this formula (shown below), but it calculates the entire sum of sales in January instead of 1-19 January only.

What step do I miss? Really need your help, thanks in advance guys 🙂

 

bimagty_0-1646117700340.png

 

2 ACCEPTED SOLUTIONS
ValtteriN
Super User
Super User

Hi,

Try switching the order of your fucntions: 


Measure  = CALCULATE(SUM(Aggregation[Duration(Secs)]),datesmtd(DATEADD('Calendar'[Date],-1,MONTH)))

This will calculate MTD amounts for the previous month (using the same date range).


I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

v-jingzhang
Community Support
Community Support

Hi @bimagty 

 

You can try these measures. I attached a sample pbix at bottom. 

This Month = 
VAR _endDate = MAX(Revenues[Date])
VAR _startDate = EOMONTH(_endDate,-1)+1
RETURN
CALCULATE(SUM(Revenues[Revenue]),DATESBETWEEN('Calendar'[Date],_startDate,_endDate))
Previous Month = 
VAR _maxDate = MAX(Revenues[Date])
VAR _startDate = EOMONTH(_maxDate,-2) + 1
VAR _endDate = _startDate + DAY(_maxDate) - 1
RETURN
CALCULATE(SUM(Revenues[Revenue]),DATESBETWEEN('Calendar'[Date],_startDate,_endDate))

vjingzhang_0-1646380169989.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

5 REPLIES 5
v-jingzhang
Community Support
Community Support

Hi @bimagty 

 

You can try these measures. I attached a sample pbix at bottom. 

This Month = 
VAR _endDate = MAX(Revenues[Date])
VAR _startDate = EOMONTH(_endDate,-1)+1
RETURN
CALCULATE(SUM(Revenues[Revenue]),DATESBETWEEN('Calendar'[Date],_startDate,_endDate))
Previous Month = 
VAR _maxDate = MAX(Revenues[Date])
VAR _startDate = EOMONTH(_maxDate,-2) + 1
VAR _endDate = _startDate + DAY(_maxDate) - 1
RETURN
CALCULATE(SUM(Revenues[Revenue]),DATESBETWEEN('Calendar'[Date],_startDate,_endDate))

vjingzhang_0-1646380169989.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Great solution @v-jingzhang - this is the best and works for me amongst all the hacks I have gone thru so far on the web.

Really helped me out of a tacky situation!

Yeay, great solution!
Thank you very much, now it works well.

 

bimagty_0-1646645959456.png

 

ValtteriN
Super User
Super User

Hi,

Try switching the order of your fucntions: 


Measure  = CALCULATE(SUM(Aggregation[Duration(Secs)]),datesmtd(DATEADD('Calendar'[Date],-1,MONTH)))

This will calculate MTD amounts for the previous month (using the same date range).


I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi,
Thanks for the reply, but unfortunately I still get the same result as before, it calculates the total of 1 month instead of only selected range of date.

 

bimagty_0-1646122817937.png

 

test is the measure following your suggestion, and the Previous Month Revenue is the total revenue in a full month (January).

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors