Forum Discussion
Stock Cover Calculation Measure
- 7 years ago
You may check the following DAX.
Column = VAR s = Table1[Stock] VAR w = Table1[Week] VAR t = FILTER ( Table1, Table1[Week] > w ) VAR t2 = ADDCOLUMNS ( t, "total", SUMX ( FILTER ( t, Table1[Week] <= EARLIER ( Table1[Week] ) ), Table1[Sales Plan] ) ) RETURN IF ( COUNTROWS ( FILTER ( t2, [total] >= s ) ) > 0, COUNTROWS ( FILTER ( t2, [total] < s ) ) + DIVIDE ( s - MAXX ( TOPN ( 1, FILTER ( t2, [total] < s ), Table1[Week], DESC ), [total] ), MAXX ( TOPN ( 1, FILTER ( t2, [total] >= s ), Table1[Week], ASC ), Table1[Sales Plan] ) ) )
v-chuncz-msft wrote:
You may check the following DAX.
Column = VAR s = Table1[Stock] VAR w = Table1[Week] VAR t = FILTER ( Table1, Table1[Week] > w ) VAR t2 = ADDCOLUMNS ( t, "total", SUMX ( FILTER ( t, Table1[Week] <= EARLIER ( Table1[Week] ) ), Table1[Sales Plan] ) ) RETURN IF ( COUNTROWS ( FILTER ( t2, [total] >= s ) ) > 0, COUNTROWS ( FILTER ( t2, [total] < s ) ) + DIVIDE ( s - MAXX ( TOPN ( 1, FILTER ( t2, [total] < s ), Table1[Week], DESC ), [total] ), MAXX ( TOPN ( 1, FILTER ( t2, [total] >= s ), Table1[Week], ASC ), Table1[Sales Plan] ) ) )
Hi All,
the above DAX is perfect for a simple table. Can it be further adapted to take into account additional dimensions as per below table?
Any help appreciated
| Product | Week | Stock | Sales Plan | Stock Cover |
| A | W01 | 100 | 40 | |
| A | W02 | 120 | 40 | |
| A | W03 | 130 | 40 | |
| A | W04 | 110 | 40 | |
| A | W05 | 80 | 40 | |
| A | W06 | 100 | 50 | |
| A | W07 | 120 | 50 | |
| B | W01 | 150 | 80 | |
| B | W02 | 180 | 80 | |
| B | W03 | 195 | 80 | |
| B | W04 | 165 | 80 | |
| B | W05 | 120 | 80 | |
| B | W06 | 150 | 100 | |
| B | W07 | 180 | 100 |
Got it, in the end. :manhappy:
Column =
VAR s = Table1[Stock]
VAR w = Table1[Week]
VAR x = Table1[Product]
VAR t =
FILTER ( Table1, Table1[Week] > w && Table1[Product] = x)
VAR t2 =
ADDCOLUMNS (
t,
"total", SUMX (
FILTER ( t, Table1[Week] <= EARLIER ( Table1[Week] )&&Table1[Product]=EARLIER(Table1[Product])),
Table1[Sales Plan]
)
)
RETURN
IF (
COUNTROWS ( FILTER ( t2, [total] >= s ) )
> 0,
COUNTROWS ( FILTER ( t2, [total] < s ) )
+ DIVIDE (
s
- MAXX ( TOPN ( 1, FILTER ( t2, [total] < s ), Table1[Week], DESC ), [total] ),
MAXX (
TOPN ( 1, FILTER ( t2, [total] >= s ), Table1[Week], ASC ),
Table1[Sales Plan]
)
)
)- Sergii247 years agoSuper User
Dear darylmc, v-chuncz-msft and else!
I've tried to replicate your solution and was wondering if it can be adopted to a calculated mesure with additional parameter - product location. This parameter creates an issues that depending on the filter I might have one, multiple or none (meaning all selected) locations selected.
The suggested solution creates a calculated column and doesn't work on aggregation level, because the Stock Cover calculation is not a sum or avergage but should be performed any time the context is changed.
In the example below the calculated column gives a wrong result on aggregated level, because summarize the selection instead of recalculating it again: - 10.83 days, while it should be 4.42 days: open inventory < forecast (prod of period N is considered as a part of open stock of period N+1, so doesn't have impact for Stock Coverage of week N).
I would be very grateful for your help!
Thank you!
- Anonymous6 years agoNot applicable
Hi darylmc
Many thanks for the solution, but do you know if there's any reason this wouldn't work in Power Pivot? It works perfectly in Power BI, but I'm just wanting to use the same calculated column within Power Pivot for Excel and I'm not sure why it's producing the same results.
Below are the results from Power BI and then Power Pivot in Excel. Below are the differing results within Power BI and Power Pivot in Excel.
The code is the same in both:
Weeks Cover = VAR s = 'Stock Movements'[Forecast Stock in Hand] VAR w = 'Stock Movements'[Date] VAR x = 'Stock Movements'[Product Code] VAR t = FILTER ( 'Stock Movements', 'Stock Movements'[Date] > w && 'Stock Movements'[Product Code] = x) VAR t2 = ADDCOLUMNS ( t, "total", SUMX ( FILTER ( t, 'Stock Movements'[Date] <= EARLIER ( 'Stock Movements'[Date] )&&'Stock Movements'[Product Code]=EARLIER('Stock Movements'[Product Code])), 'Stock Movements'[Forecast] ) ) RETURN IF ( COUNTROWS ( FILTER ( t2, [total] >= s ) ) > 0, COUNTROWS ( FILTER ( t2, [total] < s ) ) + DIVIDE ( s - MAXX ( TOPN ( 1, FILTER ( t2, [total] < s ), 'Stock Movements'[Date], DESC ), [total] ), MAXX ( TOPN ( 1, FILTER ( t2, [total] >= s ), 'Stock Movements'[Date], ASC ), 'Stock Movements'[Forecast] ) ) )Any ideas would be greatly appreciated.
Many thanks,
Paddy