Forum Discussion
AndyC
1 year agoFrequent Visitor
Reset next row based on compounded total
Hello, I've got most of the way with this but now I'm stuck. Throughout, I've been using List.Generate within power query and have linked a sample file below. Scenario - For each row of gains,...
- 1 year ago
Done (I didn't create [grouping] column because it was not necessary for this purpose).
dufoq3
1 year agoCommunity Champion
Done (I didn't create [grouping] column because it was not necessary for this purpose).
AndyC
1 year agoFrequent Visitor
Thanks very much dufoq3 !
Would you mind explaining the code for GeneratedPositionReturnGCR2 so I can fully understand what you've done?
- dufoq31 year agoCommunity Champion
Hi AndyC, you're welcome. I've added some comments:
Whole query with comments for GeneratedPositionReturnGCR2:
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Gain", Percentage.Type}, {"signal", Int64.Type}}), GeneratedPositionReturnGCR = [ tbl = Table.Buffer(ChangedType[[Gain], [signal]]), lg = List.Generate( ()=> [ x = 0, p = tbl{x}[signal], r = tbl{x}[Gain] * p, g = r ], each [x] < Table.RowCount(tbl), each [ x = [x]+1, p = if tbl{x}[signal] = -1 then 0 else if [p] = 2 then 2 else [p] + tbl{x}[signal], r = tbl{x}[Gain] * p, g = if p = 0 then 0 else ([g]+1) * (r+1) -1 ], each [ position = [p], return = [r], group comp return = [g] ] ), toTable = Table.FromRecords(lg, type table[position=Int64.Type, return=Percentage.Type, group comp return=Percentage.Type]) ][toTable], Combined = Table.FromColumns(Table.ToColumns(ChangedType) & Table.ToColumns(GeneratedPositionReturnGCR), Value.Type(ChangedType & GeneratedPositionReturnGCR)), GeneratedPositionReturnGCR2 = [ tbl = Table.Buffer(Table.SelectColumns(Combined, {"Gain", "signal", "position", "return", "group comp return"})), //Buffered selected columns lg = List.Generate( ()=> [ x = 0, s = tbl{x}[signal], g = tbl{x}[Gain], gcr = tbl{x}[group comp return] , p = tbl{0}[position], r = tbl{0}[return] ], //Generated 1st row (captured values from previous step 1st row) each [x] < Table.RowCount(tbl), //condition when should be generating stopped each [ x = [x]+1, //previous row number +1 s = tbl{x}[signal], //current row signal p = if [gcr] = 0 and s <> 1 then 0 else if [gcr] = 0 then [p]+1 else if [gcr] > -0.01 then tbl{x}[position] else 0, //if previous row gcr (group comp return) = 0 and current row signal <> 1 then 0 else if previous row gcr = 0 then previous row p (position) + 1 else if previous row gcr > -1% then current row p else 0 g = tbl{x}[Gain], //current row Gain r = g * p, //r (row) = current row g (Gain) * current row p (position) gcr = if p = 0 then 0 else ([gcr]+1) * (r+1) -1 ], //if current row p (position) = 0 then 0 else (previous row gcr + 1) * (current row return + 1) - 1 each [ gain = [g], signal = [s], position = [p], return = [r], group comp return = [gcr] ] //renamed parts of record s to signal, p to position etc... ), toTable = Table.FromRecords(lg, type table[/* gain=Percentage.Type, signal=Int16.Type, */ position=Int64.Type, return=Percentage.Type, group comp return=Percentage.Type]) //created table from records ][toTable], Combined2 = [ a = Table.RemoveColumns(Combined, Table.ColumnNames(GeneratedPositionReturnGCR2)), b = Table.FromColumns(Table.ToColumns(a) & Table.ToColumns(GeneratedPositionReturnGCR2), Value.Type(a & GeneratedPositionReturnGCR2)) ][b] in Combined2