Forum Discussion
Resetting a cummulative counter in calculated column
Hello PBI Experts
I am trying to create a calculated column which will display the running total of a particular Status value by ItemID and Date until the status value changes. If the status value occurs again I want the running total to reset to 1.
Here is my example data. The column DesiredOutput is what I want the calculated column to display.
I have been struggling with this for a while so I hope someone can help. Thanks.
| Date | ItemID | Status | StatusCounter | DesiredOutput |
| 01 Jan 2020 | 22222 | A | 0 | 0 |
| 02 Jan 2020 | 22222 | B | 1 | 1 |
| 03 Jan 2020 | 22222 | B | 1 | 2 |
| 04 Jan 2020 | 22222 | A | 0 | 0 |
| 01 Jan 2020 | 55555 | B | 1 | 1 |
| 02 Jan 2020 | 55555 | B | 1 | 2 |
| 03 Jan 2020 | 55555 | A | 0 | 0 |
| 04 Jan 2020 | 55555 | B | 1 | 1 |
4 Replies
- v-eachen-msft
Community Support
Hi Anonymous ,
You could use EARLIER() function to get previous value.
At first, create an index column with RANKX() function. You could also create it in the query editor.
Index = RANKX ( FILTER ( 'Table', 'Table'[ItemID] = EARLIER ( 'Table'[ItemID] ) ), 'Table'[Date], , ASC, DENSE )Then use IF() to get the result.
Output = VAR a = CALCULATE ( FIRSTNONBLANK ( 'Table'[StatusCounter], 1 ), FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 && 'Table'[ItemID] = EARLIER ( 'Table'[ItemID] ) ) ) RETURN IF ( 'Table'[StatusCounter] = 0, 0, a + 'Table'[StatusCounter] )Here is my test file for your reference.
- AnonymousNot applicable
Thank you for your suggestion.
I have tried this approach and whilst the "Output" column resets to ZERO at the correct point the cummulative count in this column never exceeds a value of 2.
In my real data the cummulative count in this column may gets as high as 50 before a reset to zero event occurs so the calcuation needs to continue to accumulate beyond 2.- v-eachen-msft
Community Support
Hi Anonymous ,
I have edited my test file.
At first, create an index column in the query editor.
Then create a "Group" column to sort data.
Group = CALCULATE ( COUNTROWS ( 'Table' ), FILTER ( 'Table', 'Table'[Index] <= EARLIER ( 'Table'[Index] ) && 'Table'[Status] = "A" ) )Based on the "Group", use RANKX() to get result.
OutPut = VAR a = RANKX ( FILTER ( 'Table', 'Table'[Status] = EARLIER ( 'Table'[Status] ) && 'Table'[Index] <= EARLIER ( 'Table'[Index] ) && 'Table'[Group] = EARLIER ( 'Table'[Group] ) ), 'Table'[Index], , ASC, DENSE ) RETURN IF ( 'Table'[StatusCounter] = 0, 0, a )Here is the new file for your reference.