Forum Discussion
Creating a custom column that calculates the previous row value by previous hour, date, & store
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.
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?
- ronrosenfeld2 years agoFrequent Visitor
Maybe you sorted the data after instead of before the calculations were done. If that's not the problem, I'd need to see the actual data that is producing that result, not a screenshot.
- Anonymous2 years agoNot applicable
Thanks! Sorry for the late reply. I"m going to try to post sample. I've tried multiples times and couldn't figure it out.