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
spandiar
Frequent Visitor

Calculated Stock on Hand -at any given point of time

Hi, 

I want to show stock on hand qty at any point of time. 
I have 2 tables 
1.StockBal
2.StockTransaction

 

StockBal table stores  SOH of the item and for a site at the end of every month. So for a item and a for site it will 1 entry per month. StockBal - has Feb data only.


Stock Transaction table stores, daily transactions for a item and site. 


 In order for me to calculated the SOH for Jan. I subtract StockBal Qty with transacted qty to showing historical data. for March I add Stockbal Qty with Transacted qty to get SOH.

StkUnitQty is daily transcated qty


Measure to calculated Culmulated StkUnitQty

Cumulative StkUnitQty =
Var DMnth = ENDOFMONTH(StockTransaction[AccountingDate])
Var HStockdate =ENDOFMONTH(StockBal[StockDate])
Var CQTYafterSTBaldate =

CALCULATE(SUM(StockTransaction[StkUnitQty]),FILTER(StockTransaction
    ,StockTransaction[ItemRef]=EARLIER(StockTransaction[ItemRef])
            && StockTransaction[StorageSite]=EARLIER(StockTransaction[StorageSite])
                && StockTransaction[Unit]=EARLIER(StockTransaction[Unit])
                    &&StockTransaction[AccountingDate]>StockTransaction[DateFTStockBalance]
                        &&StockTransaction[AccountingDate]<=EARLIER(StockTransaction[AccountingDate])))
Var CQTYbeforeSTBaldate =

CALCULATE(SUM(StockTransaction[StkUnitQty]),FILTER(StockTransaction
    ,StockTransaction[ItemRef]=EARLIER(StockTransaction[ItemRef])
            && StockTransaction[StorageSite]=EARLIER(StockTransaction[StorageSite])
                && StockTransaction[Unit]=EARLIER(StockTransaction[Unit])
                    &&StockTransaction[AccountingDate]<=StockTransaction[DateFTStockBalance]
                        &&StockTransaction[AccountingDate]>=EARLIER(StockTransaction[AccountingDate])))

Return IF (StockTransaction[AccountingDate] >StockTransaction[DateFTStockBalance], CQTYafterSTBaldate,CQTYbeforeSTBaldate)



Measure to calculated SOH in Stock Transacted table is 
SOH=

If(StockTransaction[AccountingDate]<=StockTransaction[DateFTStockBalance],
StockTransaction[FTStockBalance]-StockTransaction[Cumulative StkUnitQty]
,IF(StockTransaction[AccountingDate]>StockTransaction[DateFTStockBalance],
StockTransaction[FTStockBalance]+StockTransaction[Cumulative StkUnitQty]))



e.g StockBal for item 14L is 

spandiar_0-1679329359943.png

and Stock Transaction for same is 

spandiar_1-1679329403474.png

When I add this measure then it adds the SOH qty,

spandiar_2-1679329501439.png

Requried result should be 

spandiar_3-1679329796902.png

It should show the next SOH qty rather then same adding the next qty in the previous qty. 

Measure to show final StockHand Qty
Stock on Hand =
CALCULATE(SUM(StockTransaction[SOH])
,FILTER(ALL(Dates),Dates[Date]<=MAX(Dates[Date])))

 




I am sharing the pbix file
https://drive.google.com/file/d/1Z4I3hQgNFCxaIIQJByjkz4S6uj5PmJn8/view?usp=sharing

Thanks.

1 REPLY 1
lbendlin
Super User
Super User

SOH is a caclulated column, not a measure.  it can be simplified:

SOH = If(StockTransaction[AccountingDate]<=StockTransaction[DateFTStockBalance], 
StockTransaction[FTStockBalance]-StockTransaction[Cumulative StkUnitQty]
,StockTransaction[FTStockBalance]+StockTransaction[Cumulative StkUnitQty])

 

Same for Cumulative StkUnitQty - it's a calculated column, not a measure.  And that's probably ok since these values are not influenced by user filters.

 

I have to admit I don't fully understand why you sometimes add and sometimes subtract.  Your calculated columns can be simplified - have a look at the documentation for CALCULATE(), and also consider replacing EARLIER with variables.

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.