# `percentile`

## Description

Returns the approximate n-th percentile of a numerical expression.

- The percentile value must be between `0` and `1`.
- The calculation is approximate, with accuracy controlled by the optional `error_threshold`.

## Syntax

```dataprime
percentile(percentile: number, expression: number, error_threshold: number?): number
```

## Arguments

| Name            | Type   | Required  | Description                                                                              |
| --------------- | ------ | --------- | ---------------------------------------------------------------------------------------- |
| percentile      | number | **true**  | Percentile to compute, between `0` and `1`                                               |
| expression      | number | **true**  | Numerical expression whose percentile is calculated                                      |
| error_threshold | number | **false** | Accuracy of approximation. Lower values are more accurate but slower. Defaults to `0.01` |

## Example

**Use case: Calculate the 99th percentile of trace durations**

Suppose you want to compute the 99th percentile latency for Lambda function invocations.

### Example data

```json
{ "operationName": "Lambda.Invoke", "duration": 95000 },
{ "operationName": "Lambda.Invoke", "duration": 500000 },
{ "operationName": "Lambda.Invoke", "duration": 1100000 },
{ "operationName": "Lambda.Invoke", "duration": 1482000 },
{ "operationName": "CheckoutFlow", "duration": 800000 }
```

### Example query

```dataprime
source spans
| filter $l.operationName == 'Lambda.Invoke'
| groupby $l.operationName aggregate percentile(0.99, $m.duration) as p99_latency
```

### Example output

| operationName | p99_latency       |
| ------------- | ----------------- |
| Lambda.Invoke | 1482198.478333334 |
