Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Faggregation%2Fdistinct_count_if.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Faggregation%2Fdistinct_count_if.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `distinct_count_if`

## Description

Returns the number of distinct, non-null values that satisfy a given condition.

* A document is counted only if the condition evaluates to `true`.
* Duplicate values for the same expression are counted once per group.

Note

`distinct_count_if` is an aggregation function and must be used with a grouping keyword such as `groupby`.

## Syntax

```
distinct_count_if(condition: bool, expression: any): number
```

## Arguments

| Name         | Type      | Required | Description                                                     |
| ------------ | --------- | -------- | --------------------------------------------------------------- |
| `condition`  | `boolean` | **true** | Boolean expression indicating whether the row should be counted |
| `expression` | `any`     | **true** | Expression whose unique, non-null values are counted            |

## Example

**Use case: Count users who have experienced errors**

Suppose you want to count how many unique users encountered an error for each application. Here, an error is defined as a log with `$m.severity == ERROR`.

### Example data

```
{ "applicationname": "checkout-service", "username": "alice", "severity": "Error" },

{ "applicationname": "pager", "username": "bob", "severity": "Info" },

{ "applicationname": "checkout-service", "username": "alice", "severity": "Error" },

{ "applicationname": "checkout-service", "username": "charlie", "severity": "Error" }

{ "applicationname": "pager", "username": "bob", "severity": "Error" }

{ "applicationname": "checkout-service", "username": "charlie", "severity": "Error" }
```

### Example query

```
groupby $l.applicationname aggregate distinct_count_if($m.severity == ERROR, $d.username) as users_with_errors
```

### Example output

| applicationname  | users*with*errors |
| ---------------- | ----------------- |
| checkout-service | 2                 |
| pager            | 1                 |
