Forum Discussion
Anonymous
2 years agoNot applicable
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 ...
- 2 years ago
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
- 2 years ago
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"
ronrsnfld
2 years agoSuper 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"
Anonymous
2 years agoNot applicable
It worked!!! Thank you so much I really started to think that power query did not have that capability but you proved me wrong.