Kater SQL Functions Calculation context
MOVING_AVERAGE
Averages a value over this row and the rows before it, in the order you give.
Family: Calculation context
Syntax
MOVING_AVERAGE(value, rows => n) OVER ([PARTITION BY ...] ORDER BY ...)Arguments
| Argument | Description |
|---|---|
value | The number to average: a measure or an expression. |
rows | How many rows to average, this row included, such as 3. |
Returns
The average of this row and the rows before it.
Usage notes
- The first rows have fewer rows before them, so their average uses fewer rows.
- Write
OVERwithORDER BYafter the call. It counts rows, not calendar periods.
Example
This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.
SELECT orders.order_month AS "Order Month", EVAL(orders.sum__quantity) AS "Units", MOVING_AVERAGE(orders.sum__quantity, rows => 3) OVER (ORDER BY orders.order_month) AS "Units, 3-Month Average"FROM kater_dataset.orders AS ordersWHERE KATER__IS_ON_OR_AFTER(orders.order_date, DATE '2026-05-01')GROUP BY orders.order_monthORDER BY 1| Order Month | Units | Units, 3-Month Average |
|---|---|---|
| 2026-05-01 | 441 | 441.0 |
| 2026-06-01 | 403 | 422.0 |
| 2026-07-01 | 438 | 427.33 |
| 2026-08-01 | 454 | 431.67 |
| 2026-09-01 | 442 | 444.67 |
See also
- Change a measure’s context
- Other functions in Calculation context:
TOTAL,SHARE_OF_TOTAL,VALUE_FOR,CALCULATE_ACROSS,CALCULATE_BY,CALCULATE_PER,RATIO,RUNNING_TOTAL,CUMULATIVE_SHARE,BY,ACROSS,IGNORE_FILTERS