Forum Discussion
DAX Help
- 3 years ago
AV_04 , Okay then please create a column as below:-
_no_stocks_left = VAR cust_bought = COUNTROWS ( FILTER ( 'Table', 'Table'[Product] = EARLIER ( 'Table'[Product] ) && 'Table'[Sub-Product] = EARLIER ( 'Table'[Sub-Product] ) ) ) VAR stock_available = CALCULATE ( MAX ( 'Table'[Stocks Available] ), FILTER ( 'Table', 'Table'[Product] = EARLIER ( 'Table'[Product] ) && 'Table'[Sub-Product] = EARLIER ( 'Table'[Sub-Product] ) ) ) VAR _result = stock_available - cust_bought RETURN IF ( _result = 0, 1,0 )Then create a measure as below:-
Measure = CALCULATE(DISTINCTCOUNT('Table'[Sub-Product]),FILTER('Table','Table'[_no_stocks_left] = 1))Refer a file below:-
Above code satify this condition right?
Could you please share your final required output based on provided data?
Thanks samarth. I am halfway through the solution, I just need the distinct count of sub category that goes out of stock. Here in the above table the count will be only one (Lewis), incase if any other sub category within product satisfies the condition, we need to do count+1. I am not sure how to draw the resultant up in a table.
| Product | Sub-Product | Bought by customer? | Stocks Available |
| Sports | lewis | 1 | 4 |
| Sports | lewis | 1 | 4 |
| Sports | lewis | 1 | 4 |
| Sports | lewis | 1 | 4 |
| Sports | J1 | 1 | 2 |
| Sports | J1 | 1 | 2 |
| accessories | Jacks | 1 | 3 |
| accessories | Jacks | 1 | 3 |
| Sports | chrome | 1 | 2 |
| Sports | chrome | 1 | 2 |
For the above table the resultant count should be 3 since lewis, j1 and chrome satisfies the condition.
I hugely thank you for the solution. I just started with my advanced dax journey hopefully can catch up soon.
- Samarth_183 years agoCommunity Champion
AV_04 , Okay then please create a column as below:-
_no_stocks_left = VAR cust_bought = COUNTROWS ( FILTER ( 'Table', 'Table'[Product] = EARLIER ( 'Table'[Product] ) && 'Table'[Sub-Product] = EARLIER ( 'Table'[Sub-Product] ) ) ) VAR stock_available = CALCULATE ( MAX ( 'Table'[Stocks Available] ), FILTER ( 'Table', 'Table'[Product] = EARLIER ( 'Table'[Product] ) && 'Table'[Sub-Product] = EARLIER ( 'Table'[Sub-Product] ) ) ) VAR _result = stock_available - cust_bought RETURN IF ( _result = 0, 1,0 )Then create a measure as below:-
Measure = CALCULATE(DISTINCTCOUNT('Table'[Sub-Product]),FILTER('Table','Table'[_no_stocks_left] = 1))Refer a file below:-
- AV_043 years agoHelper I
Thank you very much samarth. This one works!