Forum Discussion
Calculate, Filters & Distinct Count based on a Calculated measure
Hello PowerBI Community!
I thought I understood the concept behind the CALCULATE function, the row context vs. the filter context, but it appears I still have a long way to master it :smileyhappy:
Here is my (basic problem):
- I have a Sales Table with monthly Sales Units (SELL_OUT_UNIT) by Product / By Store
- You'll notice that the Stock Unit is the same for the two dates, that's because I'm merging my sales data set with my stock data set (which contains only the last day stock value, I don't have the history)
Here is what I'm trying to do:
- Build a measure that would give the number of stores that are likely to be out of stock at the end of the month:
TO-DO so, here is what I built:
_VOL All Period := SUM([SELL_OUT_UNIT]) -- The measure holding the sum of units
_DATE # Days Elapsed Month = DATEDIFF([_DATE First Date of Month], [_DATE Current Date] , DAY) +1 -- The measure counting the number of days elapsed this month (the sub measures are straight forward so I don't detail them)
_DATE # Days Left Max Month = DATEDIFF([_MAX Date], EOMONTH([_MAX Date],0),DAY) -- The number of days left for this month
_AVG VOL per Day = IF([_VOL All Period] > 0, DIVIDE([_VOL All Period], [_DATE # Days Elapsed Max Month], BLANK())) -- The measure which calculates the average units sold per day for the end of the month estimation
_EXP Additional VOL End of Month = ROUND(([_AVG VOL per Day] * [_DATE # Days Left Max Month]),0) -- The measure that calculates the expected additional units at the end of the month
_DATE Count Distinct Date = DISTINCTCOUNT('SLS-Weekly'[SELL_OUT_DATE]) -- The measure calculating the number of dates in my data set for the current selected month
_STO All Period = DIVIDE(SUM('SLS-Weekly'[STOCK_UNIT]), [_DATE Count Distinct Date],0) -- The measure calculating my level of stock. I need to divide the sum of Stock Unit by the number of dates in the data set since I've merged my sales data set with my stock data set so the stock units are duplicated (see picture above)
_STO vs EXP End of Month Alert = ROUND([_STO All Period] - [_EXP Additional VOL End of Month],0) -- The measure that calculates the difference between the stock left and the expected end of the month volume
Now that the stage is set, I'd like to calculate the number of stores that are likely to be out of stock at the end of the month, so in theory, I'd like to count the number of stores where the measure _STO vs EXP End of Month Alert would be less than or equal to 0
Here is the measure that I tried to built, but it returns me 17 and I have no clue how this 17 is being calculated:
_Test = CALCULATE(DISTINCTCOUNT('SLS-Weekly'[STORE_NAME]), FILTER('SLS-Weekly', [_STO vs EXP End of Month Alert] <= 0))
Thanks a lot for your help!
1 Reply
- v-chuncz-msftCommunity Support