Forum Discussion
Stock Portfolio Control ! Can i use the column that I am creating on the same calculation?
Thanks for reply Edhans!
I will try to give more detail about the calculations.
I put some RawData in a google sheets so that you can see the formulas that I did to get the results :
https://docs.google.com/spreadsheets/d/1PFA5f9yeq8D02Hz4R6HVeFnemuQgYn5zuEidyzcfujY/edit?usp=sharing
I am struggling to do this in Power Query because I don't know how to use the Previous result( OF THE SAME COLUMN) in the formula of this same Column. Is that Possible in Power Query?
You are right. I don want the median value. Or the average. It was a problem in translation. This number is called "PM" in Brazil, and it is used to calculate some taxes that you have to pay in certain operations in the Stock Market.
I think what I want is more similar to a weighted average of the cost ( with the rule that a sale should not affect the PM)
Let me give an example.
Day 1 - Buy 100 of Stock A . Price = 10. Quantity = 100 Cost = 10*100 =1000
Number of stocks = 100
PM ( what i am looking for) = Cost/number of stocks = 100
Day 2 - Buy 200 of Stock A. Price = 20 . Quantity = 200 Cost = 20 * 200 = 4000
Number of Stocks = 300
PM = (cost of buy day 1+ cost of buy day 2 ) / number of Stocks
(1000+4000) /300 =16,66666
Day 3 - Sale of 200 StockA ( the price of the sale does not matter).
Number of Stocks = 100
A Sale event does not change the PM!
I can just copy PM before the sale =16,666 or you can use the formula:
(
(Previous PM * Previous Number of Stocks ) +
( ( -Quantity of Sale) * Previous PM * )
) / number of Stock
= ((16,66*300 ) + (-200 *16,66) ) / 100
=16,666
I hope the example and the data in the link can clarify what I want.
Thanks for all the help!
Feel free to ask another question ( I am stuck with this problem for more than 15 hours...)
ps: The data has more stocks. They are classified by stock ( all the operations from one stock appear before the next stock). I Think just the raw data is enough so I can apply the solution.