iff() / iif()

iff(condition, ifTrue, ifFalse) looks at the condition for each row and returns ifTrue when it holds, ifFalse otherwise. iif() is exactly the same function under another name.

| extend Severity = iff(FailedAccounts > 20, "High", "Low")

Both result values must have the same type: two strings, two numbers, and so on.

Typical uses

  • Labels: turn a code into words, as in the examples.
  • Fallbacks: iff(isempty(Url), RemoteIP, Url) uses the IP address when there is no URL.
  • Flags to count: extend IsAdmin = iff(...) and then group by it. For counting alone, countif() is shorter.

For more than two outcomes, nesting iff() inside iff() works but gets hard to read quickly. Use case() instead.

A condition that is null (for example a comparison with an empty number) counts as false, so the row gets the ifFalse value.

Examples

1 Label each sign-in as success or failure
SigninLogs
| extend Outcome = iff(ResultType == "0", "success", "failure")
| summarize count() by Outcome
2 Internal or internet traffic (iif is the same function)
DeviceNetworkEvents
| extend Direction = iif(RemoteIPType == "Public", "internet", "internal")
| summarize Connections = count(), Devices = dcount(DeviceName) by Direction
3 Flag busy servers after summarize
SecurityEvent
| where EventID == 4624
| summarize Logons = count() by Computer
| extend Load = iff(Logons > 150, "busy", "quiet")

Practice it

None of the free exercises needs this one yet.