distinct

distinct returns one row for each unique combination of the columns you name, and only those columns.

SigninLogs
| distinct UserPrincipalName, Location

This lists every user together with every country they signed in from. A user seen from two countries appears twice, once per country. distinct * uses all columns and removes exact duplicate rows.

distinct, dcount() or summarize?

  • distinct gives you the values themselves.
  • dcount() gives you how many different values there are, e.g. the number of users per IP address.
  • summarize ... by A, B gives the same rows as distinct A, B, and lets you add aggregates such as count() next to them. So when you also want a count, go straight to summarize.
  • make_set() puts the distinct values into one list per group.

Things to know

  • The rows come back in no particular order. Add sort by if order matters.
  • Comparisons are exact: Admin and admin are two different values. Normalise first if needed: extend Name = tolower(Name) | distinct Name.

Examples

1 Which client apps appear at all?
SigninLogs
| distinct ClientAppUsed
2 Unique combinations of two columns
SecurityEvent
| where EventID == 4624
| distinct Computer, LogonTypeName

Each server appears once per kind of logon it saw.

3 Who ran cmd.exe, and where?
DeviceProcessEvents
| where FileName == "cmd.exe"
| distinct DeviceName, AccountName

Practice it

None of the free exercises needs this one yet.