I want to calculate no. of days where the status is set to 1. Everytime the status is 0, the count should restart from 1.
Note: It need not be consecutive days
Input
UserId | Day | Status|
A |2021-09-16 | 1 |
A |2021-09-17 | 1 |
A |2021-09-18 | 1 |
A |2021-09-19 | 1 |
A |2021-09-20 | 0 |
A |2021-09-21 | 1 |
B |2021-09-16 | 1 |
B |2021-09-17 | 1 |
B |2021-09-20 | 1 |
B |2021-09-21 | 0 |
Expected Output
UserId | Day | Status| Cum_Sum |
A |2021-09-16 | 1 | 1 |
A |2021-09-17 | 1 | 2 |
A |2021-09-18 | 1 | 3 |
A |2021-09-19 | 1 | 4 |
A |2021-09-20 | 0 | 0 |
A |2021-09-21 | 1 | 1 |
B |2021-09-16 | 1 | 1 |
B |2021-09-17 | 1 | 2 |
B |2021-09-20 | 1 | 3 |
B |2021-09-21 | 0 | 0 |
you can use the row_cumsum()
function :
datatable(UserId:string, Day:datetime, Status:int)
[
"A", datetime(2021-09-16), 1,
"A", datetime(2021-09-17), 1,
"A", datetime(2021-09-18), 1,
"A", datetime(2021-09-19), 1,
"A", datetime(2021-09-20), 0,
"A", datetime(2021-09-21), 1,
"B", datetime(2021-09-16), 1,
"B", datetime(2021-09-17), 1,
"B", datetime(2021-09-20), 1,
"B", datetime(2021-09-21), 0,
]
| order by UserId asc, Day asc
| extend cumulative_sum = row_cumsum(Status, prev(UserId) != UserId or Status == 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.