Weighted Average Cost of Goods Sold

Hello Everyone,

Please does anyone have a sample of how to calculate the Weighted Average Cost of Goods Sold where there are

Open Inventory
Inventory purchased monthly
Monthly sales for each product

Summarily, all I want is a Dax formula for a weighted average cost of goods that is calculated at the end of every month for the company’s monthly closing.

I should be able to follow the approach to solve my problem

Please kindly share. I don’t think my data will be elf explanatory if I share.

Thanks

Hi @aedniranao

Please receive a workout based upon a more or less FIFO (First In, First Out) approach, where inventory is used first and purchases secondly for the COGS calculation, using average price per product of the inventory and the purchases during the month.
It is not a exact FIFO COGS calculation, whereby each individual price from stock and purchases
are taken into account to calculate an exact FIFO COGS.

Attached the PBIX file and the Excel input data file
Avg COGS FIFO approach.pbix (73.2 KB)
Input tables Jan Feb data.xlsx (17.7 KB)

Explanation of the workout:

  1. Data is uploaded per month: inventory, purchases and sales (for the example data, see attached Excel)

  2. COGS is calculated

  • Calculate an average price per product of the inventory and the purchases during the month.

  • Determine which quantity of the sales can be taken from the beginning month inventory, and which remaining quantity has to be taken from current month purchases for the sales during that month

  • Calculate a weighted average COGS, based upon qty x avg. price of available stock + qty x avg. price of required extra purchases ) / sales qty of the month

  • See the combined COGS-DAX formula below:

    COGS (FIFO approach) = 
    
    Var AvgStockPrice =  DIVIDE( SUM(Stock[Stock value]), SUM(Stock[Qty stock]), 0) 
    Var PurchaseValue = SUMX( Purchases, [Qty purchased] * [Purchase price])
    Var AvgPurchasePrice = DIVIDE( PurchaseValue, SUM(Purchases[Qty purchased]),0)
    
    Var QtyUsedfromStock = SWITCH( TRUE(),  //FIFO, first use inventory for sales qty 
                                SUM(Stock[Qty stock]) = 0, 0, // no inventory
                                SUM(Sales[Qty sold]) < SUM(Stock[Qty stock]),  SUM(Sales[Qty sold]),  // sales < stock
                                SUM(Sales[Qty sold]) >= SUM(Stock[Qty stock]), SUM(Stock[Qty stock])) // sales > stock
    Var QtyUsedfromPurchases = SWITCH( TRUE(),  //FIFO, first use inventory for sales qty, then use purchases of the month 
                                SUM(Purchases[Qty purchased]) = 0, 0, //no purchases
                                SUM(Sales[Qty sold]) < SUM(Stock[Qty stock]), 0, //all sales taken from stock, so no purchases needed 
                                SUM(Sales[Qty sold]) >= SUM(Stock[Qty stock]), SUM(Sales[Qty sold]) - SUM(Stock[Qty stock])) 
    return 
    // weighted avarage COGS
    DIVIDE( QtyUsedfromStock * AvgStockPrice + QtyUsedfromPurchases * AvgPurchasePrice, SUM(Sales[Qty sold]), 0) // weighted avarage COGS
    
  • All measures can be found at “Measures DS”

  1. A possible report looks as follows:

  • Note1: please select a Month in the reports for proper monthly COGS calculation, not individual dates nor all data (context has to be Month, otherwise for example double counting of inventory by monthly data input renewal )

  • Note2: if no inventory exist and no purchases are made in certain month for a product, COGS is not calculated (blank).

Please give me your thoughts, if this would work for you,

Kind regards, Jan van der Wind

2 Likes

@deltaselect

first and foremost I want to thank you again for responding to my crucial request. I have searched the internet for something related to COGS calculation on power Bi and I have not found anything. Sharing this is of great help to me.

I will go through what you shared in the morning and I will give you detailed feedback. My company currently uses weighted average but I am sure I would learn a lot from here that would probably help solve the weighted average calculation.
Thanks so much for always supporting me.

Regards

1 Like

@deltaselect

Thank you once again. Your approach seems to be more focused on FIFO than the weighted average, but I got some insight and wrote the following Dax code.

I am not sure how correct it is. It would be great if Enterprise DNA could treat this topic and offer a solution to the three inventory costing methods.

I could not share the data because it is on a different system, but if would appreciate it if you could glance through the line of codes here and comment if, in your opinion, it seems close to Weighted Average

The result I got is almost the same ask what my company got in the previous month, but I still don’t trust the outcome and feel there might be something wrong somewhere.

Thanks

Inventory Value =

SUMX(‘All Brands Invoices’, ‘All Brands Invoices’[Inventory Value])

Quantites Supplied =

SUMX(‘All Brands Invoices’, ‘All Brands Invoices’[Total Qty])

Inventory running total in Date =
CALCULATE(
[Inventory Value],
FILTER(
ALLSELECTED(‘Date’[Date]),
ISONORAFTER(‘Date’[Date], MAX(‘Date’[Date]), DESC)
))

COGS =

Var CostperUnit = DIVIDE(([Inventory running total in Date]), [Quantites Supplied] )

Return

CostperUnit

Rolling 12 COGS = CALCULATE([COGS], DATESINPERIOD(‘Date’[Date],MAX(‘Date’[Date]),-12,MONTH))

Hi @aedniranao

Which three inventory costing methods do you have in mind ?
When searching at some explanations of Weighted Average COGS, there are multiple definitions : Periodic, Perpetual, also FIFO Perpetual and FIFO periodic exist.
My method presented was FIFO periodic average, not described
(some methods are explained in appendix I ).

Some comments on your DAX-code:

  1. It is hard, (not to say impossible) to comment on DAX code without knowing the (exact) context and the data-structure set up (e.g. do you have one input file or more, how are inventory value mutations affected by sales and purchases as you want inventory calculated on a WA-COGS method, etc.)
  2. Generally speaking, SUM is sufficient for a single column reference, you do not need SUMX, so Sum( All Brands Invoices [Total Qty]) would be sufficient.
  3. It is unknown how the "All Brands Invoices [Inventory Value] is calculated, with which values reduces the sales quantity the inventory value, see also point 1
  4. Comment on Rolling 12 COGS, see also point 1, formula seems not wrong.

Your question:

Summarily, all I want is a Dax formula for a weighted average cost of goods that is calculated at the end of every month for the company’s monthly closing.

To further help you with your question, a sample PBIX of your data is needed and a clear definition what method of WA COGS is used.

=====
A second workout below, now with the Periodic COGS method, hopefully helps you further, but be aware, the calculations are based upon 3 separate data input fact-files ( inventory, purchases and sales), which obviously affects the required DAX formulas to reach the desired output.

Please find attached an example of the Periodic COGS method worked out in Power BI, which is the easiest methodology.
Weigted Avg COGS (period).pbix (173.9 KB)

Hereby is COGS per unit from a period calculated as (total value inventory + total purchase value in the period) / (quantity inventory + quantity purchased in the period)

WA COGS (period) per unit = 
Var PurchaseValue = SUMX( Purchases, [Qty purchased] * [Purchase price])
return 
// Weighted Average COGS (period) per unit
DIVIDE( SUM(Stock[Begin Stock value]) + PurchaseValue,  SUM(Stock[Begin Qty stock]) + sum(Purchases[Qty purchased]), 0) 

See measure group “Measures Period WA COGS period”

An example report can be as follows :

Appendix I:

==> Period and Perpetual COGS explained

==>FIFO period COGS explained

Appendix II
P.S . For a better readability of the DAX code copied in the forum, you can use:

– copy in the code,
– select all code
—press then </>
—press thereafter " " , see also below picture and example of a much better readable result .

image

Quantites Supplied =
SUMX(‘All Brands Invoices’, ‘All Brands Invoices’[Total Qty])
1 Like

Thank you so much for a very detailed answer @deltaselect! We appreciate you sharing your knowledge and expertise in the forum everytime. :slight_smile:

Hello @aedniranao, it’s been a while since we got a response from you.

Just following up if the answer above was able to solve your inquiry?

@deltaselect

Thank you so much for your time. Sorry I could not share my output because the data is on my company PC and I only write from my Personal PC.

Sometimes I think too far while the solution is just right at my door step

The approach explained above was what I applied, just a little tweak based on my dataset and also used some variables to separate different calculation

This solution absolutely explains it and I will also add mine as soon as I’m able to process the data for public consumption.

Thanks so much for taking your time out to explain this in detail. You are cherished.

Regards
Adediran

@deltaselect

This actually solves FIFO and I will always reference it to it to solve a problem on FIFO.

Thanks so much

Regards
Adediran