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
MQudmani
Regular Visitor

FIFO Cost Valuation for The sales transactions

Hi

 

I am looking for a way to calculate the cost per sales transaction using FIFO method in Power Query, I tried a lot of formulas but none is working.

In my case, I have multiple items and multiple purchasing costs. I attached a sample of the data so it can help better understand the case.

 

Fifo.PNG

4 REPLIES 4
kirete17
Frequent Visitor

FYI

let
    Source = Excel.CurrentWorkbook(){[Name="Table15"]}[Content],
    #"Change Type" = Table.TransformColumnTypes(Source,{{"Total Value", type number}, {"value per unit", type number}}),
    Custom1 = 
        Table.Group(
            #"Change Type",
            "Symbol",
            {
                "q",
                each
                    let
                        t = Table.Group( _, "type", { "w", (x)=> x } ),
                        buy = t{0}[w],
                        sale = t{1}[w],
                        f = 
                            (p)=>
                            List.Accumulate(
                                Table.ToRows( buy ),
                                { 0, 0 },
                                (x,y)=>
                                    if x{1} + y{1} < p
                                    then { x{0} + y{1} * y{4}, x{1} + y{1} }
                                    else { x{0} + List.Max( { p - x{1}, 0 } ) * y{4}, x{1} + y{1} }
                            ){0},
                        a =
                            Table.AddIndexColumn( sale, "i", 1, 1 ),
                        b =
                            Table.AddColumn( a, "q", (x)=> f( List.Sum( Table.FirstN( a, x[i] )[Units] ) ) ),
                        c =
                            Table.AddColumn( b, "cost", (x)=> if x[i] = 1 then x[q] else x[q] - b[q]{x[i]-2} ),
                        d =
                            Table.AddColumn( buy, "cost", (x)=> null ) & Table.RemoveColumns( c, { "i", "q" } )
                    in
                        Table.Sort( d, "index" )
            }
        )[q],
   Custom2 = 
        Table.Combine( Custom1 )
in
    Custom2
MQudmani
Regular Visitor

Hi @ERD 

 

This is the data sample with the expected value as a cost of goods sold column, the calculation for this column was as follows :

for the symbol "100", I first bought 2 pcs for 43.65 each and then we sell 1 pcs so the cost of this 1 pcs is 43.65 after that we bought 1 pcs for 84 then we sell 1 pcs and the cost of this pcs should be 43.65 ( we bought 2 and then sell 1 so we have 1 left), after that, we sell 1 pcs and the cost of this pcs should be 84 ( we finish the first batch of the products that we bought first for 43.65 per item and then we move to the second batch that cost 84 per item),

this case should be repeated for the second item also.

 

SymbolUnitstypeTotal Valuevalue per unitindex Cost of goods sold 
1002Buy                 87.30                       43.651 
1001Sale                 84.02                       84.022                           43.65
1001Buy                 84.00                       84.003 
1001Sale                 78.53                       78.534                           43.65
1001Sale                 92.94                       92.945                           84.00
152516Buy               368.32                       23.021 
1525100Buy           2,302.00                       23.022 
15253Buy                 69.06                       23.023 
15253Buy                 69.06                       23.024 
152512Sale               363.49                       30.295                        276.24
15251Sale                 40.99                       40.996                           23.02
15254Sale               119.89                       29.977                           92.08
15251Sale                 42.50                       42.508                           23.02
15253Sale               105.00                       35.009                           69.06
15251Sale                 40.00                       40.0010                           23.02
152514Buy               315.64                       22.5511 
15253Sale               120.00                       40.0012                           69.06
15253Sale               150.00                       50.0013                           69.06
15253Sale               117.00                       39.0014                           69.06
15251Sale                 45.34                       45.3415                           23.02
15251Sale                 41.45                       41.4516                           23.02
152510Sale               299.72                       29.9717                        230.20
15252Sale                 79.91                       39.9618                           46.04
15251Sale                 39.56                       39.5619                           23.02
15251Sale                 38.11                       38.1120                           23.02
15251Sale                 35.00                       35.0021                           23.02
15253Sale                 83.16                       27.7222                           69.06
152545Sale           1,125.00                       25.0023                     1,035.90
15251Sale                 35.00                       35.0024                           23.02
15252Sale                 80.09                       40.0425                           46.04
15252Sale                 80.00                       40.0026                           46.04
15252Buy                 80.00                       40.0027 
15252Sale               100.00                       50.0028                           46.04
15252Sale                 79.99                       39.9929                           46.04
15251Sale                 39.69                       39.6930                           23.02
15252Sale                 79.92                       39.9631                           46.04
15251Sale                 39.99                       39.9932                           23.02
15252Buy                 80.00                       40.0033 
15254Sale               164.02                       41.0134                           92.08
15252Sale                 75.01                       37.5135                           46.04
152510Buy               202.43                       20.2436 
15259Sale               353.96                       39.3337                        206.23
15251Sale                 40.52                       40.5238                           22.55
15253Sale                 75.00                       25.0039                           67.64
15255Sale               138.60                       27.7240                        147.64

 



@MQudmani ,

I'm not sure how to get this in Power Query, but here are some links that might be useful:

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

ERD
Super User
Super User

Hi @MQudmani ,

As a general advice, please, provide:

1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1 to 2.

If this post helps, then please consider Accept it as the solution ✔️to help the other members find it more quickly.

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

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
Top Kudoed Authors