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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors