> ## Documentation Index
> Fetch the complete documentation index at: https://offthepace.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# trailing_median

> Median of `value_col` over observations STRICTLY BEFORE the current row.

Median of `value_col` over observations STRICTLY BEFORE the current row. The canonical point-in-time baseline. Two models compute one (int\_lap\_thermal\_proxy's stint baseline, 08e; int\_corner\_skill\_residuals' corner field median, 02g). A third point-in-time rebuild, int\_sc\_hazard\_history (02d), needs a trailing RATE rather than a median and so calls trailing\_sum() below over the same frame. Both leaks that opened 08 were a baseline built with a GROUP BY over a block that included the row it scored -- write the window once, here, so the next one is a call rather than a rediscovery. Returns NULL where fewer than `min_observations` valid observations precede the row, rather than a median over one or two laps that is mostly noise. Those NULLs are NOT declarable from the feature contract: they are deterministic on the count of valid prior observations, which is not a contract axis. Emit trailing\_observation\_count() alongside as a companion column so a consumer can condition on it.

## Signature

```sql theme={null}
{{ trailing_median(value_col, partition_by, order_by, lookback=none, frame='rows', min_observations=1, valid_condition=none) }}
```

## Arguments

| Argument | Description |
| - | - |
| `value_col` | column or expression to take the median of |
| `partition_by` | list of columns, or a raw string -- the entity the baseline is for |
| `order_by` | list of columns, or a raw string -- the time axis |
| `lookback` | integer N for a trailing-N window, or none for expanding. Expanding pools the whole history; trailing-N keeps the window local to current conditions. The choice belongs to the caller and is a decision its leaf doc must record. |
| `frame` | 'rows' (default) or 'range' -- see trailing\_window\_frame |
| `min_observations` | floor below which the result is NULL (default 1) |
| `valid_condition` | boolean expression; rows failing it contribute no observation |

## Returns

scalar expression composable in a SELECT list.

## Usage

```sql theme={null}
SELECT
      {{ trailing_median('lap_time_s', ['stint_id'], ['lap_in_stint'],
                         valid_condition='is_valid_lap') }} AS stint_baseline_pace,
      {{ trailing_observation_count('lap_time_s', ['stint_id'], ['lap_in_stint'],
                         valid_condition='is_valid_lap') }} AS baseline_observations_n
    FROM combined
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.