BigQuery · Window function
BigQuery LEAD
LEAD(value, offset, default) returns a value from a later row of the window partition, the next row by default. Use it for the time to the next event, valid-from and valid-to ranges, and funnel steps. The last row of each partition gets the default, or NULL.
LEAD is the forward-looking twin of LAG. It reads value_expression from the row offset positions after the current one (1 by default) in the window's ORDER BY order. If that row doesn't exist, as on the last row of a partition, it returns default_expression, or NULL. ORDER BY is required and a window frame is not allowed.
A classic use is turning change events into validity ranges: LEAD(valid_from) OVER (PARTITION BY sku ORDER BY valid_from) is the date the current price stopped applying. The latest version has no successor, so give LEAD a default such as DATE '9999-12-31', or keep the NULL to mean 'still current'.
As with LAG, BigQuery doesn't accept IGNORE NULLS for LEAD, offset must be a non-negative literal or query parameter, and the default must be a constant expression. For the next non-NULL value, use FIRST_VALUE(x IGNORE NULLS) OVER (... ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING).
Syntax
LEAD(value_expression [, offset [, default_expression]]) OVER ([PARTITION BY partition_expression [, ...]] ORDER BY expression [{ASC | DESC}] [, ...])Arguments and return value
| Argument | Description |
|---|---|
value_expression | The value to read from the later row. Any type. |
offset | How many rows to look ahead. A non-negative INT64 literal or query parameter; defaults to 1. |
default_expression | Returned when there is no row at offset. A constant whose type can be coerced to the type of value_expression; defaults to NULL. |
PARTITION BY partition_expression | Keeps each entity separate, so the last row of one user doesn't read the first row of the next. |
ORDER BY expression | Required. Defines which row is next. |
Returns: The type of value_expression.
Examples
Next step and time to it
WITH funnel AS (
SELECT 'u1' AS user_id, 'view_product' AS step, TIMESTAMP '2026-11-20 10:00:00 UTC' AS event_ts UNION ALL
SELECT 'u1', 'add_to_cart', TIMESTAMP '2026-11-20 10:02:30 UTC' UNION ALL
SELECT 'u1', 'purchase', TIMESTAMP '2026-11-20 10:09:00 UTC' UNION ALL
SELECT 'u2', 'view_product', TIMESTAMP '2026-11-20 11:00:00 UTC'
)
SELECT
user_id,
step,
LEAD(step) OVER w AS next_step,
TIMESTAMP_DIFF(LEAD(event_ts) OVER w, event_ts, SECOND) AS seconds_to_next
FROM funnel
WINDOW w AS (PARTITION BY user_id ORDER BY event_ts)
ORDER BY user_id, event_ts;| user_id | step | next_step | seconds_to_next |
|---|---|---|---|
| u1 | view_product | add_to_cart | 150 |
| u1 | add_to_cart | purchase | 390 |
| u1 | purchase | NULL | NULL |
| u2 | view_product | NULL | NULL |
Valid-from and valid-to ranges
WITH price_changes AS (
SELECT 'sku-1' AS sku, 19.99 AS price, DATE '2026-01-01' AS valid_from UNION ALL
SELECT 'sku-1', 21.49, DATE '2026-06-15' UNION ALL
SELECT 'sku-1', 18.99, DATE '2026-11-20'
)
SELECT
sku,
price,
valid_from,
LEAD(valid_from, 1, DATE '9999-12-31') OVER (PARTITION BY sku ORDER BY valid_from) AS valid_to
FROM price_changes
ORDER BY valid_from;| sku | price | valid_from | valid_to |
|---|---|---|---|
| sku-1 | 19.99 | 2026-01-01 | 2026-06-15 |
| sku-1 | 21.49 | 2026-06-15 | 2026-11-20 |
| sku-1 | 18.99 | 2026-11-20 | 9999-12-31 |
Days until the next order
WITH orders AS (
SELECT 'c1' AS customer_id, DATE '2026-10-02' AS order_date UNION ALL
SELECT 'c1', DATE '2026-10-09' UNION ALL
SELECT 'c1', DATE '2026-11-20' UNION ALL
SELECT 'c2', DATE '2026-10-15'
)
SELECT
customer_id,
order_date,
DATE_DIFF(LEAD(order_date) OVER (PARTITION BY customer_id ORDER BY order_date), order_date, DAY) AS days_to_next_order
FROM orders
ORDER BY customer_id, order_date;| customer_id | order_date | days_to_next_order |
|---|---|---|
| c1 | 2026-10-02 | 7 |
| c1 | 2026-10-09 | 42 |
| c1 | 2026-11-20 | NULL |
| c2 | 2026-10-15 | NULL |
Next non-NULL value
LEAD returns the next row's value even when it is NULL. FIRST_VALUE with IGNORE NULLS over the following rows finds the next known value.
WITH tracking AS (
SELECT 1 AS seq, 'Hamburg' AS hub UNION ALL
SELECT 2, NULL UNION ALL
SELECT 3, NULL UNION ALL
SELECT 4, 'Prague'
)
SELECT
seq,
hub,
LEAD(hub) OVER (ORDER BY seq) AS next_row_hub,
FIRST_VALUE(hub IGNORE NULLS) OVER (ORDER BY seq ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) AS next_known_hub
FROM tracking
ORDER BY seq;| seq | hub | next_row_hub | next_known_hub |
|---|---|---|---|
| 1 | Hamburg | NULL | Prague |
| 2 | NULL | NULL | Prague |
| 3 | NULL | Prague | Prague |
| 4 | Prague | NULL | NULL |
Common mistakes
- Forgetting the last row. LEAD returns NULL there, and any arithmetic with it is NULL too. Pass a default or handle the NULL explicitly.
- Using IGNORE NULLS. BigQuery rejects it for LEAD; use FIRST_VALUE with IGNORE NULLS and a frame starting at
1 FOLLOWING. - Leaving out PARTITION BY. The next row may then belong to another user or product.
- Expecting LEAD to find the next day. With gaps in the data it returns the next existing row; check the distance with DATE_DIFF.
- Ordering by a non-unique column. With ties, which row counts as next is arbitrary; add a tie-breaker to ORDER BY.
Cost and performance
- Without PARTITION BY, the whole input is ordered on a single worker. Partition by the entity the sequence belongs to, such as the user, order or product.
FAQ
How do I get the next row's value in BigQuery?
Use LEAD(column) OVER (PARTITION BY entity ORDER BY timestamp). The last row of each partition gets NULL, or the default you pass as the third argument.
How do I build valid_from and valid_to ranges from change records?
Take LEAD(valid_from, 1, DATE '9999-12-31') OVER (PARTITION BY id ORDER BY valid_from) as valid_to. A row is current when valid_to equals the far-future default.
What is the difference between LEAD and LAG?
LEAD reads a later row and LAG an earlier one. Reversing the window order turns one into the other.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
