BigQuery · Window function
BigQuery LAST_VALUE
LAST_VALUE(x) returns x from the last row of the current window frame. With ORDER BY and no frame clause the frame ends at the current row, so it returns the current value, not the partition's last one; add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
The default frame is the trap. When the OVER clause has ORDER BY but no frame clause, BigQuery uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The last row of that frame is the current row, or the last of its ties, so LAST_VALUE(status) OVER (PARTITION BY order_id ORDER BY updated_at) repeats each row's own status instead of the latest one. Spell out ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, or use FIRST_VALUE(x) OVER (... ORDER BY updated_at DESC), which gives the same answer with the default frame. This is standard SQL behavior, so PostgreSQL produces the same surprise.
With IGNORE NULLS, LAST_VALUE returns the last non-NULL value in the frame. Combined with a frame that ends at the current row, that is the standard forward fill: LAST_VALUE(x IGNORE NULLS) OVER (PARTITION BY id ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) carries the last known value into the gaps. Ending the frame at 1 PRECEDING returns the previous non-NULL value, which BigQuery's LAG can't do.
ORDER BY is required in the OVER clause. If several rows tie for the last position, which one is returned is arbitrary; add a tie-breaker to ORDER BY.
Syntax
LAST_VALUE(value_expression [{RESPECT | IGNORE} NULLS]) OVER ([PARTITION BY partition_expression [, ...]] ORDER BY expression [{ASC | DESC}] [, ...] [window_frame_clause])Arguments and return value
| Argument | Description |
|---|---|
value_expression | The value to return from the last row of the frame. Any type. |
RESPECT NULLS | IGNORE NULLS | RESPECT NULLS, the default, can return NULL from the last row. IGNORE NULLS returns the last non-NULL value in the frame. |
PARTITION BY partition_expression | Restarts the window for each group. Optional. |
ORDER BY expression | Required. Defines which row is last. |
window_frame_clause | Usually needed. Without it the frame ends at the current row; ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING covers the whole partition. |
Returns: The type of value_expression.
Examples
The default frame vs the whole partition
With the default frame each row sees only itself and the rows before it, so default_frame copies the current status. The explicit frame and FIRST_VALUE with DESC both return the latest status.
WITH order_events AS (
SELECT 1001 AS order_id, 'created' AS status, TIMESTAMP '2026-11-20 09:00:00 UTC' AS updated_at UNION ALL
SELECT 1001, 'paid', TIMESTAMP '2026-11-20 09:05:00 UTC' UNION ALL
SELECT 1001, 'shipped', TIMESTAMP '2026-11-21 16:40:00 UTC'
)
SELECT
status,
LAST_VALUE(status) OVER (PARTITION BY order_id ORDER BY updated_at) AS default_frame,
LAST_VALUE(status) OVER (
PARTITION BY order_id ORDER BY updated_at
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS latest_status,
FIRST_VALUE(status) OVER (PARTITION BY order_id ORDER BY updated_at DESC) AS latest_via_first_value
FROM order_events
ORDER BY updated_at;| status | default_frame | latest_status | latest_via_first_value |
|---|---|---|---|
| created | created | shipped | shipped |
| paid | paid | shipped | shipped |
| shipped | shipped | shipped | shipped |
Forward-fill missing readings
WITH readings AS (
SELECT 'sensor-1' AS sensor_id, TIMESTAMP '2026-11-23 08:00:00 UTC' AS ts, 21.5 AS temp_c UNION ALL
SELECT 'sensor-1', TIMESTAMP '2026-11-23 08:05:00 UTC', NULL UNION ALL
SELECT 'sensor-1', TIMESTAMP '2026-11-23 08:10:00 UTC', NULL UNION ALL
SELECT 'sensor-1', TIMESTAMP '2026-11-23 08:15:00 UTC', 22.0
)
SELECT
ts,
temp_c,
LAST_VALUE(temp_c IGNORE NULLS) OVER (
PARTITION BY sensor_id ORDER BY ts
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS temp_c_filled
FROM readings
ORDER BY ts;| ts | temp_c | temp_c_filled |
|---|---|---|
| 2026-11-23 08:00:00 UTC | 21.5 | 21.5 |
| 2026-11-23 08:05:00 UTC | NULL | 21.5 |
| 2026-11-23 08:10:00 UTC | NULL | 21.5 |
| 2026-11-23 08:15:00 UTC | 22.0 | 22.0 |
Closing value per month
PARTITION BY month with a full frame gives the last value of each month; SELECT DISTINCT collapses it to one row per month.
WITH prices AS (
SELECT DATE '2026-10-30' AS day, 101.2 AS close UNION ALL
SELECT DATE '2026-10-31', 102.8 UNION ALL
SELECT DATE '2026-11-02', 99.5 UNION ALL
SELECT DATE '2026-11-03', 100.4
)
SELECT DISTINCT
DATE_TRUNC(day, MONTH) AS month,
LAST_VALUE(close) OVER (
PARTITION BY DATE_TRUNC(day, MONTH) ORDER BY day
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS month_close
FROM prices
ORDER BY month;| month | month_close |
|---|---|
| 2026-10-01 | 102.8 |
| 2026-11-01 | 100.4 |
Common mistakes
- Relying on the default frame. With ORDER BY and no frame clause, LAST_VALUE returns the current row's value (or that of its last tie). Add
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. - Using RANGE instead of ROWS for a forward fill. A RANGE frame that ends at CURRENT ROW also contains the rows that tie with the current row, so tied rows can pick up each other's values. Use ROWS and a unique ORDER BY.
- Forgetting IGNORE NULLS when forward-filling. Without it, LAST_VALUE over a frame ending at the current row just returns the current NULL.
- Expecting one row per group. LAST_VALUE returns a value on every row; collapse with SELECT DISTINCT, or use MAX_BY in a GROUP BY query.
- Omitting ORDER BY. LAST_VALUE needs it, and BigQuery rejects
LAST_VALUE(x) OVER (PARTITION BY id).
Cost and performance
- As with other window functions, a window without PARTITION BY is processed on a single worker. Partition by the entity (order, sensor, account) so the work spreads across workers.
FAQ
Why does LAST_VALUE return the current row's value?
Because the default frame with ORDER BY is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which ends at the current row. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, or use FIRST_VALUE with the order reversed.
How do I forward-fill NULL values in BigQuery?
Use LAST_VALUE(x IGNORE NULLS) OVER (PARTITION BY id ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Each NULL takes the most recent non-NULL value before it.
How do I get the last value per group as one row?
In a GROUP BY query use MAX_BY: MAX_BY(x, ts). With window functions, use LAST_VALUE with a full frame and SELECT DISTINCT.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
