let

let gives a name to something so you can use it later in the same query. Each let statement ends with a semicolon, and the main query comes after the last one.

let threshold = 20;
let lookback = 1d;
SigninLogs
| where TimeGenerated > ago(lookback)
| summarize Failures = countif(ResultType != "0") by IPAddress
| where Failures > threshold

What you can name

  • A value: a number, a string, a date, a time span. Putting thresholds and time windows at the top makes a query easy to tune.
  • A tabular expression: a whole sub-query, such as let failures = SigninLogs | where ResultType != "0";. You then use failures like a table: start a query with it, join it, or union it.

A let can use the names defined above it. Names are case-sensitive.

Why use it

Detection rules and hunting queries in Microsoft Sentinel are full of let: they name the time window, the thresholds and the intermediate results. That keeps long queries readable, and it lets you use the same sub-query twice without copying it.

Things to know

  • A named sub-query is not stored: it is run where you use it. Using it twice runs it twice.
  • After the last let, write the query itself: the let statements only prepare names for it.
  • On this site, user-defined functions (let f = (x:long) { ... };) and materialize() are not supported yet.

Examples

1 A named point in time
let since = datetime(2026-06-14);
SigninLogs
| where TimeGenerated >= since
| summarize SignIns = count() by AppDisplayName
2 Name a sub-query, then use it as a table
let servers = SecurityEvent | distinct Computer;
servers
| count
3 A threshold and a sub-query in one query
let minLogons = 100;
let busyServers = SecurityEvent
    | where EventID == 4624
    | summarize Logons = count() by Computer
    | where Logons > minLogons;
busyServers
| join kind=inner (
    SecurityEvent
    | where EventID == 4634
    | summarize Logoffs = count() by Computer
  ) on Computer
| project Computer, Logons, Logoffs

Practice it

None of the free exercises needs this one yet.