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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
George1973
Helper V
Helper V

Partially Weighted and FIFO method for inventory cost calculation

Hi All,
I'm stucked with the following DAX task:
I have a table and measures calculating the quantitative stock at the end of the desired period (please see the pic. below):

George1973_0-1599986968812.png

Everithing works fine except the Stock Cost for the end of the period (Yellow Column).
If it were a training or a simple dataset I would say that it is OK - Because the current measure calculates the stock values with the following approach:

COST OF GOODS = DIVIDE([TOTAL CUMM COST GOODS RECEIVED],[TOTAL GOODS CUMM RECEIVED],0)

and then:

Cost of End Stock for Period = [COST OF GOODS]*[Stock End for Period] (This is actualy what is written in the yellow column)
..and it works fine.. Meaning The Cost for the period end should be equal to the value of TOTAL COSTS of Good Received divided by TOTAL QNTY of Good Received - Theoreticaly!

But in reality we have another picture:
Imagine we have the following case (I have layed in out in excel for more clearity):
 
 

Excel.JPG

When we come to the date5 (highlighted in green) to sell 150 untints of prod1, there should be calculation considering not only the total weighted average of the total IN OUT, but also partially also FIFO method like given below:
Excel_1.JPG

 

Meaning to calculate the total cost of the remaining batches and summirize it with the upcomming new batch by weighted average. Ultimatly wich will be the running cost of the 150 selling units on the date5.
In excel it's very easy to do this calculation and table - just pointing the running cost of the selling units a row before the transaction, but I can not do it in DAX and Power BI accordingly 😞

Any suggestions and comments will be very mych appriciated.
Thanks in advance,

BTW: The date filter for the accumulated sums looks as follows:

FILTER(ALL(DateKey),DateKey[Date]=MAX(TS_OPERATIONS[OPERDATE]))

 

8 REPLIES 8
AllisonKennedy
Super User
Super User

One way to reference a previous row in DAX Is to create an INDEX column, then use the EARLIER function to reference the current row INDEX value -1 using FILTER(table, EARLIER(table[index])+1 = table[index])

I don't fully understand what the problem is though, why just the green highlighted and not all goods sold? What rows/columns are you in when calculating E21/C21?

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Hi,
I'm back again and sorry for the late answer.
I'm providing the table showing the both results - The current one (Red Numbers) which works in DAX and I'm not fully satisfied and the disired one (Blue Numbers) - How the DAX should work:

 

The current formula works in this way: [Total Purchased Costs] / [Total Porchaed Quantity] = 8.15 (Emphsizies all the date periods from Date1 to Date10) - That's why all the COGS (Cost of Goods Sold have the same costs - 8.15).


If you notice - When if the quantitative stock goes to zero, the both variants end up with the zero volume balance as well. But what is important to me - Are the intermediate COGS and Stock Balances within the Dateperiods:

DateProductBuy_QntyBuy_CostBuy_AmountSell_Qnty1.Sell_Amount1.COGS2.Sell_Amount2.COGSStock_Qnty`.Stock_Unt_Cost1.Stock_Amount2.Stock_Unt_Cost2.Stock_Amount
Date1Prod1100        10.00       1,000.00              8.15100     10.000      1,000.00       10.000     1,000.00
Date2Prod150        12.00           600.00                     -          10.00           8.15150     10.667      1,600.00        10.667     1,600.00
Date3Prod1                      -  30           320.00        10.67         244.62          8.15120     10.667      1,280.00     11.295     1,355.38
Date4Prod1200        11.00       2,200.00                     -          10.67                  -            8.15320     10.875      3,480.00     11.111     3,555.38
Date5Prod1                      -  150       1,631.25        10.88     1,223.08          8.15170     10.875      1,848.75     13.719     2,332.31
Date6Prod1300          5.00       1,500.00                     -          10.88                  -            8.15470        7.125      3,348.75        8.154     3,832.31
Date7Prod1                      -  400       2,850.00          7.13     3,261.54          8.1570        7.125         498.75        8.154         570.77
Date8Prod1                      -  70           498.75          7.13         570.77          8.150               -                     -                 -                    -  
Date9Prod1                      -                       -                 -                    -            8.150               -                     -                 -                    -  
Date10Prod1                      -                       -                 -                    -            8.150               -                     -                 -    -  

@George1973
sorry, i haven't had time to come back to this one in depth, but you could have a go using the EARLIER function (or variables if you prefer) to constrain the calculation to look at current row's date period only when doing the COGS calculation.

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@AllisonKennedy Thanks a lot for the comment.

I will try to apply the EARLIER finction as you've suggested and report back about the results

@AllisonKennedy 
Hi,

I tried EARLIER as suggested, but did not work as "measure". As for the calculated column I do not want to do it, because there are millions of records in that data set one more calculated column will make it heavier.

I’m thinking about another solution. Not done yet 😞

@George1973

You should be able to get similar results using variables in a Measure as what the EARLIER function will do with calculated column.

You just need to be careful when you define the VAR so that it has the necessary row context that you are looking for.

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@George1973 , red columns are the one need to calculate? Costing needs to be done based on the stock rate assuming FIFO?

 

In case you have not referred it

https://radacad.com/dax-inventory-or-stock-valuation-using-fifo

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Actually the red cells are the EXISTING numbers which should be replaced by the DESIRED BLUE cell numbers.

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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