Hi All,
I have been tasked with creating a stock on hand report by my supervisor.
The dataset im working with contains logistics transactions, with the following columns.
- Date
- Product
- Warehouse
- Quantity
- MovementTypeDesc
All movement types except “StockCount” can be summarized cumulatively to get the stock on hand total at a given date.
The “StockCount” type on the other hand is the actual result of the stock count, so to include this into the cumulative sum, i need to “reset” the count
As far as i can see, i have successfully done this in the attached model, and it works fine on the dummydataset, but its very slow when working with the actual dataset.
Quantity Cumulative =
VAR CurrentDate = MAX(DIM_Dates[Date])
VAR CurrentWarehouse = MAX(FACT_StockMovement[Warehouse])
VAR CurrentProduct = MAX(FACT_StockMovement[Product2])
VAR LastStockCountDate =
CALCULATE (
MAX ( FACT_StockMovement[Date] ),
FILTER (
ALL ( FACT_StockMovement ),
FACT_StockMovement[Warehouse] = CurrentWarehouse &&
FACT_StockMovement[Product2] = CurrentProduct &&
FACT_StockMovement[Date] < CurrentDate &&
FACT_StockMovement[MovementTypeDesc] = "StockCount"
)
)
VAR CumulativeQuantityBeforeStockCount =
CALCULATE (
SUM ( FACT_StockMovement[Quantity] ),
FILTER (
ALL ( FACT_StockMovement ),
FACT_StockMovement[Warehouse] = CurrentWarehouse &&
FACT_StockMovement[Product2] = CurrentProduct &&
FACT_StockMovement[Date] <= CurrentDate &&
FACT_StockMovement[Date] > LastStockCountDate
)
)
VAR LastStockCountQuantity =
CALCULATE (
MAX ( FACT_StockMovement[Quantity] ),
FILTER (
ALL ( FACT_StockMovement ),
FACT_StockMovement[Warehouse] = CurrentWarehouse &&
FACT_StockMovement[Product2] = CurrentProduct &&
FACT_StockMovement[Date] = LastStockCountDate &&
FACT_StockMovement[MovementTypeDesc] = "StockCount"
)
)
RETURN
IF (
ISBLANK ( LastStockCountDate ),
CALCULATE (
SUM ( FACT_StockMovement[Quantity] ),
FILTER (
ALL ( FACT_StockMovement ),
FACT_StockMovement[Warehouse] = CurrentWarehouse &&
FACT_StockMovement[Product2] = CurrentProduct &&
FACT_StockMovement[Date] <= CurrentDate
)
),
CumulativeQuantityBeforeStockCount + LastStockCountQuantity
)
Also, i cannot wrap my head around how to get the stock in han on any given date. In the attached solution it only gives me the stock on hand when there is a movement or a stock count present for the combination “date-product-warehouse”
So my questions are:
- Any performance improvement tips? Either on the DAX code or modelling changes in Power Query (The source is an on prem SQL db)
- How do i get the daily stock on hand, regardless if there is a “date-product-warehouse” combination present in the dataset?
DummyFile.xlsx (43.1 KB)
stockmovementcase.pbix (68.9 KB)