count / count() / countif()
There are three ways to count in KQL, and they produce different column names.
The count operator counts the rows that reach it and returns a single row with a single column
called Count:
SecurityEvent
| where EventID == 4625
| count
The count() aggregate is used inside summarize. With by, it counts the rows in
each group. Its default column name is count_ (with an underscore), so a later operator refers to
it as sort by count_. Naming it is clearer: summarize Failures = count() by IPAddress.
countif(condition) counts only the rows where the condition is true. It lets you compute several
counts in one summarize, which is shorter and faster than running two queries. Its default name is
countif_.
Things to know
count(the operator) takes no parentheses and stands on its own after a pipe;count()(the aggregate) needs them and goes insidesummarize.- Counts are whole numbers (
long). When you divide one count by another, convert one side first withtodouble(), or the result is rounded down. - To count different values rather than rows, use
dcount().
Examples
SigninLogs
| where Location == "BR"
| countThe count operator returns one row with one column, named Count.
SigninLogs
| summarize count(), countif(ResultType != "0") by AppDisplayNameThe default names are count_ and countif_.
SecurityEvent
| summarize Logons = countif(EventID == 4624), Failures = countif(EventID == 4625) by Computer
| extend FailureShare = todouble(Failures) / (Logons + Failures)todouble() makes the division a decimal one; dividing two whole numbers would round down to 0.
Practice it
- #02Complete the query: brute force against a servermedium
- #04Size up a password spray from one IPeasy
- #06Failed sign-ins by client app, most firsteasy
- #07Which account names did the attacker try?easy
- #08When did the brute force start and stop?medium
- #10Complete the query: guesses at accounts that don't existmedium
- #14Find the one-device destination with hundreds of connectionsmedium
- #15Successful sign-ins from countries that almost never appearmedium
+ 14 in the full SC-200 set · all exercises