Forum Discussion
Loop using the output from previous row as variable
Hi, I'm posting this question after researching and not finding similar question brought up.
I have report that shows the initial quantity, quantity in and quantity out and the week number, I would like to add a new column New_initial which equals to previous week initial + previous week IN - previous week OUT
I there a way to do that in power query M. Thanks in advance.
Desired outcome
There are several ways to do this. Here's a method using the List.Generate function:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0ABKmQGyuFKsTrWQEZBkghEFCxlAhkCJDI7CQCVTIAqIoFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WEEK = _t, INITIAL = _t, IN = _t, OUT = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"WEEK", Int64.Type}, {"INITIAL", Int64.Type}, {"IN", Int64.Type}, {"OUT", Int64.Type}}), #"Add New Initial" = Table.FromColumns( Table.ToColumns(#"Changed Type") & {List.Generate( ()=>[a=#"Changed Type"[INITIAL]{0}, idx=0], each [idx] < Table.RowCount(#"Changed Type"), each [a = [a]+#"Changed Type"[IN]{[idx]} - #"Changed Type"[OUT]{[idx]}, idx=[idx]+1], each [a])}, type table[WEEK=Int64.Type,INITIAL=Int64.Type, IN=Int64.Type, OUT=Int64.Type, NEW_INITIAL=Int64.Type]), #"Reordered Columns" = Table.ReorderColumns(#"Add New Initial",{"WEEK", "INITIAL", "NEW_INITIAL", "IN", "OUT"}) in #"Reordered Columns"Source
Results
You would use the same method, just embed it as an aggregation in the Table.Group function:
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"WEEK", Int64.Type}, {"PRODUCT ID", Int64.Type}, {"INITIAL", Int64.Type}, {"IN", Int64.Type}, {"OUT", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"PRODUCT ID"}, { {"New Initial", (t)=> Table.FromColumns( Table.ToColumns(t) & {List.Generate( ()=>[a=t[INITIAL]{0}, idx=0], each [idx] < Table.RowCount(t), each [a = [a] + t[IN]{[idx]} - t[OUT]{[idx]}, idx=[idx]+1], each [a])}, Table.ColumnNames(t) & {"NEW INITIAL"}), type table[WEEK=Int64.Type,PRODUCT ID=Int64.Type, INITIAL=Int64.Type, IN=Int64.Type, OUT=Int64.Type, NEW INITIAL=Int64.Type]}}), #"Expanded New Initial" = Table.ExpandTableColumn(#"Grouped Rows", "New Initial", {"WEEK", "INITIAL", "IN", "OUT", "NEW INITIAL"}) in #"Expanded New Initial"
5 Replies
- ronrsnfldSuper User
There are several ways to do this. Here's a method using the List.Generate function:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0ABKmQGyuFKsTrWQEZBkghEFCxlAhkCJDI7CQCVTIAqIoFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WEEK = _t, INITIAL = _t, IN = _t, OUT = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"WEEK", Int64.Type}, {"INITIAL", Int64.Type}, {"IN", Int64.Type}, {"OUT", Int64.Type}}), #"Add New Initial" = Table.FromColumns( Table.ToColumns(#"Changed Type") & {List.Generate( ()=>[a=#"Changed Type"[INITIAL]{0}, idx=0], each [idx] < Table.RowCount(#"Changed Type"), each [a = [a]+#"Changed Type"[IN]{[idx]} - #"Changed Type"[OUT]{[idx]}, idx=[idx]+1], each [a])}, type table[WEEK=Int64.Type,INITIAL=Int64.Type, IN=Int64.Type, OUT=Int64.Type, NEW_INITIAL=Int64.Type]), #"Reordered Columns" = Table.ReorderColumns(#"Add New Initial",{"WEEK", "INITIAL", "NEW_INITIAL", "IN", "OUT"}) in #"Reordered Columns"Source
Results
- AnonymousNot applicable
It worked, thank you so much ronrsnfld. Is it possible to do the calculation within every product id as shown below?
I forgot to include the product id variable on my initial request I appologize for that.
Desired outcome
- ronrsnfldSuper User
You would use the same method, just embed it as an aggregation in the Table.Group function:
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"WEEK", Int64.Type}, {"PRODUCT ID", Int64.Type}, {"INITIAL", Int64.Type}, {"IN", Int64.Type}, {"OUT", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"PRODUCT ID"}, { {"New Initial", (t)=> Table.FromColumns( Table.ToColumns(t) & {List.Generate( ()=>[a=t[INITIAL]{0}, idx=0], each [idx] < Table.RowCount(t), each [a = [a] + t[IN]{[idx]} - t[OUT]{[idx]}, idx=[idx]+1], each [a])}, Table.ColumnNames(t) & {"NEW INITIAL"}), type table[WEEK=Int64.Type,PRODUCT ID=Int64.Type, INITIAL=Int64.Type, IN=Int64.Type, OUT=Int64.Type, NEW INITIAL=Int64.Type]}}), #"Expanded New Initial" = Table.ExpandTableColumn(#"Grouped Rows", "New Initial", {"WEEK", "INITIAL", "IN", "OUT", "NEW INITIAL"}) in #"Expanded New Initial"