case()

case() takes pairs of condition, value and a final default value:

case(
    RiskScore >= 80, "High",
    RiskScore >= 50, "Medium",
    "Low")

For each row it checks the conditions in order and returns the value of the first one that is true. If none is true, it returns the last argument, the default. The default is required.

Order matters

Because the first true condition wins, write the narrowest conditions first. In the example above, putting RiskScore >= 50 first would label a score of 90 as “Medium”.

case() or iff()?

For two outcomes, iff() is shorter. For three or more, case() is far easier to read than nested iff() calls.

All values, including the default, must have the same type.

Typical uses

Mapping codes to readable names, sorting rows into bands (low/medium/high), and labelling the stage of an attack from what a row contains.

Examples

1 Name the Windows event IDs
SecurityEvent
| extend Kind = case(
    EventID == 4624, "logon",
    EventID == 4625, "failed logon",
    EventID == 4634, "logoff",
    "other")
| summarize Events = count() by Kind
2 Order of the conditions matters
SigninLogs
| extend Hour = datetime_part("hour", TimeGenerated)
| extend Shift = case(Hour < 6, "night", Hour < 18, "day", "evening")
| summarize SignIns = count() by Shift

An hour of 3 matches both Hour < 6 and Hour < 18; the first true condition wins, so it is "night".

Practice it

None of the free exercises needs this one yet.