Google BigQuery

BigQuery navigation functions: LAG, LEAD, LAST_VALUE and PERCENTILE_CONT

Navigation functions read a value from another row of the same window: the previous one, the next one, the first, the last, the n-th, or the one at a percentile. This guide runs all seven BigQuery navigation functions on one seven-row table and shows the result, including why LAST_VALUE returns the current row by default, where IGNORE NULLS is allowed, and the exact PERCENTILE_CONT and PERCENTILE_DISC syntax.

Google BigQueryAlyona SamovarVadym Kramarenko11 min read

BigQuery navigation functions: LAG, LEAD, LAST_VALUE and PERCENTILE_CONT

A navigation function reads a value from another row of the same window: the row before, the row after, the first or last row, the n-th row, or the row at a percentile. BigQuery has seven. All of them are window functions, so every one needs an OVER (...) clause, and none of them collapses rows the way GROUP BY does.

Function Returns Watch out for
LAG(x, n, default) x from n rows back no IGNORE NULLS; default only fills missing rows
LEAD(x, n, default) x from n rows ahead same as LAG
FIRST_VALUE(x) x from the first row of the frame NULL first row returns NULL
LAST_VALUE(x) x from the last row of the frame with ORDER BY, the frame ends at the current row
NTH_VALUE(x, n) x from the n-th row of the frame same frame trap
PERCENTILE_CONT(x, p) interpolated percentile window only; no ORDER BY inside OVER
PERCENTILE_DISC(x, p) an actual value at the percentile same syntax as PERCENTILE_CONT

Note: Originally published in January 2024. Fully rewritten in October 2026: every result below was produced by running the query in BigQuery on 16 October 2026.

Reading to get this done?

The table used in every example

The table used in every example

Paste this before any query below and it runs as is:

WITH sales AS (
  SELECT 'web' AS channel, DATE '2026-01-01' AS month, 100 AS revenue
  UNION ALL SELECT 'web', DATE '2026-02-01', 120
  UNION ALL SELECT 'web', DATE '2026-03-01', NULL
  UNION ALL SELECT 'web', DATE '2026-04-01', 150
  UNION ALL SELECT 'ads', DATE '2026-01-01', 80
  UNION ALL SELECT 'ads', DATE '2026-02-01', 60
  UNION ALL SELECT 'ads', DATE '2026-03-01', 90
)

Monthly revenue for two channels. The web channel has four months, one of them with no revenue recorded; the ads channel has three.

LAG and LEAD: the row before and the row after

LAG and LEAD: the row before and the row after

SELECT
  channel, month, revenue,
  LAG(revenue) OVER w AS prev_revenue,
  revenue - LAG(revenue) OVER w AS change,
  LEAD(revenue, 1, 0) OVER w AS next_revenue,
  LAG(revenue, 2) OVER w AS two_back
FROM sales
WINDOW w AS (PARTITION BY channel ORDER BY month)
ORDER BY channel, month;
channel month revenue prev_revenue change next_revenue two_back
ads 2026-01-01 80 NULL NULL 60 NULL
ads 2026-02-01 60 80 -20 90 NULL
ads 2026-03-01 90 60 30 0 80
web 2026-01-01 100 NULL NULL 120 NULL
web 2026-02-01 120 100 20 NULL NULL
web 2026-03-01 NULL 120 NULL 150 100
web 2026-04-01 150 NULL NULL 0 120

The syntax is LAG(value, offset, default) OVER (PARTITION BY ... ORDER BY ...). The offset is 1 when you leave it out, and the default is NULL. PARTITION BY channel restarts the count for each channel, so the first ads month does not look back into web. The named WINDOW w saves repeating the same OVER clause four times.

Three things in that table:

  • The default only fills rows that do not exist. LEAD(revenue, 1, 0) returned 0 for the last month of each channel, where there is no next row. For web in February it returned NULL, because the March row exists and its revenue is NULL. The default does not replace NULL values.
  • One NULL breaks two comparisons. Web’s March change is NULL, and so is April’s, because April’s previous row is the empty March.
  • The offset must be a constant, zero or more. LAG(revenue, -1) fails with “Argument 2 to LAG must be at least 0; got -1.” To look forward, use LEAD. The default must be coercible to the value’s type: LAG(revenue, 1, 2.5) runs and returns FLOAT64, but LAG(revenue, 1, 'none') on an INT64 column fails with “No matching signature for analytic function LAG”.

LAG and LEAD also reject a frame clause. Adding ROWS BETWEEN 1 PRECEDING AND CURRENT ROW returns “Window framing clause is not allowed for analytic function lag”: the offset already says which row to read.

Hand this to your AI

Month-over-month numbers come out wrong because LAG skipped a missing month or LAST_VALUE returned the current row.

Paste this into your AI assistant. It writes the LAG, LEAD and LAST_VALUE queries for your own BigQuery tables, with the window frame set right and empty months filled in.

The prompt

write me BigQuery SQL that compares each month's revenue with the previous month for every channel, filling months with no data so LAG doesn't skip them

LAG has no IGNORE NULLS: use LAST_VALUE instead

LAG has no IGNORE NULLS: use LAST_VALUE instead

To compare April with the last month that had revenue, you want “the previous non-NULL value”. The obvious syntax does not exist in BigQuery:

LAG(revenue IGNORE NULLS) OVER (ORDER BY month)
-- Error: IGNORE NULLS and RESPECT NULLS are not allowed for analytic function LAG

LAST_VALUE does accept IGNORE NULLS, and with a frame that stops one row before the current one it behaves like a NULL-skipping LAG:

SELECT
  channel, month, revenue,
  LAG(revenue) OVER (PARTITION BY channel ORDER BY month) AS lag_plain,
  LAST_VALUE(revenue IGNORE NULLS) OVER (
    PARTITION BY channel ORDER BY month
    ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS prev_known,
  LAST_VALUE(revenue IGNORE NULLS) OVER (
    PARTITION BY channel ORDER BY month) AS filled
FROM sales
WHERE channel = 'web'
ORDER BY month;
channel month revenue lag_plain prev_known filled
web 2026-01-01 100 NULL NULL 100
web 2026-02-01 120 100 100 120
web 2026-03-01 NULL 120 120 120
web 2026-04-01 150 NULL 120 150

prev_known gives April 120, the last month that had a value, where LAG gave NULL. filled is the forward-fill pattern: each row takes the latest non-NULL value up to and including itself, so the empty March shows 120. Whether a gap should carry the previous value forward is a business decision; for revenue it usually should not, for a price or a status it usually should.

FIRST_VALUE, LAST_VALUE, NTH_VALUE, PERCENTILE_CONT and PERCENTILE_DISC all accept IGNORE NULLS or RESPECT NULLS; LAG and LEAD accept neither, according to the navigation functions reference (read in October 2026).

FIRST_VALUE and LAST_VALUE: the default frame trap

FIRST_VALUE and LAST_VALUE: the default frame trap

SELECT
  channel, month, revenue,
  FIRST_VALUE(revenue) OVER (
    PARTITION BY channel ORDER BY month) AS first_rev,
  LAST_VALUE(revenue) OVER (
    PARTITION BY channel ORDER BY month) AS last_default,
  LAST_VALUE(revenue) OVER (
    PARTITION BY channel ORDER BY month
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_full
FROM sales
ORDER BY channel, month;
channel month revenue first_rev last_default last_full
ads 2026-01-01 80 80 80 90
ads 2026-02-01 60 80 60 90
ads 2026-03-01 90 80 90 90
web 2026-01-01 100 100 100 150
web 2026-02-01 120 100 120 150
web 2026-03-01 NULL 100 NULL 150
web 2026-04-01 150 100 150 150

last_default is just a copy of revenue. That is not a bug. When OVER has an ORDER BY and no frame clause, BigQuery uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (window function calls, read in October 2026). The frame ends at the current row, so the last row of the frame is the current row.

To get the last value of the whole partition, say so: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. Then every row gets 150 for web and 90 for ads.

FIRST_VALUE looks correct with the default frame, because the frame always starts at the first row. It has its own trap: if the first row’s value is NULL, the result is NULL for the whole partition. Write FIRST_VALUE(revenue IGNORE NULLS) to skip it.

Because the default frame is RANGE, rows with the same ORDER BY value are peers and are all inside the frame together. If two rows share a month, LAST_VALUE with the default frame can return the other one. Order by something unique, or add a tie-breaker column.

NTH_VALUE: the n-th row of the frame

NTH_VALUE: the n-th row of the frame

SELECT
  channel, month, revenue,
  NTH_VALUE(revenue, 2) OVER (
    PARTITION BY channel ORDER BY month) AS second_default,
  NTH_VALUE(revenue, 3) OVER full_w AS third,
  NTH_VALUE(revenue, 3 IGNORE NULLS) OVER full_w AS third_known
FROM sales
WINDOW full_w AS (PARTITION BY channel ORDER BY month
  ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
ORDER BY channel, month;
channel month revenue second_default third third_known
ads 2026-01-01 80 NULL 90 90
ads 2026-02-01 60 60 90 90
ads 2026-03-01 90 60 90 90
web 2026-01-01 100 NULL NULL 150
web 2026-02-01 120 120 NULL 150
web 2026-03-01 NULL 120 NULL 150
web 2026-04-01 150 120 NULL 150
  • Same frame trap. With the default frame, January has only one row in its frame, so there is no second row and the result is NULL.
  • NULLs count as rows. Web’s third month is the empty March, so third is NULL. With IGNORE NULLS the third non-NULL value is April’s 150.
  • NTH_VALUE(x, 1) is the same as FIRST_VALUE(x). There is no default argument: a missing row returns NULL.

PERCENTILE_CONT and PERCENTILE_DISC: the syntax that works

PERCENTILE_CONT and PERCENTILE_DISC: the syntax that works

These two are the most searched and the most often written wrong. In BigQuery they are window functions only. The value and the percentile both go inside the parentheses, and the OVER clause takes PARTITION BY but no ORDER BY:

PERCENTILE_CONT(value, percentile [RESPECT NULLS | IGNORE NULLS]) OVER ([PARTITION BY ...])
PERCENTILE_DISC(value, percentile [RESPECT NULLS | IGNORE NULLS]) OVER ([PARTITION BY ...])
SELECT DISTINCT
  channel,
  PERCENTILE_CONT(revenue, 0.5) OVER (PARTITION BY channel) AS median_cont,
  PERCENTILE_DISC(revenue, 0.5) OVER (PARTITION BY channel) AS median_disc,
  PERCENTILE_CONT(revenue, 0.5 RESPECT NULLS)
    OVER (PARTITION BY channel) AS median_cont_nulls,
  PERCENTILE_CONT(revenue, 0.9) OVER (PARTITION BY channel) AS p90_cont,
  PERCENTILE_DISC(revenue, 0.9) OVER (PARTITION BY channel) AS p90_disc
FROM sales
ORDER BY channel;
channel median_cont median_disc median_cont_nulls p90_cont p90_disc
ads 80.0 80 80.0 88.0 90
web 120.0 120 110.0 144.0 150

SELECT DISTINCT is there because a window function returns a value on every row; without it you get the same median repeated on each month.

  • CONT interpolates, DISC picks a real value. Web’s known values are 100, 120 and 150. The 90th percentile sits 80% of the way from 120 to 150, so PERCENTILE_CONT returns 144.0. PERCENTILE_DISC returns 150, the first value whose cumulative share reaches 0.9. Across the whole table (six known values: 60, 80, 90, 100, 120, 150) with OVER (), the medians differ the familiar way: PERCENTILE_CONT returned 95.0 and PERCENTILE_DISC returned 90.
  • NULLs are ignored by default, the opposite of FIRST_VALUE and LAST_VALUE. With RESPECT NULLS, the NULL sorts first and takes a place, so web’s median moves from 120.0 to 110.0, halfway between 100 and 120.
  • The return types differ. On an INT64 column, PERCENTILE_CONT returned FLOAT64 and PERCENTILE_DISC returned INT64, the type of the column.

The PERCENTILE errors you will meet

Query BigQuery says
PERCENTILE_CONT(revenue, 0.5) with GROUP BY percentile_cont aggregate function is not supported.
PERCENTILE_CONT(revenue, 0.5) OVER (PARTITION BY channel ORDER BY revenue) Window ORDER BY is not allowed for analytic function percentile_cont
PERCENTILE_DISC(0.5) OVER (PARTITION BY channel ORDER BY revenue) No matching signature for aggregate function PERCENTILE_DISC … requires at least 2 arguments
PERCENTILE_CONT(revenue, 90) OVER () Argument 2 to PERCENTILE_CONT must be between 0 and 1

The third row is the PostgreSQL and SQL Server habit of PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY x). BigQuery does not read it that way: the column is the first argument, and the sorting is implied.

If you need a percentile per group in a GROUP BY query, use APPROX_QUANTILES. APPROX_QUANTILES(revenue, 2)[OFFSET(1)] grouped by channel returned 80 for ads and 120 for web; APPROX_QUANTILES(revenue, 4) returned the quartile boundaries [60, 60, 80, 90, 90] for ads. As the name says, the result is approximate; on this seven-row table it matched the exact medians. More in statistical aggregate functions.

The RANGE error with dates

The RANGE error with dates

A frequent search that lands here is this error: “ORDER BY key must be numeric in a RANGE-based window with OFFSET PRECEDING or OFFSET FOLLOWING boundaries, but has type DATE”. It comes from a frame like ORDER BY month RANGE BETWEEN 31 PRECEDING AND CURRENT ROW. A RANGE frame with a number of preceding values needs exactly one numeric ORDER BY key. Turn the date into a day number:

SELECT channel, month, revenue,
  SUM(revenue) OVER (
    PARTITION BY channel
    ORDER BY UNIX_DATE(month)
    RANGE BETWEEN 31 PRECEDING AND CURRENT ROW) AS this_and_last_month
FROM sales
WHERE channel = 'web'
ORDER BY month;
channel month revenue this_and_last_month
web 2026-01-01 100 100
web 2026-02-01 120 220
web 2026-03-01 NULL 120
web 2026-04-01 150 150

UNIX_DATE returns days since 1970-01-01, so 31 PRECEDING means “rows up to 31 days earlier”: this month and the previous one. Unlike ROWS BETWEEN 1 PRECEDING, it does not reach back further when a month is missing from the table. For timestamps, use UNIX_SECONDS.

Where to go next

Where to go next

Month-over-month numbers that agree

Month-over-month numbers that agree

Month-over-month change is the most common use of LAG, and it is where reports drift apart: one analyst skips NULL months with LAST_VALUE ... IGNORE NULLS, another lets them break the comparison, a third fills them with zero. All three queries are correct; they answer different questions.

In OWOX Data Marts the query that makes that choice is saved once as a Data Mart, with the rule written in the column description, and every report in Google Sheets or Data Studio reads the same data model. OWOX Data Marts runs your SQL as written, in your own BigQuery project, and does not transform the data.

Hand this to your AI

Month-over-month numbers come out wrong because LAG skipped a missing month or LAST_VALUE returned the current row.

Paste this into your AI assistant. It writes the LAG, LEAD and LAST_VALUE queries for your own BigQuery tables, with the window frame set right and empty months filled in.

The prompt

write me BigQuery SQL that compares each month's revenue with the previous month for every channel, filling months with no data so LAG doesn't skip them

FAQ

Frequently Asked Questions

How does LEAD differ from LAG in BigQuery?

LAG reads a value from a row before the current one and LEAD reads it from a row after, in the order given by ORDER BY inside OVER. Both take an optional offset (1 by default) and a default value that is returned only when that row does not exist; neither accepts IGNORE NULLS or a frame clause.

Why does LAST_VALUE in BigQuery return the current row's value?

When OVER has ORDER BY and no frame clause, BigQuery uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so the last row of the frame is the current row. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to get the last value of the whole partition, for example LAST_VALUE(revenue) OVER (PARTITION BY channel ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING).

How do you use FIRST_VALUE in BigQuery?

FIRST_VALUE(x) OVER (PARTITION BY ... ORDER BY ...) returns x from the first row of each partition, for example the first month's revenue to compare every later month against. If that first value is NULL the result is NULL; write FIRST_VALUE(x IGNORE NULLS) to take the first non-NULL value instead.

What is the difference between PERCENTILE_CONT and PERCENTILE_DISC in BigQuery?

PERCENTILE_CONT interpolates between values and returns FLOAT64 for an integer column, while PERCENTILE_DISC returns an actual value from the data in the column's type. For the values 100, 120 and 150, the 90th percentile is 144.0 with PERCENTILE_CONT and 150 with PERCENTILE_DISC. Both are window functions written as PERCENTILE_CONT(value, 0.9) OVER (PARTITION BY ...), with no ORDER BY inside OVER.

What is the syntax for LAG in BigQuery?

LAG(value, offset, default) OVER (PARTITION BY ... ORDER BY ...). The offset is a non-negative integer constant and defaults to 1; the default must be the same type as the value and defaults to NULL. A common use is month-over-month change: revenue - LAG(revenue) OVER (PARTITION BY channel ORDER BY month).

Does LAG or LEAD support IGNORE NULLS in BigQuery?

No. BigQuery returns the error that IGNORE NULLS and RESPECT NULLS are not allowed for analytic function LAG. To get the previous non-NULL value, use LAST_VALUE(x IGNORE NULLS) OVER (PARTITION BY ... ORDER BY ... ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING).

What does the default value in LEAD do?

LEAD(x, 1, 0) returns 0 only when there is no next row, such as on the last row of a partition. If the next row exists but its value is NULL, LEAD still returns NULL, so wrap the result in COALESCE if NULL values should also be replaced.

Who wrote this

Alyona Samovar

Alyona Samovar Senior Digital Analyst

Alyona Samovar is a Senior Digital Analyst who spent years at OWOX helping enterprise clients design analytics systems, build BigQuery reporting pipelines, and optimize marketing measurement. She specializes in SQL-based analytics, data modeling, and turning raw marketing data into actionable insights. Alyona writes about practical analytics workflows, BigQuery best practices, and data-driven decision-making.

Vadym Kramarenko

Vadym Kramarenko Growth Marketing Manager

Vadym Kramarenko is a Growth Marketing Manager at OWOX, where he drives user acquisition and product-led growth strategies. He hosts the OWOX podcast, interviewing analytics professionals about data-driven marketing, attribution, and reporting best practices. Vadym specializes in turning complex analytics concepts into practical, actionable marketing frameworks.