Forum Discussion
Counting the rows in a category
- 3 years ago
Hi Anonymous ,
you can modify the measure like so:WeeksInQuartile = VAR __previousWeek = 'Table'[id_week] - 1 VAR __Result = RANKX ( FILTER( 'Table', 'Table'[id_employee] = EARLIER('Table'[id_employee]) && 'Table'[quartile] = EARLIER('Table'[quartile]) && 'Table'[quartile] = CALCULATE(MAX('Table'[quartile]), FILTER(all('Table'), 'Table'[id_week] = __previousWeek)) ), 'Table'[id_week], 'Table'[id_week], ASC ) Return __Result - 3 years ago
Hello Anonymous ,
thanks for taking the time to point out the error in the previous formula.
Please try out this new approach:WeeksInQuartile = VAR __StartRangeWeek = MAXX ( FILTER ( 'Table', 'Table'[id_employee] = EARLIER ( 'Table'[id_employee] ) && 'Table'[id_week] <= EARLIER ( 'Table'[id_week] ) && 'Table'[quartile] <> EARLIER ( 'Table'[quartile] ) ), 'Table'[id_week] ) VAR __EndRangeWeek = MINX ( FILTER ( 'Table', 'Table'[id_employee] = EARLIER ( 'Table'[id_employee] ) && 'Table'[id_week] >= EARLIER ( 'Table'[id_week] ) && 'Table'[quartile] <> EARLIER ( 'Table'[quartile] ) ), 'Table'[id_week] ) VAR __Result = RANKX ( FILTER ( 'Table', 'Table'[id_employee] = EARLIER ( 'Table'[id_employee] ) && 'Table'[quartile] = EARLIER ( 'Table'[quartile] ) && 'Table'[id_week] > __StartRangeWeek && 'Table'[id_week] < IF ( __EndRangeWeek = BLANK (), EARLIER ( 'Table'[id_week] ) + 1, __EndRangeWeek ) ), 'Table'[id_week], 'Table'[id_week], ASC ) RETURN __ResultPerformance will probably be terrible on large datasets.
A Power Query solution would probably be much faster in this case.
Hi Anonymous,
I can see that the numbers in my previous reply match with those in your post. Can you please elaborate this driver change scenario?
Sorry, my sample data wasn't taking into consideration every case. Using the code of your reply, if a same id_employee is in quartile q1 then in quartile q2 and then again in quartile q1 I get the following result for the calculated column:
Weeks in quartile
1
1
2
What I should get is:
Weeks in quartile
1
1
1
Because the counter should reset if the weeks in a same quartile aren't consecutive.
Thanks!