Forum Discussion
Reset next row based on compounded total
- 1 year ago
Done (I didn't create [grouping] column because it was not necessary for this purpose).
Hello dufoq3
In your v1 the returns are being compounded which is correct.
In your v2 they are being summed.
Although the results are similar in a small group of values I do need the returns to be compounded as in v1.
I would like the position (current row) to be conditional on BOTH the signal (current row) AND the group comp return (GCR) (previous row).
So, even though the signal is 1 in row 20, because the GCR (row 19) had reached -2.92% (i.e. <= -1%) the position in row 20 should reset to 0.
Return (row 20) would then be 0% (0.70%*0) , grouping (row 20) would calcuate to 0 and the GCR (row20) would thus reset to 0%.
The position would then stay at 0 until the next signal of 1 in row 24 and the returns then compounded until either a signal of -1 or GCR <= -1%.
I basically got stuck because the calculated return, grouping and GCR (row 19) are derived from the position (row 19) and I couldn't figure out how to use the GCR calculated in that previous row (19) in the calculation of the new position in the current row (20).
Done (I didn't create [grouping] column because it was not necessary for this purpose).
- AndyC1 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- AndyC1 year agoFrequent Visitor
Brilliant, thanks again.