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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
up60
Regular Visitor

Subtracting yesterday´s value from today´s when several items exist per day

Hello community,

 

I have table with several products which stores the accumulated sales of that product on each day. i.e., the total sales of that product since the beginning of the month are stored everyday. (sample below)

 

ProductDayTotal Sales Since beginning of Month
X01/11/202310
Y01/11/202315
Z01/11/20239
X02/11/202318
Y02/11/202321
Z02/11/202316
X03/11/202332
Y03/11/202330
Z03/11/202327


I am looking to create a new column "Daily Sales" and store the daily sales for each product as shown below -

ProductDayTotal Sales Since beginning of MonthDaily Sales
X01/11/20231010
Y01/11/20231515
Z01/11/202399
X02/11/2023188
Y02/11/2023216
Z02/11/2023167
X03/11/20233214
Y03/11/2023309
Z03/11/20232711

 

Can someone please help with this?

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZczLDYAwDIPhXXyu1MQVr02AKPuvAcolKRz9yfrNcKJBtKt2Csc7VODNcP18Cb+/fgRHhvW+Z6Y6NTPTf83OKD6YncklO9W5wf0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Day = _t, Total = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Day", type date}, {"Total", Int64.Type}},"en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Daily", each  try [Total]- Table.Sort(Table.SelectRows(#"Changed Type",(k)=> [Product]=k[Product] and k[Day]<[Day]),{"Day",Order.Descending}){0}[Total] otherwise [Total],Int64.Type)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZczLDYAwDIPhXXyu1MQVr02AKPuvAcolKRz9yfrNcKJBtKt2Csc7VODNcP18Cb+/fgRHhvW+Z6Y6NTPTf83OKD6YncklO9W5wf0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Day = _t, Total = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Day", type date}, {"Total", Int64.Type}},"en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Daily", each  try [Total]- Table.Sort(Table.SelectRows(#"Changed Type",(k)=> [Product]=k[Product] and k[Day]<[Day]),{"Day",Order.Descending}){0}[Total] otherwise [Total],Int64.Type)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Hi @lbendlin ,

 

This works, thank you very much.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.