jmarraztoa's avatar
jmarraztoa
Copper Contributor
Dec 20, 2025
Status:
New

FIFO.COST(array_qt_products_in;array_price_of_products_in;array_qt_products_out)

I've created a formula to get the FIFO cost of the last product that is going out of the inventory. 
It needs 3 arrays. The first array is the array that gets the quantity of the products going in the inventory, the second array is the array that that gets the cost of those products going in, and the third and last array is the quantity of the products going out.
This formula is very useful for most industries.
You are free to use it.

=LAMBDA(
    InQty,
    UnitCost,
    OutQty,
    LET(
        PrevOutQty,
            TAKE(OutQty, ROWS(OutQty)-1),

        LastOutQty,
            INDEX(OutQty, SEQUENCE(1,1,ROWS(OutQty))),

        OutIndex,
            ROWS(OutQty),

        RemainingFromPrevious,
            LET(
                InMatrix, InQty,
                OutMatrix, PrevOutQty,

                ApplyFIFO,
                    LAMBDA(InMatrix, OutMatrix,
                        LET(
                            n, ROWS(OutMatrix),

                            RecursiveFIFO,
                                LAMBDA(self, Remaining, i,
                                    IF(
                                        i > n,
                                        Remaining,
                                        self(
                                            self,
                                            LET(
                                                OutAmount, INDEX(OutMatrix, i),
                                                CurrentStock, Remaining,

                                                Consumed,
                                                    VSTACK(
                                                        OutAmount,
                                                        TAKE(
                                                            SCAN(
                                                                OutAmount,
                                                                CurrentStock,
                                                                LAMBDA(acc, qty,
                                                                    IF(qty > acc, 0, acc - qty)
                                                                )
                                                            ),
                                                            ROWS(CurrentStock)-1
                                                        )
                                                    ),

                                                IF(CurrentStock > Consumed, CurrentStock - Consumed, 0)
                                            ),
                                            i + 1
                                        )
                                    )
                                ),

                            RecursiveFIFO(RecursiveFIFO, InMatrix, 1)
                        )
                    ),

                ApplyFIFO(InMatrix, OutMatrix)
            ),

        FirstRemaining,
            SCAN(
                LastOutQty,
                IF(OutIndex = 1, InQty, RemainingFromPrevious),
                LAMBDA(stock, qty,
                    IF(qty >= stock, 0, stock - qty)
                )
            ),

        SecondRemaining,
            VSTACK(
                LastOutQty,
                TAKE(FirstRemaining, ROWS(FirstRemaining)-1)
            ),

        FIFOQuantities,
            MAP(
                IF(OutIndex = 1, InQty, RemainingFromPrevious),
                SecondRemaining,
                LAMBDA(stock, qty, MIN(stock, qty))
            ),

        FIFOUnitCost,
            SUMPRODUCT(FIFOQuantities, UnitCost) / LastOutQty,

        IF(ROWS(InQty) = 1, UnitCost, FIFOUnitCost)
    )
)

Have a nice day.
Juan Miguel Arraztoa

1 Comment

  • jmarraztoa's avatar
    jmarraztoa
    Copper Contributor

    This LAMBDA demonstrates that FIFO inventory costing can be implemented using native Excel functions, but the complexity and maintainability issues highlight the need for a built-in FIFO function.