Forum Discussion
Create column that shows data difference compared with last week in date format (YYYYWK)
- 3 years ago
Hi, Anonymous
Please try formula like:
diff = VAR _previousweek = CALCULATE ( MAX ( 'Table'[Week] ), FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Week] < MAX ( 'Table'[Week] ) ) ) VAR _previousvalue = CALCULATE ( [Stock weeks(FC)], FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Week] = _previousweek ) ) RETURN [Stock weeks(FC)] - _previousvalue'Table'[Week] (YYYYWK) need to be adjusted to 'whole number' type.
Best Regards,
Community Support Team _ Eason - 3 years ago
Hi Sammy,
Please try this measure and see if it works.
Diff Stock =var _previousweek =calculate(max(Stock[Week]),filter(all('Stock'),Stock[Week]<max(Stock[Week])))var _previousweekqty =calculate(sum(Stock[Stock Qty]),filter(all(Stock),Stock[Week]=_previousweek))var _previoussubqty =calculate(sum(Stock[Stock Qty]),filter(all(Stock),Stock[Week]=_previousweek),values(Stock[Sub-C]))returnSWITCH(TRUE(),ISINSCOPE(Stock[Sub-C]),sum(Stock[Stock Qty])-_previoussubqty,ISINSCOPE(Stock[Week]),sum(Stock[Stock Qty])-_previousweekqty,blank()) - Anonymous3 years ago
TonyZhou1980
Ive done some tweaking and following code seems to work to branch down into one sub-category:Diff. StockWk (FC) =
var _previousweek =
calculate(
max('Power BI'[Week]),
filter(allselected('Power BI'), 'Power BI'[Week]<max('Power BI'[Week]))
)
var _previousWEEKqty =
calculate(
[Stock weeks (FC)],
filter(allselected('Power BI'), 'Power BI'[Week] =_previousweek)
)
var _previousPRODqty =
calculate(
[Stock weeks (FC)],
filter(allselected('Power BI'), 'Power BI'[Week]=_previousweek),
values('Power BI'[Product type])
)
return
SWITCH(
TRUE(),
ISINSCOPE('Power BI'[Product type]),[Stock weeks (FC)]-_previousPRODqty,ISINSCOPE('Power BI'[Week]),[Stock weeks (FC)]-_previousWEEKqty,
blank()
)
I cannot seem to figure out how to add more subcategories to this. Because in reality I would want to branch down further 2-3 subcategories.
Any help is appreciated
/Sammy - Anonymous3 years ago
Hey,
I have managed now. Many thanks all for the help. If anyone is interested, here is the code I used with help from TonyZhou1980. Just replace SUB-C with your own sub categories:var _previousweek =CALCULATE(MAX('Power BI'[Week]),FILTER(ALLSELECTED('Power BI'), 'Power BI'[Week] < MAX('Power BI'[Week])))var _previousWEEKqty =CALCULATE([Stock weeks (FC)],FILTER(ALLSELECTED('Power BI'), 'Power BI'[Week] = _previousweek))var _previousPRODqty =CALCULATE([Stock weeks (FC)],FILTER(ALLSELECTED('Power BI'), 'Power BI'[Week] = _previousweek),VALUES('Power BI'[Sub-C1]))var _previousREGqty =CALCULATE([Stock weeks (FC)],FILTER(ALLSELECTED('Power BI'), 'Power BI'[Week] = _previousweek),VALUES('Power BI'[Sub-C1]),VALUES('Power BI'[Sub-C2]))var _previousSUBREGqty =CALCULATE([Stock weeks (FC)],FILTER(ALLSELECTED('Power BI'), 'Power BI'[Week] = _previousweek),VALUES('Power BI'[Sub-C1]),VALUES('Power BI'[Sub-C2]),VALUES('Power BI'[Sub-C3]))var _previousARTqty =CALCULATE([Stock weeks (FC)],FILTER(ALLSELECTED('Power BI'), 'Power BI'[Week] = _previousweek),VALUES('Power BI'[Sub-C1]),VALUES('Power BI'[Sub-C2]),VALUES('Power BI'[Sub-C3]),VALUES('Power BI'[Sub-C4]))RETURNSWITCH(TRUE(),ISINSCOPE('Power BI'[Sub-C4]), [Stock weeks (FC)] - _previousARTqty,ISINSCOPE('Power BI'[Sub-C3]), [Stock weeks (FC)] - _previousSUBREGqty,ISINSCOPE('Power BI'[Sub-C2]), [Stock weeks (FC)] - _previousREGqty,ISINSCOPE('Power BI'[Sub-C1]), [Stock weeks (FC)] - _previousPRODqty,ISINSCOPE('Power BI'[Week]), [Stock weeks (FC)] - _previousWEEKqty,BLANK())
Hey,
Thanks for reaching out
I will try this when I come home, this looks good, no issues adjusting YYYYWK to whole number in the data sheet.
Where can I enter this code though? Will it suffice creating a new measure and simply inserting the code there?
Best wishes
Sammy
Hi, Anonymous
Yes, it is a measure.
You need to insert the code and put it in your matrix.
Best Regards,
Community Support Team _ Eason
- Anonymous3 years agoNot applicable
Hey again v-easonf-msft
Many many thanks for providing this code to me, it is very helpful
It seems to work on a total lvl in my powerBI matrix but when I further drill down into subcategories under each week (on product type lvl) it seems not to work... Let me provide you with a screenshot (highlighted in yellow) what I mean:
You can see that on total lvl (in bold font) we have decreased the stockweeks with -0,1 which is correct.
But when I drill down one more "branch" into the matrix we can see that for product type "seat shell junior chair" we decreased with -2,2 stock weeks compared with wk202245 and this is not correct as it should be 24,2-25,1 = -0,9
Best wishes
Sammy