Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Faggregation%2Fcount_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%2Fcount_if.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `count_if`

## Description

Returns the number of rows that satisfy a given condition, counting only non-null expression values.

* Useful for measuring subsets of data within groups.
* Can evaluate a condition alone or in combination with a non-null expression.

Note

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

## Syntax

```
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`     | **false** | Optional expression. If provided, only non-null values are counted |

## Example

**Use case: Count high-duration HTTP requests per operation**

Suppose you want to count requests where the duration exceeds 500 ms, grouped by operation name.

### Example data

```
{ "operationName": "scheduledEvent", "duration": 567 },

{ "operationName": "HipsterShop", "duration": 512 },

{ "operationName": "login", "duration": 33 }

{ "operationName": "HipsterShop", "duration": 744 },
```

### Example query

```
source spans

| groupby $l.operationName aggregate count_if($m.duration > 500) as high_duration_request_count
```

### Example output

| operationName  | high*duration*request\_count |
| -------------- | ---------------------------- |
| scheduledEvent | 1                            |
| HipsterShop    | 2                            |
