Supplies are limited. Contact info@espc.tech right away to save your spot before the conference sells out.
Get your discountScore big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount
Hi everyone,
I have a list of operations of buying/selling shares of different financial products and, for each of them, I am trying to compute the average buying cost of the total position at time of transaction using Power Query. Due to how the value is computed, a formula referencing the previous cell of the new column being created is necessary.
The table is formatted as the one below, even though grouping by "Product" may be a good start. I am trying to replicate column F in PowerQuery, if it is even possible.
Thank you
You can try this GUI process (thinking through in my head might not be 100% right):
Hello, @Faroacces0
let
Source = your_table,
f = (tbl as table) =>
[rows = Table.ToRecords(tbl),
gen = List.Generate(
() => [i = 0, r = rows{0}, nc = r & [new_column = r[Total Cost] / r[Shares]]],
(x) => rows{x[i]}? <> null,
(x) =>
[i = x[i] + 1,
r = rows{i},
nc = r & [new_column =
if r[Total Cost] > 0
then (r[Total Cost] + x[r][#"RT-Shares"] * x[nc][new_column]) / r[#"RT-Shares"]
else x[nc][new_column]]],
(x) => x[nc]
)][gen],
g = Table.Group(Source, {"Product"}, {{"new", each f(Table.Sort(_, "date"))}}),
z = Table.FromRecords(List.Combine(g[new]))
in
z