Forum Discussion
Creating a custom column that calculates the previous row value by previous hour, date, & store
If your logic is to calculate the difference between two rows, given a matching "Store-Ke" and "Date", you are going to have at least one negative value given the data you show in your second picture. If negative values cannot exist, you may need to rethink your logic.
- Anonymous2 years agoNot applicable
I think a logic can be placed if there is negative numbers. If negative, then bring back 0 as technically 0 orders would have been orders dropped to that store for that hour and date. However, trying to come up with the entire logic is where i'm struggling.
- ronrsnfld2 years agoSuper User
Given your stated logic, and starting from the data in your second screenshot, the following code seems to do what you say you want:
Documentation is within the code and comments
let //change next line to refer to your last table Source = Excel.CurrentWorkbook(){[Name="Table23"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{ {"Store- Ke", Int64.Type}, {"Date", type date}, {"Order Create Hour (UTC)", type time}, {"Total Consumed (w/o Cancellations)", Int64.Type}}), //Group by Store-Ke and Date // you can add a sort if necessary // then aggregate to create Orders Dropped column according to your state logic #"Grouped Rows" = Table.Group(#"Changed Type", {"Store- Ke", "Date"}, { {"Orders Dropped by Hour", (t)=> let //To sequential rows, we add a column which is the same column shifted up one // the logic ensures that the first row in each subgroup will be set to the original value // and that no value can be less than zero (0) #"Shift Consume" = Table.FromColumns( Table.ToColumns(t) & {{null} & List.RemoveLastN(t[#"Total Consumed (w/o Cancellations)"],1)}, {"Store-Ke", "Date","Order Create Hour (UTC)","Total Consumed (w/o Cancellations)", "Shifted Consumed"}), #"Add Orders Dropped" = Table.AddColumn( #"Shift Consume","Orders Dropped by Hour", each List.Max({0, ([#"Total Consumed (w/o Cancellations)"] - [Shifted Consumed]) ?? [#"Total Consumed (w/o Cancellations)"]}) , Int64.Type ) in #"Add Orders Dropped", type table[#"Order Create Hour (UTC)"=time, #"Total Consumed (w/o Cancellations)"=Int64.Type, Orders Dropped by Hour=Int64.Type] } }), #"Expanded Orders Dropped by Hour" = Table.ExpandTableColumn(#"Grouped Rows", "Orders Dropped by Hour", {"Order Create Hour (UTC)", "Total Consumed (w/o Cancellations)", "Orders Dropped by Hour"}) in #"Expanded Orders Dropped by Hour"- Anonymous2 years agoNot applicable
Hi ronrsnfld ! Thanks. I"ve tried your method but noticed that some of the numbers are off. It seems that as we get towards the later hours (for example 5 PM), the running difference (orders drop by hour) would be off. The screenshot below shows 74 orders were dropped at 5 PM. However, the total consumed at 4 PM was 117 and at 5 PM, it was 129. This would mean 5 PM would have had 12 orders dropped to that store. I checked my sort and it looks right. Any idea why this would be?