
Hello Mansoor Ali,
Welcome to Microsoft Q&A.
I understand what you are trying to do, and I'm happy to help you calculate FIFO-based cost of goods sold in Excel using formulas only.
Let's say this is your Purchase Table:
For easier understanding, I will add a helper column for cumulative number of units till that batch (CumUnits). Put this in cell E2 and copy down:
=SUMIF($A$2:A2,A2,$B$2:B2)
This gives you a running total for each product. First you have 10 units, then every batch will get added: Your sales table is put in column F and G. Now, in cell H2, enter this to get your desired cost:
=SUMPRODUCT(--($A$2:$A$100 = F2),$B$2:$B$100-IF($E$2:$E$100>G2,$E$2:$E$100-G2,0),$C$2:$C$100)
Explanation:
-
--($A$2:$A$100 = F2)
checks which purchase rows match the product in F2 and converts TRUE/FALSE to 1/0. -
$B$2:$B$100 - IF($E$2:$E$100 > G2, $E$2:$E$100 - G2, 0)
calculates how many units from each batch are actually consumed, based on FIFO.- If CumUnits is less, no subtract - count all units till that point.
- When the cumulative total exceeds the sold quantity, subtract out the extra units.
- If CumUnits is less, no subtract - count all units till that point.
-
$C$2:$C$100
is the price per unit. -
SUMPRODUCT
multiplies consumed units by price and sums the result.
I hope this is what you are looking for. If you need any further explanation, feel free to ask.
Best regards,
Thomas
------------------------------------------------------------------------------------------------------------
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.