I have a unique issue that I would like to solve and would definitely appreciate any help. I have a sample data such as below:
Avg Cost Calculation 1
Avg Cost Calculation 2
Here I have 2 examples of calculation average cost, however I need to achieve Average Cost Calculation 2.
In this example, Total Cost is Unit_Cost_Price * Quantity + Fee (which is the right most column)
The easy way of calculation the average cost would be to sum up the total cost and divide it by the sum of quantity (which is calculation 1)
However I need to achieve the calculation using FIFO, where the first 2 items are ignored (the items are sorted by date from oldest to newest) as the quantity cancels each other, and the average cost is calculated after.
Would definitely appreciate any suggestions on achieving this in powerbi
@Greg_Deckler I am not looking based on the max date. Lets assume that there are 2 more after the third entry, and the quantity are all positive. The goal is to then calculate the average based on these 3 entrys (1 current + 2 new entries)
@draj - That certainly didn't come across in the original post and I am still not sure I am clear on the requirement. So, are you saying that you want any entries after the last negative entry? Still seems like Lookup Min/Max to me if that is the case. Lookup the MAXX date of all negative values. Use that date in your filter to filter for rows after that date. Then you can do the appropriate SUMX, etc. on those FILTER'ed rows.
@Greg_Deckler Thanks for your feedback. Let me go in more detail.
I will introduce a column call running QTY, which would make more sense as it explains the concept of FIFO
Lets take a look at this 3 entries, which have been sorted by date (old to new). We have a running quantity column. At this point of time, the correct way of calculating average cost would be to ignore the first 2 items (as the quantity cancels each other), and just consider the last entry.
Now lets looks at a scenario where another entry is added, increasing the quantity:
SO here we have item 4, and the running quantity is now 2,000,000. The average cost is now calculated as (Total Cost of 3 and 4)/(Last Running Quantity).
You can see that it is more dynamic than the usual average calculation (SUM of Total Cost / SUM of Quantity)
@draj - Then why not just use the Running Total Quick Measure that is built into Power BI to create that column? And probably another running total column for Total Cost. Then it truly is a Lookup Min/Max situation as your last date will have the totals you want.