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

# interpolated_delta()

> Calculate the change in a counter, interpolating values at boundaries as needed

<Icon icon="tag" iconType="duotone" /> Since [1.14.0][toolkit-1.14.0]

Calculate the change in a counter over the time period covered by a counter aggregate. Data points at the exact
boundaries of the time period aren't needed. The function interpolates the counter values at the boundaries from
adjacent counter aggregates if needed.

## Samples

Calculate the counter delta for each 15-minute interval, using interpolation to get the values at the interval
boundaries if they don't exist in the data.

```sql theme={"dark"}
SELECT
    id,
    bucket,
    interpolated_delta(
        summary,
        bucket,
        '15 min',
        LAG(summary) OVER (PARTITION BY id ORDER by bucket),
        LEAD(summary) OVER (PARTITION BY id ORDER by bucket)
    )
FROM (
    SELECT
        id,
        time_bucket('15 min'::interval, ts) AS bucket,
        counter_agg(ts, val) AS summary
    FROM foo
    GROUP BY id, time_bucket('15 min'::interval, ts)
) t
```

## Arguments

The syntax is:

```sql theme={"dark"}
interpolated_delta(
    summary CounterSummary,
    start TIMESTAMPTZ,
    interval INTERVAL
    [, prev CounterSummary]
    [, next CounterSummary]
) RETURNS DOUBLE PRECISION
```

| Name       | Type             | Default | Required | Description                                                                                                                                                                       |
| ---------- | ---------------- | ------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `summary`  | `CounterSummary` | -       | ✔        | A counter aggregate created using [`counter_agg`][counter-agg]                                                                                                                    |
| `start`    | `TIMESTAMPTZ`    | -       | ✔        | The start of the time period to compute the delta over                                                                                                                            |
| `interval` | `INTERVAL`       | -       | ✔        | The length of the time period to compute the delta over                                                                                                                           |
| `prev`     | `CounterSummary` | -       |          | The counter aggregate from the previous interval, used to interpolate the value at `start`. If `NULL`, the first timestamp in `summary` is used as the start of the interval.     |
| `next`     | `CounterSummary` | -       |          | The counter aggregate from the next interval, used to interpolate the value at `start + interval`. If `NULL`, the last timestamp in `summary` is used as the end of the interval. |

## Returns

| Column              | Type             | Description                                                                                                                                                                                                                  |
| ------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| interpolated\_delta | DOUBLE PRECISION | The delta between the first and last points of the time interval. If exact values are missing in the raw data for the first and last points, these values are interpolated linearly from the neighboring counter aggregates. |

[counter-agg]: /api-reference/timescaledb-toolkit/counters-and-gauges/counter_agg/counter_agg

[toolkit-1.14.0]: https://github.com/timescale/timescaledb-toolkit/releases/tag/1.14.0
