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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
SudipRoy1610
Frequent Visitor

Need Help with DAX Function to Calculate Sales Difference between two consecutive Date

Hello,

 

Can someone please help me solve the following query, I have got the following table -

 

DateSales
24-05-2024100
24-05-2024100
27-05-2024200
27-05-2024200
27-05-2024200
27-05-2024200
27-05-2024200
28-05-2024300
28-05-2024300
28-05-2024300
28-05-2024300
28-05-2024300
28-05-2024300
28-05-2024300
29-05-2024400
29-05-2024400
29-05-2024400
29-05-2024400
29-05-2024400
29-05-2024400
29-05-2024400
30-05-2024500
30-05-2024500
30-05-2024500
30-05-2024500
30-05-2024500
30-05-2024500
30-05-2024500
31-05-2024600
31-05-2024600
31-05-2024600
31-05-2024600
31-05-2024600
31-05-2024600
31-05-2024600
31-05-2024700

 

And I need to calculate a cumulative sum for each date as follows -

 

DateTotal Sales
24-05-2024200
27-05-20241000
28-05-20242100
29-05-20242800
30-05-20243500
31-05-20244900

 

And after calculating the Total Sales, I need to calculate T-1 Total Sales as follows -

 

DateTotal SalesTotal Sales T-1
24-05-2024200 
27-05-20241000200
28-05-202421001000
29-05-202428002100
30-05-202435002800
31-05-202449003500

 

Need help to get this sorted. Can someone please help on this?

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @SudipRoy1610 ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

(2) We can create a table.

 

Table 2 = SUMMARIZE('Table',[Date],"Total sales",SUM('Table'[Sales]))

 

(3) We can create a calculated column.

 

Total sales T-1 = 
var _a=[Date]
var tmp=FILTER('Table 2',[Date]<_a)
var _b=MAXX(tmp,[Date])
return
CALCULATE(MAX([Total sales]),FILTER('Table 2',[Date]=_b))

 

(4) Then the result is as follows.

vtangjiemsft_0-1717554286088.png

 

Best Regards,

Neeko Tang

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

Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1717556435773.png

 


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

View solution in original post

7 REPLIES 7
BIstvan
Resolver I
Resolver I

@SudipRoy1610  Thanks to the new visual calculations it can be very easily done. You can see the the steps below

Visual calc1.png

Visual calc2.png

Visual calc3.png

Visual calc4.png

Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1717556435773.png

 


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

I am not sure why but when I create the calendar table at my end it is giving me 1st Jan 2024 as the minimum date and 31st Dec 2024 as the maximum date. Even MAX(Date) is giving me 31st Dec 2024, when the answer should be 31st May 2024. 

Review my file carefully.


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

SudipRoy1610_1-1717567758896.png

 

In the image the data contains period from 6th May 2024 to 31st May 2024 from the main table. The data on the left is a calendar created out of the main table just like you mentioned but the earliest date is 1st of Jan 2024 rather than 6th May 2024 and the latest date is 31st Dec 2024 rather than 31st May 2024. This should not happen but not sure what that is the result.

In the Table calculated table formula, remove the .Date


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

Hi @SudipRoy1610 ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

(2) We can create a table.

 

Table 2 = SUMMARIZE('Table',[Date],"Total sales",SUM('Table'[Sales]))

 

(3) We can create a calculated column.

 

Total sales T-1 = 
var _a=[Date]
var tmp=FILTER('Table 2',[Date]<_a)
var _b=MAXX(tmp,[Date])
return
CALCULATE(MAX([Total sales]),FILTER('Table 2',[Date]=_b))

 

(4) Then the result is as follows.

vtangjiemsft_0-1717554286088.png

 

Best Regards,

Neeko Tang

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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors