Windowing functions for streaming data

By Hatnest · Reviewed by Saurav Raj Joshi · Updated Nov 30, 2026

A stream never ends, so an aggregate such as "count" or "average" needs a boundary. A window is that boundary: a slice of time over which events are grouped and summarized. Which windowing function you pick decides how events are assigned to windows, whether a single event can count more than once, and when a result is finished. Fabric exposes windows in three places: the eventstream Group by operator and SQL operator, Spark structured streaming, and KQL.

The window types

  • Tumbling: fixed length, back to back, no overlap. Each event belongs to exactly one window. Use it for regular reporting such as "orders per minute".
  • Hopping: fixed length, but a new window starts every hop, so windows overlap and one event can fall into several. A five-minute window that hops every minute gives a smoothed, rolling view. A tumbling window is a hopping window whose hop equals its size.
  • Sliding: the meaning depends on the engine. In Spark, a sliding window is a fixed-size window with a slide interval, which is the hopping idea (a rolling 10-minute count produced every minute). In the Stream Analytics language of an eventstream, a sliding window has a fixed length but gives a result only at the moments its content changes, so every window holds at least one event. The eventstream Group by operator covers tumbling, hopping and sliding patterns.
  • Session: not fixed in length. Events for the same key that arrive close together form a session, and a gap of inactivity (for example 15 minutes) closes it. Use it for user activity or machine runs.

Rule of thumb: tumbling for non-overlapping periodic totals, hopping or sliding for rolling metrics and alerts, session for bursts of activity separated by quiet gaps.

In an eventstream

No-code: add a Group by operator, choose the fields to group by, pick the window type and duration, and preview the result. For code, the SQL operator uses the Stream Analytics language, where the window function appears in GROUP BY.

-- tumbling: one result per room per minute
SELECT System.Timestamp AS WindowEnd, roomId, AVG(temperature) AS AvgTemp
INTO output FROM input
GROUP BY roomId, TumblingWindow(minute, 1)

-- hopping: 5 minute window, evaluated every minute
SELECT System.Timestamp AS WindowEnd, userId, COUNT(*) AS Orders
INTO output FROM input
GROUP BY userId, HoppingWindow(minute, 5, 1)
HAVING COUNT(*) > 20

HoppingWindow(minute, 5, 1) reads as unit, window size, hop size.

In Spark structured streaming

Use window() on an event-time column inside groupBy.

from pyspark.sql import functions as F

tumbling = (events.withWatermark("event_time", "10 minutes")
    .groupBy(F.window("event_time", "5 minutes"), "device_id").count())

sliding = (events.withWatermark("event_time", "10 minutes")
    .groupBy(F.window("event_time", "10 minutes", "1 minute"), "device_id").count())

sessions = (events.withWatermark("event_time", "10 minutes")
    .groupBy(F.session_window("event_time", "15 minutes"), "device_id").count())

The third argument to window() is the slide. Without it you get a tumbling window. Session windows use session_window() with a gap duration, and they do not support update output mode.

Late events and watermarks

Windows are defined on event time, the time stamped on the event, not the time Spark receives it. The watermark states how late an event may be. Spark keeps a window open for updates until the watermark passes its end, then finalizes it and drops its state. With a watermark, append mode can emit a window only once it is final. A shorter watermark gives faster output but may discard late events; a longer one keeps more state. If no new data arrives, the watermark does not advance, so the last windows may stay unfinished.

In KQL

KQL has no dedicated tumbling operator. You build fixed windows by rounding time with bin() and grouping on it.

Telemetry
| summarize Events = count(), AvgTemp = avg(Temp) by bin(Timestamp, 1m), DeviceId

For other shapes:

  • sliding_window_counts (called with evaluate) returns counts and distinct counts over a lookback window for each step.
  • row_window_session() works on a serialized (ordered) row set and returns where each session starts, using a maximum session length and a maximum gap between neighboring events.
  • Window functions need serialized input, so sort or use serialize first, and do it on the smallest row set you can because serialization limits parallelism.

Common mistakes

  • Confusing tumbling and hopping: a tumbling window never double counts an event.
  • Windowing on processing time and expecting results to match event time.
  • Omitting a watermark, so append-mode aggregation cannot finish, or state grows forever.
  • In KQL, forgetting bin() and grouping on every distinct timestamp.

For the exam

  • Tumbling: fixed, non-overlapping. Hopping or sliding: fixed, overlapping. Session: closed by a gap.
  • HoppingWindow(minute, 5, 1) is a five-minute window every minute.
  • Spark: window(col, size) tumbling, window(col, size, slide) sliding, session_window(col, gap).
  • The watermark bounds lateness and releases state.
  • KQL: bin() with summarize for fixed windows; row_window_session() and sliding_window_counts for the rest.

Sources