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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply

Calculate previous month sales

Hi guys, I'm currently want to calculate previous month sale. This is my data table

Nerdywantocode_1-1701789329084.png

 

Nerdywantocode_2-1701789506939.png

For some reasons, I dont want to convert the column month into date to use DAX like PREVIOUS or EARLIER. My idea is that to calculate based on the value of the most nearest number, but I dont know how to do this. Please help me to do this, or if you have any greater idea, please share for me.

 

Best regards,

 

Nerd

2 ACCEPTED SOLUTIONS
Jamie_Scott
Resolver II
Resolver II

Didn't think about that. Can you not just convert the month to date with DAX in a new column like:


MonthToDateConverted =
DATE(LEFT(SalesTable[Month], 4), RIGHT(SalesTable[Month], 2), 1)

Then use 
PreviousMonthSales =
 CALCULATE(SUM(SalesTable[SALES]), PREVIOUSMONTH(SalesTable[MonthToDateConverted]), REMOVEFILTERS())

Jamie_Scott_2-1701791095922.png

 

View solution in original post

Hi there,

 

I think it's because of the REMOVEFILTERS() piece. Could you try changing the measure to use the ALLEXCEPT function;

 

CALCULATE(
SUM(SalesTable[SALES]),
PREVIOUSMONTH(SalesTable[MonthToDateConverted]),
ALLEXCEPT(SalesTable, SalesTable[Product])
)

 

Jamie_Scott_0-1701852141087.png

 

View solution in original post

7 REPLIES 7
Jamie_Scott
Resolver II
Resolver II

Didn't think about that. Can you not just convert the month to date with DAX in a new column like:


MonthToDateConverted =
DATE(LEFT(SalesTable[Month], 4), RIGHT(SalesTable[Month], 2), 1)

Then use 
PreviousMonthSales =
 CALCULATE(SUM(SalesTable[SALES]), PREVIOUSMONTH(SalesTable[MonthToDateConverted]), REMOVEFILTERS())

Jamie_Scott_2-1701791095922.png

 

Hi Jamie, I want to ask you one more question. If I expand my table with product name like this:

Nerdywantocode_0-1701831918679.png

and I used your DAX, when I use matrix visual to breakdown amount of product sold, the value of previous month did not show amount of each product, instead the total amount of previous month of all products

Nerdywantocode_1-1701832011232.png

Could you help me resolve this problem.

Kind regards, 

 

Hi there,

 

I think it's because of the REMOVEFILTERS() piece. Could you try changing the measure to use the ALLEXCEPT function;

 

CALCULATE(
SUM(SalesTable[SALES]),
PREVIOUSMONTH(SalesTable[MonthToDateConverted]),
ALLEXCEPT(SalesTable, SalesTable[Product])
)

 

Jamie_Scott_0-1701852141087.png

 

Thanks a lot. It works like charm

Thank you so much, it works now 😄

Jamie_Scott
Resolver II
Resolver II

If you don't want to convert to date format... It looks like you're using a numerical value, which should always increase in value, so you can just take the max -1 from that value.

 

Sales PrevMo =
VAR PrevMonth = MAX('Table'[Month]) - 1
RETURN
CALCULATE(SUM('Table'[Sales]), 'Table'[Month] = PrevMonth)

Nice idea, I tried your solution earlier, but the problem when year change, the calculation skip it. The result is like this  😄

Nerdywantocode_0-1701790195457.png

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.