Forum Discussion
Kusto: summarize consecutive segments of the same value per batch
You need a gaps-and-islands query: create a new segment whenever either the batch changes or the team differs from the previous serialized row. Sorting is essential because prev() and row_cumsum() operate on a serialized row set. This query preserves a team that returns later as a separate segment:
YourTable | sort by batch_number asc, datetime asc | serialize | extend starts_segment = batch_number != prev(batch_number) or team != prev(team) or isnull(prev(batch_number)) | extend segment_id = row_cumsum(iff(starts_segment, 1, 0)) | summarize start_time=min(datetime), end_time=max(datetime) by batch_number, team, segment_id | project-away segment_id | sort by batch_number asc, start_time asc
segment_id is global, which is fine because every batch boundary increments it; grouping still includes batch_number. If two rows in one batch can share the same datetime, add a deterministic tie-breaker column to the sort. Without that, “consecutive” is undefined for tied events and results can vary.