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

# `min_by`

## Description

Returns the value of an expression associated with the minimum value of a given sort key.

* Both `sortKey` and `expression` must be comparable types (`string`, `bool`, `number`, `interval`, `timestamp`, `regexp`, or `enum`).
* Useful for retrieving details from the row that has the earliest or smallest value of another field.

## Syntax

```
min_by(sortKey: T, expression: U): U
```

## Arguments

| Name         | Type | Required | Description                                                                                         |
| ------------ | ---- | -------- | --------------------------------------------------------------------------------------------------- |
| `sortKey`    | `T`  | **true** | Field to minimize. Must be `string`, `bool`, `number`, `interval`, `timestamp`, `regexp`, or `enum` |
| `expression` | `U`  | **true** | Value to return from the row with the minimum `sortKey`. Must be a comparable type                  |

## Example

**Use case: Retrieve details of the earliest event per user**

Suppose you want to know the first activity performed by each user. You can use `min` to compute the earliest timestamp and `min_by` to extract the associated details.

### Example data

```
{ "username": "chris", "path": "/home", "latency": 320, "ts": 1728919749261000000 },

{ "username": "chris", "path": "/checkout", "latency": 5000, "ts": 1728919759261000000 }
```

### Example query

```
groupby username aggregate min(ts) as ts, min_by(ts, path) as path
```

### Example output

| username | path  | ts                  |
| -------- | ----- | ------------------- |
| chris    | /home | 1728919749261000000 |
