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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
George1973
Helper V
Helper V

Calculate Stock Day to Day Quantitative Balance

Hi All,

I have a following sample table:

 

George1973_0-1677666241964.png

 

Where I have to calculate weighted cost per item based on the purchased qn_ty and purcahsed orders amounts. 

 

I need to create to table in Power BI, where as a result I would have Yellow column (Weighted cost) and Gray Column (End Qnty). All other data are available.

Here is the dropbox link to the excel file:

https://www.dropbox.com/s/5av0eqpe3geyn3x/COGS_Calculae%20Template.xlsx?dl=0

 

Here is the data:

Oper DateProd CodeQnty Tot ReceivedReceived PriceCost Tot ReceivedWeighted Cost PCEQnty SoldCOGSEnd Qnty
01-01-230011010100         10.003       30.007
02-01-2300179.969.3           9.952       19.9012
03-01-2300159.849           9.911         9.9116
04-01-2300161060           9.935       49.6617

 

I'm sure that I should use ERLIER function, but HOW I faill to realize 😞

Thanks in advance for your help.

3 REPLIES 3
andhiii079845
Solution Sage
Solution Sage

I hope this are the underlaying data?
Weighted cost = SELECTEDVALUE(table[Cost])-SELECTEDVALUE(table[Cost])
The same way for the second measure.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @andhiii079845 ,

Thanks for your respond.

I think there is a misunderstanding here and it is totaly my fault if it is so.

 

I beleive the result lays in much more complex solution, then you've suggested. If use the provided link and look at the excel template, you will understand , what I mean.

The "End Quantity" and then even more complex "Weghted Cost" have to be requiring from the previous row content.

- End Quantity should be calculated as "Running Total" of Qnty-Received minus Running Total of "Qnty-Sold"

I worked hard on it yesterday and got the desired result:

George1973_0-1677755910591.png

Where 1st and 2nd collumns - CLMN_Purcahsed Qnty and CLMN_Total_Usage are Total Running sum from Qnty Received and Qnty Sold (Out) respectivly and finaly I got CLMN_Endig Stock as the desired result. Here is the code:

 

CLMN_ENDING_STOCK = 
VAR ActualCode=TS_COGSS[GOODS_CODE]
VAR InTot=CALCULATE(sum(TS_COGSS[CLMN_Purchased_Qnty]),FILTER(TS_COGSS,TS_COGSS[Action_Date]<=EARLIER(TS_COGSS[Action_Date]) && TS_COGSS[GOODS_CODE]=ActualCode))
VAR OutTot=CALCULATE(sum(TS_COGSS[CLMN_Total_Usage]),FILTER(TS_COGSS,TS_COGSS[Action_Date]<=EARLIER(TS_COGSS[Action_Date]) && TS_COGSS[GOODS_CODE]=ActualCode))
Return
InTot - OutTot

 

So, fist task is over. But now I'm facing the main problem: How to calculate "Weighted Cost" and I fail to do it?

Weighted  Cost - If we have the first day, the result is very simple - Just [CLMN_Purcahsed_Amount] / [CLMN_Purchased_Qnty], but when we come to the next day and after, then:
( [CLMN_PrevDay_Stock]*[CLMN_PrevDay_WeightedCost] +[CLMN_Purchased_Amount] )
/
([CLMN_Purchased_Qnty + CLMN_PrevDay_Stock])

This is the logic and formula. Unfortunately, I'm missing only one component (The Red Highlighted Number).. And I can not understand how can I get it 🙂

Hope, this explanation will help to understand the issue more exactly.

Regards,

George







 




 

This was my short question: This are the underlaying data, or? 😉

Please upload some new example. I do not want to copy it from a screenshot.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.