Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello everyone!
I need to calculate Profit and Loss of stock trades (for each sell transaction) using FIFO.
I am using Mr Philip Seamark's DAX to calculate the P&L. However, 2 customizations are required in my case:
1. There are more than 1 trades for each stock in a day.
2. The raw data includes hundreds of clients' transactions, so I need to filter by clients.
To apply the DAX to my case, I used group by function to add index for each client and stock
Here is my sample Power BI file
Here is my sample excel output file with a. Correct/Wrong indicator, b. Correct P&L value, c. How the wrong value is calculated
Some P&L are correct but some are not.
Those with wrong P&L value is calculated by subtracting an extra value of NetAmountTrade (BUY) that had been used for previous FIFO P&L calculation. (For details, please refer to my excel file. There is excel formula to show how the wrong value is calculated.)
Wrong calculation is most likely be caused by FIFOFilterTable or FilteredFIFOTable below.
However, with my limited knowledge in DAX, I could not locate or rewrite the DAX to fix the issue I am facing.
Please have a look and give me advice on how to solve this 🙏
P&L (FIFO) =
VAR myClient = 'Order History'[Account Number]
VAR myUniqueOrder = 'Order History'[Index]
VAR myCurrentSell = 'Order History'[Cumulative Sell]
VAR myLastSell = 'Order History'[Previous Cumulative Sell]
VAR mySymbol = 'Order History'[Instrument]
VAR myCumulativeBuy = 'Order History'[Cumulative Buy]
VAR myLastCumulativeBuy = 'Order History'[Previous Cumulative Buy]
VAR FIFOFilterTable =
FILTER (
'Order History',
'Order History'[Account Number] = myClient
&& 'Order History'[Instrument] = mySymbol
&& 'Order History'[Index] < myUniqueOrder
&& 'Order History'[TransType]= "BUY"
&& ( ( 'Order History'[Cumulative Buy] >= myLastSell
&& 'Order History'[Cumulative Buy] < myCurrentSell )
|| 'Order History'[Cumulative Buy] >= myCurrentSell
&& 'Order History'[Previous Cumulative Buy] < myCurrentSell
|| 'Order History'[Previous Cumulative Buy] > myLastCumulativeBuy
&& 'Order History'[Cumulative Buy] < myLastCumulativeBuy )
)
VAR FilteredFIFOTable =
ADDCOLUMNS (
FIFOFilterTable,
"New Value", SWITCH (
TRUE (),
'Order History'[Cumulative Buy] > myLastSell
&& 'Order History'[Previous Cumulative Buy] < myLastSell, 'Order History'[Qty]
- ( myLastSell - 'Order History'[Previous Cumulative Buy] ),
'Order History'[Cumulative Buy] < myCurrentSell, 'Order History'[Qty],
-- ELSE --
'Order History'[Qty]
- ( 'Order History'[Cumulative Buy] - myCurrentSell )
)
)
VAR Result =
'Order History'[NetAmountTrade]
- SUMX ( FilteredFIFOTable, [New Value] *'Order History'[PricePerShare] )
RETURN
IF ( 'Order History'[TransType] = "SELL", Result )
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
37 | |
25 | |
19 | |
14 | |
8 |
User | Count |
---|---|
68 | |
48 | |
45 | |
18 | |
15 |