TH2.7 arg_max and arg_min for Entity Investigation
The right event, not all events
During the enrichment step (TH1.4), you often need one specific event per entity: the most recent sign-in for a suspect user, the first sign-in from a new IP, the highest-risk event for a flagged account. The naive approach — | sort by TimeGenerated desc | take 1 — works for a single entity but fails when you need it per-entity across a population.
arg_max(Column, *) inside a summarize returns the full row where Column has its maximum value, for each group. arg_min returns the minimum.
| |
| |
| |
Figure TH2.7 — Three arg_max/arg_min patterns. Each returns one row per entity with the full event details preserved.
Try it yourself
Exercise: Find first-seen IPs for your suspect users
Take the suspect users from the TH2.1 or TH2.3 exercises. For each, use arg_min to find their first sign-in from each IP in the last 30 days. Are the anomalous IPs recently first-seen, or have they been used before?
The myth: sort by TimeGenerated desc | take 1 achieves the same result as arg_max and is easier to read.
The reality: sort + take works for a single entity but does not scale to per-entity extraction. summarize arg_max(TimeGenerated, *) by UserPrincipalName returns the most recent event for every user in a single pass. Achieving the same with sort + take requires a loop or serialization that KQL does not natively support. arg_max is not just a syntax preference — it enables a query pattern that sort + take cannot replicate efficiently.
Extend this pattern
arg_max and arg_min accept multiple output columns instead of `*`: `arg_max(TimeGenerated, IPAddress, AppDisplayName)`. This returns only the specified columns from the maximum row, reducing result size when you do not need all columns. Use this in performance-sensitive queries where the full row is unnecessary.
References Used in This Subsection
- Microsoft. “KQL arg_max() Aggregation Function.” Microsoft Learn. https://learn.microsoft.com/en-us/kusto/query/arg-max-aggfunction
You're reading the free modules of this course
The full course continues with advanced topics, production detection rules, worked investigation scenarios, and deployable artifacts. Premium subscribers get access to all courses.