#454·skills

kql skill: sessionization examples pass `row_window_session()` arguments in the wrong order

Author: ZhangyinglunCreated Sep 24, 2026Updated Sep 24, 2026

Repository: microsoft/skills Skill: .github/skills/kql Observed at commit: 23d0dac5f83f268166a17f0bc7dc6c73dc348a33 (latest main; last change to the skill directory: e7457389248c42433c940c3e497e2ee6c37604d4)

Summary

Two sessionization examples swap the second and third arguments of row_window_session(). The idle gap from each code comment is passed as MaxDistanceFromFirst (maximum session length) instead of MaxDistanceBetweenNeighbors (maximum gap between consecutive rows). The queries still run, but they split sessions by fixed duration rather than by idle time. Agents that copy them get silently wrong groupings.

The first template also fails to compile: its summarize defines an aggregate named SessionStart while also grouping by SessionStart.

Reference

From the row_window_session() documentation:

row_window_session(Expr, MaxDistanceFromFirst, MaxDistanceBetweenNeighbors [, Restart])
  • MaxDistanceFromFirst: start a new session when the value is at least this far from the session's first value.
  • MaxDistanceBetweenNeighbors: start a new session when the value is at least this far from the previous value.

The official example row_window_session(Timestamp, 1h, 5m, ID != prev(ID)) means "a session can't exceed one hour; it continues while records are less than five minutes apart."

Affected examples

1. references/query-templates.md (lines 176–187), section "Gap-based sessions"

kql
// Group events into sessions (30-min idle gap)
Events
| order by UserId, Timestamp asc
| extend SessionStart = row_window_session(Timestamp, 30m, 24h, UserId != prev(UserId))
| summarize
    SessionStart = min(Timestamp),
    SessionEnd = max(Timestamp),
    EventCount = count()
    by UserId, SessionStart

Problems:

  1. Argument order. 30m, 24h means "sessions last at most 30 minutes; a new session starts only after a 24-hour gap". The comment asks for a 30-minute idle gap.
  2. Duplicate column. SessionStart = min(Timestamp) duplicates the SessionStart group key. The query fails with: Semantic error: SEM0034: Duplicate column name(s): SessionStart

2. references/advanced-patterns.md (line 341), section "Sessionization"

kql
// try it! — group trace logs into sessions with 5-minute gap
cluster("help").database("SampleLogs").TraceLogs
| order by Source, Timestamp asc
| extend SessionId = row_window_session(Timestamp, 5m, 1h, Source != prev(Source))
| summarize EventCount = count(), SessionStart = min(Timestamp) by Source, SessionId
| take 5

Problem: 5m, 1h means "sessions last at most 5 minutes; a new session starts only after a 1-hour gap". The comment asks for a 5-minute gap.

Reproduction

Run on https://help.kusto.windows.net, database Samples. Each u1 event is 20 minutes after the previous one until 11:00, followed by a two-hour gap.

kql
let Events = datatable(UserId:string, Timestamp:datetime)[
    "u1", datetime(2024-01-01 10:00), "u1", datetime(2024-01-01 10:20),
    "u1", datetime(2024-01-01 10:40), "u1", datetime(2024-01-01 11:00),
    "u1", datetime(2024-01-01 13:00), "u2", datetime(2024-01-01 10:05)];
Events
| order by UserId, Timestamp asc
| extend SessionStart = row_window_session(Timestamp, 30m, 24h, UserId != prev(UserId))  // current argument order
| summarize SessionEnd = max(Timestamp), EventCount = count() by UserId, SessionStart

The duplicate SessionStart aggregate is removed so the query compiles. Results for user u1:

Arguments u1 sessions Expected for a 30-minute idle gap?
30m, 24h (current) 10:00–10:20 (2), 10:40–11:00 (2), 13:00 (1) No. The 10:00–11:00 activity is cut into fixed 30-minute windows even though no gap exceeds 20 minutes.
24h, 30m (proposed) 10:00–11:00 (4), 13:00 (1) Yes

Running the original template unchanged returns SEM0034: Duplicate column name(s): SessionStart.

Proposed fix

diff
--- a/.github/skills/kql/references/query-templates.md
+++ b/.github/skills/kql/references/query-templates.md
@@
 // Group events into sessions (30-min idle gap)
 Events
 | order by UserId, Timestamp asc
-| extend SessionStart = row_window_session(Timestamp, 30m, 24h, UserId != prev(UserId))
+| extend SessionStart = row_window_session(Timestamp, 24h, 30m, UserId != prev(UserId))
 | summarize
-    SessionStart = min(Timestamp),
     SessionEnd = max(Timestamp),
     EventCount = count()
     by UserId, SessionStart
diff
--- a/.github/skills/kql/references/advanced-patterns.md
+++ b/.github/skills/kql/references/advanced-patterns.md
@@
 // try it! — group trace logs into sessions with 5-minute gap
 cluster("help").database("SampleLogs").TraceLogs
 | order by Source, Timestamp asc
-| extend SessionId = row_window_session(Timestamp, 5m, 1h, Source != prev(Source))
+| extend SessionId = row_window_session(Timestamp, 1h, 5m, Source != prev(Source))
 | summarize EventCount = count(), SessionStart = min(Timestamp) by Source, SessionId
 | take 5

The 24h and 1h session caps are illustrative. Any cap that is longer than the idle gap preserves the documented intent. Both corrected queries were run on the public help cluster: the first with the datatable above, the second against SampleLogs.TraceLogs.

Suggested follow-up

Consider adding a short argument-order note to the SKILL.md serialization or datetime section, for example: "row_window_session(ts, maxSessionLength, maxGap): the idle gap is the third argument."