Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let
- //Change next line to reflect actual data source
- Source = Production,
- #"Changed Type" = Table.TransformColumnTypes(Source,{ {"StoreNumber", Int64.Type}, {"StoreName", type text}, {"DepartmentName", type text}, {"WeekEnding", type date}, {"Week", Int64.Type}, {"Year", Int64.Type}, {"EndingInventoryAmount", Int64.Type}}),
- /*If data not sorted as per your example add a sorting step here*/
- //Add Index for subsequent sorting
- #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
- //group by StoreNumber, StoreName and DepartmentName
- //then add aggregation with a shifted EndingInventoryAmount column
- //which would be the Beg_Inv for that row
- #"Grouped Rows" = Table.Group(#"Added Index", {"StoreNumber", "StoreName", "DepartmentName"}, { {"Starting", (t)=>Table.FromColumns( Table.ToColumns(t) & {{null} & List.RemoveLastN(t[EndingInventoryAmount],1)}, type table [StoreNumber=nullable number, StoreName=nullable text, DepartmentName=nullable text, WeekEnding=nullable date, Week=nullable number, Year=nullable number, EndingInventoryAmount=nullable number, Index=number, Beg_Inv=number])}}),
- //Expand the subtables
- #"Expanded Starting" = Table.ExpandTableColumn(#"Grouped Rows", "Starting", {"WeekEnding", "Week", "Year", "EndingInventoryAmount", "Index", "Beg_Inv"}),
- //Sort back to original order
- //Then remove the Index column and set the data types
- #"Sorted Rows" = Table.Sort(#"Expanded Starting",{{"Index", Order.Ascending}}), #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"}), #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"StoreNumber", Int64.Type}, {"StoreName", type text}, {"DepartmentName", type text}, {"WeekEnding", type date}, {"Week", Int64.Type}, {"Year", Int64.Type}, {"EndingInventoryAmount", Int64.Type}, {"Beg_Inv", Int64.Type}}) in #"Changed Type1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement