Request a demo

BigQuery · Window function

BigQuery LAG

LAG(value, offset, default) returns a value from an earlier row of the window partition, the previous row by default. Use it for day-over-day changes, time since the previous event and gap detection. In BigQuery it has no IGNORE NULLS option.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

LAG looks back offset rows (1 by default) in the order set by the window's ORDER BY and returns value_expression from that row. When there is no such row, as on the first row of each partition, it returns default_expression, or NULL if you didn't give one. ORDER BY is required in the OVER clause, and a window frame is not allowed.

The previous row is not necessarily the previous day. If a date is missing, LAG compares with the last row that exists, so check the gap with DATE_DIFF or fill in the missing dates with GENERATE_DATE_ARRAY first. When two rows tie on the ORDER BY value, which one counts as previous is arbitrary; add a tie-breaker.

Snowflake and Redshift accept LAG(x) IGNORE NULLS; BigQuery rejects it. To get the last non-NULL value before the current row, use LAST_VALUE(x IGNORE NULLS) OVER (PARTITION BY ... ORDER BY ... ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING). The offset must be a non-negative literal or query parameter, and default_expression must be a constant, so LAG(price, 1, price) is rejected too.

Syntax

LAG(value_expression [, offset [, default_expression]]) OVER ([PARTITION BY partition_expression [, ...]] ORDER BY expression [{ASC | DESC}] [, ...])

Arguments and return value

ArgumentDescription
value_expressionThe value to read from the earlier row. Any type.
offsetHow many rows to look back. A non-negative INT64 literal or query parameter; defaults to 1. 0 returns the current row.
default_expressionReturned 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_expressionKeeps each entity separate, so the first row of one customer doesn't read the last row of another.
ORDER BY expressionRequired. Defines which row is previous.

Returns: The type of value_expression.

Examples

Day-over-day change

A named WINDOW avoids repeating the same OVER clause. SAFE_DIVIDE returns NULL on the first day instead of failing.

WITH daily AS (
  SELECT DATE '2026-11-01' AS day, 1200 AS revenue UNION ALL
  SELECT DATE '2026-11-02', 1350 UNION ALL
  SELECT DATE '2026-11-03', 1100 UNION ALL
  SELECT DATE '2026-11-04', 1450
)
SELECT
  day,
  revenue,
  LAG(revenue) OVER w AS prev_revenue,
  revenue - LAG(revenue) OVER w AS change,
  ROUND(SAFE_DIVIDE(revenue - LAG(revenue) OVER w, LAG(revenue) OVER w), 3) AS pct_change
FROM daily
WINDOW w AS (ORDER BY day)
ORDER BY day;
Result
dayrevenueprev_revenuechangepct_change
2026-11-011200NULLNULLNULL
2026-11-02135012001500.125
2026-11-0311001350-250-0.185
2026-11-04145011003500.318

Minutes since the previous event and session starts

A gap of more than 30 minutes, or no previous event, starts a new session.

WITH events AS (
  SELECT 'u1' AS user_id, TIMESTAMP '2026-11-20 10:00:00 UTC' AS event_ts UNION ALL
  SELECT 'u1', TIMESTAMP '2026-11-20 10:04:00 UTC' UNION ALL
  SELECT 'u1', TIMESTAMP '2026-11-20 11:30:00 UTC' UNION ALL
  SELECT 'u2', TIMESTAMP '2026-11-20 09:15:00 UTC'
)
SELECT
  user_id,
  event_ts,
  TIMESTAMP_DIFF(event_ts, LAG(event_ts) OVER w, MINUTE) AS minutes_since_prev,
  IFNULL(TIMESTAMP_DIFF(event_ts, LAG(event_ts) OVER w, MINUTE) > 30, TRUE) AS starts_session
FROM events
WINDOW w AS (PARTITION BY user_id ORDER BY event_ts)
ORDER BY user_id, event_ts;
Result
user_idevent_tsminutes_since_prevstarts_session
u12026-11-20 10:00:00 UTCNULLtrue
u12026-11-20 10:04:00 UTC4false
u12026-11-20 11:30:00 UTC86true
u22026-11-20 09:15:00 UTCNULLtrue

Offset and default value

WITH monthly AS (
  SELECT DATE '2026-09-01' AS month, 40 AS new_customers UNION ALL
  SELECT DATE '2026-10-01', 55 UNION ALL
  SELECT DATE '2026-11-01', 48
)
SELECT
  month,
  new_customers,
  LAG(new_customers, 1, 0) OVER (ORDER BY month) AS prev_month,
  LAG(new_customers, 2) OVER (ORDER BY month) AS two_months_ago
FROM monthly
ORDER BY month;
Result
monthnew_customersprev_monthtwo_months_ago
2026-09-01400NULL
2026-10-015540NULL
2026-11-01485540

Previous non-NULL value

LAG returns whatever the previous row holds, NULL included. LAST_VALUE with IGNORE NULLS and a frame that ends one row back skips the gaps.

WITH readings AS (
  SELECT 1 AS seq, 21.5 AS temp_c UNION ALL
  SELECT 2, NULL UNION ALL
  SELECT 3, 22.0 UNION ALL
  SELECT 4, NULL
)
SELECT
  seq,
  temp_c,
  LAG(temp_c) OVER (ORDER BY seq) AS prev_row_value,
  LAST_VALUE(temp_c IGNORE NULLS) OVER (ORDER BY seq ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS prev_non_null
FROM readings
ORDER BY seq;
Result
seqtemp_cprev_row_valueprev_non_null
121.5NULLNULL
2NULL21.521.5
322.0NULL21.5
4NULL22.022.0

Common mistakes

  • Assuming the previous row is the previous day. With missing dates LAG reaches further back. Compare the dates with DATE_DIFF, or generate a full calendar and LEFT JOIN the data to it first.
  • Writing LAG(x IGNORE NULLS) or LAG(x) IGNORE NULLS. The first fails with IGNORE NULLS and RESPECT NULLS are not allowed for analytic function LAG, the second with a syntax error. Use LAST_VALUE with IGNORE NULLS and a frame ending at 1 PRECEDING.
  • Forgetting PARTITION BY. Without it, the first row of one customer is compared with the last row of the previous customer.
  • Using a column as the default. LAG(price, 1, price) fails because the default must be a constant; write COALESCE(LAG(price) OVER (...), price).
  • Adding a frame clause. LAG(x) OVER (ORDER BY day ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) fails with Window framing clause is not allowed for analytic function lag.

Cost and performance

  • LAG replaces the self-join on t1.day = DATE_ADD(t2.day, INTERVAL 1 DAY), which Google's performance guide recommends avoiding in favor of window functions.
  • Partition by the entity (user, pipeline, product) whenever the comparison is per entity. Without PARTITION BY the whole input is ordered on a single worker, which fails on large tables.

FAQ

How do I get the previous row's value in BigQuery?

Use LAG(column) OVER (PARTITION BY entity ORDER BY timestamp). The first row of each partition gets NULL, or the default you pass as the third argument.

How do I calculate the difference from the previous row?

Subtract: value - LAG(value) OVER (ORDER BY day). For a percentage change use SAFE_DIVIDE(value - LAG(value) OVER w, LAG(value) OVER w), which returns NULL instead of failing when the previous value is 0. See SAFE_DIVIDE.

Does LAG support IGNORE NULLS in BigQuery?

No. Use LAST_VALUE(x IGNORE NULLS) OVER (ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), which returns the most recent non-NULL value before the current row.

What is the difference between LAG and LEAD?

LAG reads an earlier row and LEAD a later one. LAG(x) OVER (ORDER BY t) returns the same values as LEAD(x) OVER (ORDER BY t DESC).

Errors you may hit

More window functions

All BigQuery window functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.