BigQuery · Window function
BigQuery FIRST_VALUE
FIRST_VALUE(x) returns x from the first row of the current window frame, such as each session's landing page or a series' starting value. With IGNORE NULLS it returns the first non-NULL value; an ORDER BY in the OVER clause is required.
FIRST_VALUE reads value_expression from the first row of the window frame. With the default frame (ORDER BY and no frame clause means RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) the frame always starts at the beginning of the partition, so FIRST_VALUE returns the partition's first value on every row. That makes it safer than LAST_VALUE, whose default frame ends at the current row.
NULLs are respected by default: if the first row's value is NULL, the result is NULL. FIRST_VALUE(x IGNORE NULLS) skips them and returns the first non-NULL value in the frame. With the frame ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING it returns the current value or, if that is NULL, the next known one, which back-fills gaps.
If several rows tie for first place in the ORDER BY, which one is first is arbitrary. FIRST_VALUE is a window function only; for one value per group in a GROUP BY query, use MIN_BY or ARRAY_AGG(x ORDER BY ts LIMIT 1)[OFFSET(0)].
Syntax
FIRST_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 first row of the frame. Any type. |
RESPECT NULLS | IGNORE NULLS | RESPECT NULLS, the default, can return NULL from the first row. IGNORE NULLS returns the first 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 first. |
window_frame_clause | Optional ROWS or RANGE frame. With ORDER BY and no frame, the frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. |
Returns: The type of value_expression.
Examples
Landing page of each session on every row
WITH page_views AS (
SELECT 's1' AS session_id, '/blog/bigquery-costs' AS page, TIMESTAMP '2026-11-20 10:00:00 UTC' AS viewed_at UNION ALL
SELECT 's1', '/pricing', TIMESTAMP '2026-11-20 10:03:00 UTC' UNION ALL
SELECT 's1', '/signup', TIMESTAMP '2026-11-20 10:06:00 UTC' UNION ALL
SELECT 's2', '/docs', TIMESTAMP '2026-11-20 12:00:00 UTC'
)
SELECT
session_id,
page,
FIRST_VALUE(page) OVER (PARTITION BY session_id ORDER BY viewed_at) AS landing_page
FROM page_views
ORDER BY session_id, viewed_at;| session_id | page | landing_page |
|---|---|---|
| s1 | /blog/bigquery-costs | /blog/bigquery-costs |
| s1 | /pricing | /blog/bigquery-costs |
| s1 | /signup | /blog/bigquery-costs |
| s2 | /docs | /docs |
Change since the first day
WITH daily AS (
SELECT DATE '2026-11-01' AS day, 1200 AS mrr UNION ALL
SELECT DATE '2026-11-02', 1260 UNION ALL
SELECT DATE '2026-11-03', 1320
)
SELECT
day,
mrr,
mrr - FIRST_VALUE(mrr) OVER (ORDER BY day) AS change_since_start
FROM daily
ORDER BY day;| day | mrr | change_since_start |
|---|---|---|
| 2026-11-01 | 1200 | 0 |
| 2026-11-02 | 1260 | 60 |
| 2026-11-03 | 1320 | 120 |
IGNORE NULLS and back-filling
The first row has no source, so plain FIRST_VALUE is NULL everywhere. IGNORE NULLS over the whole partition finds the first known source; over the current and following rows it back-fills each gap with the next known value.
WITH signups AS (
SELECT 1 AS seq, NULL AS utm_source UNION ALL
SELECT 2, 'newsletter' UNION ALL
SELECT 3, NULL UNION ALL
SELECT 4, 'google'
)
SELECT
seq,
utm_source,
FIRST_VALUE(utm_source) OVER (ORDER BY seq) AS first_value_default,
FIRST_VALUE(utm_source IGNORE NULLS) OVER (ORDER BY seq ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS first_known,
FIRST_VALUE(utm_source IGNORE NULLS) OVER (ORDER BY seq ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS backfilled
FROM signups
ORDER BY seq;| seq | utm_source | first_value_default | first_known | backfilled |
|---|---|---|---|---|
| 1 | NULL | NULL | newsletter | newsletter |
| 2 | newsletter | NULL | newsletter | newsletter |
| 3 | NULL | NULL | newsletter | |
| 4 | NULL | newsletter |
Common mistakes
- Getting NULL from the first row. If the earliest row has no value, FIRST_VALUE returns NULL; add IGNORE NULLS.
- Omitting ORDER BY.
FIRST_VALUE(x) OVER (PARTITION BY id)fails withWindow ORDER BY is required for analytic function first_value. - Ordering by a column with ties. If two rows share the first timestamp, either value can be returned; add a tie-breaker.
- Expecting one row per group. FIRST_VALUE returns a value on every row; add SELECT DISTINCT, or use MIN_BY in a GROUP BY query.
- Assuming LAST_VALUE mirrors FIRST_VALUE. With the default frame, FIRST_VALUE sees the partition's first row, but LAST_VALUE only sees up to the current row; give LAST_VALUE an explicit frame.
Cost and performance
- Partition the window by the entity you look at (session, user, order). Without PARTITION BY the whole input is sorted on one worker, which fails on large tables.
FAQ
How do I get the first value per group in BigQuery?
On every row: FIRST_VALUE(x) OVER (PARTITION BY group_col ORDER BY ts). One row per group: MIN_BY(x, ts) with GROUP BY. See MIN_BY.
Why does FIRST_VALUE return NULL?
The first row of the frame has a NULL value. Use FIRST_VALUE(x IGNORE NULLS) to skip NULLs.
What is the difference between FIRST_VALUE and MIN?
MIN(x) returns the smallest x. FIRST_VALUE(x) OVER (ORDER BY ts) returns x from the earliest row by ts, whatever its size.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
