I wanted to calculate holding period of mutual fund, If the quantity is sold in any month then the program should automatically create a new column called Holding period and then calculate the difference of duration as shown in below table.
For example for mutual fund no :124, The current month quantity is 3000 and previous month was 4000 that means its sold in this month by 1000 unit.
But if the quantity is never sold like for mutual fund no :236 then it should give the duration from current month till first month.
| Mutual Fund | Quantity_201904 | Quantity_201903 | Quantity_201902 | Quantity_201901 | Holding Period_1 | Holding Period_2 |
| 124 | 3000 | 4000 | 1500 | 2000 | 1 | 2 |
| 236 | 4000 | 3000 | 2500 | 500 | 3 | 0 |
| 456 | 7000 | 4000 | 3500 | 800 | 3 | 0 |