# `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

```dataprime
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

```json
{ "operationName": "scheduledEvent", "duration": 567 },
{ "operationName": "HipsterShop", "duration": 512 },
{ "operationName": "login", "duration": 33 }
{ "operationName": "HipsterShop", "duration": 744 },
```

### Example query

```dataprime
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                           |
