Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply

FIFO Cost

Hi All Experts,

 

I am beginner to BI and tried many methods in order to get the below result and also google about this problem but i did not get the correct answer and have no ideas how to use the relevant formula to get this below result.  

 

Any suggestion of fomula to get the beblow result?

 

Thanks and Regards,

 

Sales Table

DateProduct NameQuantity
10/10/2022A10
10/12/2022A3
10/12/2022A2
10/13/2022A1
10/10/2022B22
10/11/2022B10
10/12/2022B3
10/12/2022B3

 

Purchase Table

DateProduct NameQuantityCost
9/12/2022A10110
9/30/2022A4130
10/1/2022A6150
9/1/2022B20210
9/2/2022B20250
9/20/2022B10230

 

Then i would like get the table like this;

 

DateProduct NameQuantity

Cost of sales

Closing Inventory Qty

Closing Inventory Value

10/10/2022A101100101420
10/12/2022A339071030
10/12/2022A22805750
10/13/2022A11504600
10/10/2022B22920286800
10/11/2022B102500184300
10/12/2022B3750153550
10/12/2022B3750122800
1 ACCEPTED SOLUTION
kirete17
Frequent Visitor

Power Query may not be the best solution, but I made it work

let
    Purchase = Excel.CurrentWorkbook(){[Name="Purchase"]}[Content],
    Sales = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    Custom1 = 
        Table.AddColumn(
            Table.Group( Sales, "Product Name", { "S", each _ } ),
            "P",
            (x)=> List.Select( Table.ToRows( Purchase ), (y)=> y{1} = x[Product Name] )
        ),
    Custom2 = 
        Table.ToList(
            Custom1,
            (t)=>
                let
                    f =
                        (p)=>
                        List.Accumulate(
                            t{2},
                            { 0, 0 },
                            (x,y)=>
                                if x{1} + y{2} < p
                                then { x{0} + y{2} * y{3}, x{1} + y{2} }
                                else { x{0} + List.Max( { 0, p - x{1} } ) * y{3}, x{1} + y{2} }
                        ){0},
                    a =
                        Table.AddIndexColumn( t{1}, "i", 1, 1 ),
                    b =
                        Table.AddColumn( a, "q", each f( List.Sum( Table.FirstN( a, [i] )[Quantity] ) ) ),
                    c =
                        Table.AddColumn( b, "amount", each if [i] = 1 then [q] else [q] - b[q]{[i]-2} ),
                    l =
                        List.Transform( List.Zip( List.Transform( t{2}, (x)=> { x{2}, x{2} * x{3} } ) ), List.Sum ),
                    d =
                        Table.AddColumn( c, "Qty", each l{0} - List.Sum( Table.FirstN( c, [i] )[Quantity] ) ),
                    e =
                        Table.AddColumn( d, "Value", each l{1} - List.Sum( Table.FirstN( c, [i] )[amount] ) )
                in
                    Table.RemoveColumns( e, { "i", "q" } )
        ),
    Custom3 = 
        Table.Combine( Custom2 )
in
    Custom3

 

View solution in original post

2 REPLIES 2
kirete17
Frequent Visitor

Power Query may not be the best solution, but I made it work

let
    Purchase = Excel.CurrentWorkbook(){[Name="Purchase"]}[Content],
    Sales = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    Custom1 = 
        Table.AddColumn(
            Table.Group( Sales, "Product Name", { "S", each _ } ),
            "P",
            (x)=> List.Select( Table.ToRows( Purchase ), (y)=> y{1} = x[Product Name] )
        ),
    Custom2 = 
        Table.ToList(
            Custom1,
            (t)=>
                let
                    f =
                        (p)=>
                        List.Accumulate(
                            t{2},
                            { 0, 0 },
                            (x,y)=>
                                if x{1} + y{2} < p
                                then { x{0} + y{2} * y{3}, x{1} + y{2} }
                                else { x{0} + List.Max( { 0, p - x{1} } ) * y{3}, x{1} + y{2} }
                        ){0},
                    a =
                        Table.AddIndexColumn( t{1}, "i", 1, 1 ),
                    b =
                        Table.AddColumn( a, "q", each f( List.Sum( Table.FirstN( a, [i] )[Quantity] ) ) ),
                    c =
                        Table.AddColumn( b, "amount", each if [i] = 1 then [q] else [q] - b[q]{[i]-2} ),
                    l =
                        List.Transform( List.Zip( List.Transform( t{2}, (x)=> { x{2}, x{2} * x{3} } ) ), List.Sum ),
                    d =
                        Table.AddColumn( c, "Qty", each l{0} - List.Sum( Table.FirstN( c, [i] )[Quantity] ) ),
                    e =
                        Table.AddColumn( d, "Value", each l{1} - List.Sum( Table.FirstN( c, [i] )[amount] ) )
                in
                    Table.RemoveColumns( e, { "i", "q" } )
        ),
    Custom3 = 
        Table.Combine( Custom2 )
in
    Custom3

 

Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @PhyuLayKhine333 - it appears you have asked the same question four different forums.  Do you still need a response?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.