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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
mes0012
Frequent Visitor

Calculating previous months sales using monthly data

Hi All,

 

I'm trying to build a formula into dax that calculates the previous months sales depending on which month is selected. For example in the screenshot below i want 'Sales (£) PM' to pull through July sales when August is selected in the Month filter.

 

mes0012_0-1697010816454.png

The data is at month level therefore a lot of the solutions posted on this forum aren't giving me the desired result, I have attached the calendar table below:

 

mes0012_1-1697012022997.png

 

Any help would be massively appreicated!

 

Thanks,

Matt.

 

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @mes0012 ,

 

I suggest you to create a DimDate table with continuous date in it and then try PREVIOUSMONTH.

 

If this reply still couldn't help you solve your issue, please share a sample file with us and show us a screenshot with the result you want.

 

Best Regards,
Rico Zhou

 

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

 

 

View solution in original post

4 REPLIES 4
v-rzhou-msft
Community Support
Community Support

Hi @mes0012 ,

 

I suggest you to create a DimDate table with continuous date in it and then try PREVIOUSMONTH.

 

If this reply still couldn't help you solve your issue, please share a sample file with us and show us a screenshot with the result you want.

 

Best Regards,
Rico Zhou

 

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

 

 

ValtteriN
Super User
Super User

Hi,

Here is one way to do this:
Data:

ValtteriN_0-1697013706231.png

Relation:

ValtteriN_1-1697013728813.png

Dax:

LM = var _cmonth = MAX('Table (7)'[YearMonth])
return
CALCULATE(max('Table (7)'[Value]),ALL('Table (7)'[YearMonth]),'Table (7)'[YearMonth]<_cmonth)

End result:
ValtteriN_2-1697013862795.png

 

Here we remove YearMonth filter context and take the maximum value which is lower than current mont -> last month as context.


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!




@ValtteriN, thanks for coming back on this.

I've given the above suggestion a go but i can't get it to work. I created a new table, summarised in the same way as in your suggestion above (Total Debit Balance is the original field name before i've renamed it in the visual):

mes0012_0-1697106725484.png

mes0012_1-1697106725261.png

However, when i build the measure i'm still getting blanks:

 
Sales (£) PM = var _cmonth = MAX('AP - DR PM Table'[Y/MN Key])
return
CALCULATE(max('AP - DR PM Table'[Sales]),ALL('AP - DR PM Table'[Y/MN Key]),'AP - DR PM Table'[Y/MN Key]<_cmonth)
 

Any chance you can see where i've gone wrong from those screenshots?

 

Thanks,

Matt.

Hi @mes0012 ,

Now that I revisited this I realized that there isn't anything preventing you from using previousmonth like @v-rzhou-msft  suggested. 

Here is a bit better dax (if you don't want to use PREVIOUSMONTH for one reason or another):

LM =

var _cmonth = MAX('Calendar'[YearMonth])
 var _lmonth = CALCULATE(MAX('Table (7)'[YearMonth]),ALL('Table (7)'),'Table (7)'[YearMonth]<_cmonth)
 return

CALCULATE(MAX('Table (7)'[Value]),ALL('Table (7)'),'Table (7)'[YearMonth]= _lmonth)

And here is one with PREVIOUSMONTH:

 
LM2 = CALCULATE(MAX('Table (7)'[Value]),PREVIOUSMONTH('Calendar'[Date]))

End results:

ValtteriN_0-1697437220397.png

 





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

Proud to be a Super User!




Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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