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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
ShamR9T
Frequent Visitor

Change Cumulative Sums back to differences

I have a data source in the following form

 

Sales PersonLocationItem1-Jan-238-Jan-2315-Jan-2322-Jan-23
A1Z1122
B2X0012
C3Y0123
A3X1577
B2Y0000
C1Z0000

 

It is an aggregation for each sales person where their sales has been and the item and it shows progressively how many total sales they have made by each day. EG: Sales Person A has sold 1 X at location 3 on 1 January and by 8 January this number increased to 5 which means they have sold 4 more items.

 

I need to change this data to the following format. I have been able to unpivot the dates and create the first 5 columns but done know how to create the sales each day column. Wouldnt mind a calculated column or a power query to achieve this.

 

Sales PersonLocationItemDateCumulative SumSales Each Day
A1Z1/1/202311
A1Z1/8/202310
A1Z1/15/202321
A1Z1/22/202320
B2X1/1/202300
B2X1/8/202300
B2X1/15/202311
B2X1/22/202321
C3Y1/1/202300
C3Y1/8/202311
C3Y1/15/202321
C3Y1/22/202331
A3X1/1/202311
A3X1/8/202354
A3X1/15/202372
A3X1/22/202370
B2Y1/1/202300
B2Y1/8/202300
B2Y1/15/202300
B2Y1/22/202300
C1Z1/1/202300
C1Z1/8/202300
C1Z1/15/202300
C1Z1/22/202300

 

Sorry for the messy title and long explanations. Hope the issue is clear though.

 

Thank you

1 ACCEPTED SOLUTION
rubayatyasmin
Super User
Super User

Hi, @ShamR9T 

 

there was some miss calculations in the previous solution. 

 

rubayatyasmin_0-1696921830764.png

the solution is okay now. 

 

There is some workaround in this query that you need to modify. 

 

Here is the PQ code

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7MSS1WCEgtKs7PU9JR8slPTizJBDM9S1JzgZShrldinq6RMZBpgWAamiLYRkYwdqxOtJIjSBaIo6A0CBuBMUjWCcqLAGIDKDaEyzoDWSATI1FkQGIwk42hekEypkBsDsbIJkcimQzBMJNhrkKTjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}, {"(blank).5", type text}, {"(blank).6", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Sales Person", type text}, {"Location", Int64.Type}, {"Item", type text}, {"1-Jan-23", Int64.Type}, {"8-Jan-23", Int64.Type}, {"15-Jan-23", Int64.Type}, {"22-Jan-23", Int64.Type}}),
   // Step 2: Unpivot the date columns
    UnpivotedColumns = Table.UnpivotOtherColumns(#"Changed Type1", {"Sales Person", "Location", "Item"}, "Date", "Value"),

   // Step 3: Group by Sales Person, Location, Item, and Date
    GroupedTable = Table.Group(UnpivotedColumns, {"Sales Person", "Location", "Item", "Date"}, {{"Cumulative Sum", each List.Max([Value]), type number}}),
    AddIndex = Table.AddIndexColumn(GroupedTable, "Index", 0, 1, Int64.Type),

     AddPreviousDaySales = Table.AddColumn(AddIndex, "Previous Day Sales", each if [Index] > 0 then AddIndex{[Index]-1}[Cumulative Sum] else 0),
    AddSalesEachDay = Table.AddColumn(AddPreviousDaySales, "Sales Each Day", each [Cumulative Sum] - [Previous Day Sales]),
    
    // Step 4: Remove unnecessary columns
    RemovedColumns = Table.RemoveColumns(AddSalesEachDay,{"Index", "Previous Day Sales"})
in
    RemovedColumns

 

try to understand the values that is showing minus, the calculation is alright with the given logic. You wan to subtract values with the previous values. You need to adjust it a little bit. But this should be very helpful. 

 

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


View solution in original post

3 REPLIES 3
rubayatyasmin
Super User
Super User

Hi, @ShamR9T 

 

there was some miss calculations in the previous solution. 

 

rubayatyasmin_0-1696921830764.png

the solution is okay now. 

 

There is some workaround in this query that you need to modify. 

 

Here is the PQ code

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7MSS1WCEgtKs7PU9JR8slPTizJBDM9S1JzgZShrldinq6RMZBpgWAamiLYRkYwdqxOtJIjSBaIo6A0CBuBMUjWCcqLAGIDKDaEyzoDWSATI1FkQGIwk42hekEypkBsDsbIJkcimQzBMJNhrkKTjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}, {"(blank).5", type text}, {"(blank).6", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Sales Person", type text}, {"Location", Int64.Type}, {"Item", type text}, {"1-Jan-23", Int64.Type}, {"8-Jan-23", Int64.Type}, {"15-Jan-23", Int64.Type}, {"22-Jan-23", Int64.Type}}),
   // Step 2: Unpivot the date columns
    UnpivotedColumns = Table.UnpivotOtherColumns(#"Changed Type1", {"Sales Person", "Location", "Item"}, "Date", "Value"),

   // Step 3: Group by Sales Person, Location, Item, and Date
    GroupedTable = Table.Group(UnpivotedColumns, {"Sales Person", "Location", "Item", "Date"}, {{"Cumulative Sum", each List.Max([Value]), type number}}),
    AddIndex = Table.AddIndexColumn(GroupedTable, "Index", 0, 1, Int64.Type),

     AddPreviousDaySales = Table.AddColumn(AddIndex, "Previous Day Sales", each if [Index] > 0 then AddIndex{[Index]-1}[Cumulative Sum] else 0),
    AddSalesEachDay = Table.AddColumn(AddPreviousDaySales, "Sales Each Day", each [Cumulative Sum] - [Previous Day Sales]),
    
    // Step 4: Remove unnecessary columns
    RemovedColumns = Table.RemoveColumns(AddSalesEachDay,{"Index", "Previous Day Sales"})
in
    RemovedColumns

 

try to understand the values that is showing minus, the calculation is alright with the given logic. You wan to subtract values with the previous values. You need to adjust it a little bit. But this should be very helpful. 

 

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


rubayatyasmin
Super User
Super User

Hi, @ShamR9T 

 

Is this what you are looking for?

 

rubayatyasmin_0-1696839191785.png

 

find the solution file

https://file.io/NKZpL7JQlvX6

 

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi @rubayatyasmin 

 

Sorry, this is not. if you look at the sales each day, it still shows it as the same value as the cumulative sum. I need the value for each product to subtract from the previous days value. 

 

For example:
Sales Person A

Location 1

Product Z

 

1st Jan is 1

8 Jan is 1 

 

Hence 1 Jan sales each day should be 1 

but 8 Jan should be 0 since its still 1 on 8 Jan. 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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