I want to insert a formula in the pivot table. I tried to include a 'calculated field', but couldn't get the correct result when I have to calculate based on 2nd level. I think the example makes it clear.
It is an example from stock trading book. I have to calculate what quantity of stock is left after buy / sell actions. Also, I have to calculate if there is profit or loss on overall stock. I have added quantity & value for each stock for buy & sell actions in a pivot table. What I want to achieve is shown in separate columns (final qty, investment / gain) outside the pivot table. These columns show the difference between quantity & price for sell & buy. I want these columns to be part of the pivot table.
When I include calculated field in pivot table, it just subtracts without considering buy / sell. It doesn't give the required result. I'm sure there must be a way to do this & I'm making some silly mistake Can someone help me to fix this please?
Base data -
Pivot table -
It is an example from stock trading book. I have to calculate what quantity of stock is left after buy / sell actions. Also, I have to calculate if there is profit or loss on overall stock. I have added quantity & value for each stock for buy & sell actions in a pivot table. What I want to achieve is shown in separate columns (final qty, investment / gain) outside the pivot table. These columns show the difference between quantity & price for sell & buy. I want these columns to be part of the pivot table.
When I include calculated field in pivot table, it just subtracts without considering buy / sell. It doesn't give the required result. I'm sure there must be a way to do this & I'm making some silly mistake Can someone help me to fix this please?
Base data -
pivot.xlsx | |||||||||
---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | |||
2 | Sl.# | Date | Stock name | Action | Qty | Price | Value | ||
3 | 1 | 19-Jan-22 | xxx | Buy | 500 | 61.75 | 30,875.00 | ||
4 | 2 | 24-Jan-22 | yyy | Buy | 200 | 97.00 | 19,400.00 | ||
5 | 3 | 25-Jan-22 | xxx | Sell | 500 | 65.00 | 32,500.00 | ||
6 | 4 | 27-Jan-22 | yyy | Sell | 200 | 96.50 | 19,300.00 | ||
7 | 5 | 27-Jan-22 | zzz | Buy | 100 | 77.00 | 7,700.00 | ||
trade book |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H3:H7 | H3 | =F3*G3 |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
E3:E7 | List | Buy,Sell |
Pivot table -
pivot.xlsx | |||||||||
---|---|---|---|---|---|---|---|---|---|
K | L | M | N | O | P | Q | |||
4 | Column Labels | ||||||||
5 | Buy | Sell | |||||||
6 | Row Labels | Sum of Qty | Sum of Value | Sum of Qty | Sum of Value | Final qty | Investment / gain | ||
7 | xxx | 500 | 30875 | 500 | 32500 | 0 | 1625 | ||
8 | yyy | 200 | 19400 | 200 | 19300 | 0 | -100 | ||
9 | zzz | 100 | 7700 | 100 | -7700 | ||||
10 | Grand Total | 800 | 57975 | 700 | 51800 | -6175 | |||
trade book |
Cell Formulas | ||
---|---|---|
Range | Formula | |
P7:P9 | P7 | =L7-N7 |
Q7:Q9 | Q7 | =O7-M7 |
Q10 | Q10 | =SUM(Q7:Q9) |