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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! 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
Community Champion
Community Champion

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

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.