Skip to content

distinct_count_if - Count distinct value that pass some condition

The distinct_count_if function will count a given document if:

  • The document is able to match a given condition, which is a boolean expression.
  • The document contains a unique, non-null value for a specified field.

NOTE: distinct_count_if is an aggregation function, so should be used in conjunction with a grouping keyword, like groupby.

Syntax

distinct_count_if(condition: bool, expression: any)

Arguments

Name Type Required Description
condition bool true bool value indicating if row should be counted or not
expression any true The non-null value to be counted distinctly

Example - Counting the number of users who have experienced an error

In this example, we're seeking to understand how many of our users have experienced an error, for each application. An error in this case is any log event with a $m.severity of Error.

We simply group by a given application, and we provide our condition and our expression:

  • $m.severity == 'Error' is true is the severity of a given log is Error.
  • $d.username is our unique value, meaning we will only count each unique value of $d.username. In other words, if a user has experienced an error twice, they're only counted once.
groupby $l.applicationname aggregate distinct_count_if($m.severity == 'Error', $d.username) as users_with_errors