Google BigQuery

BigQuery window functions: OVER, PARTITION BY, frames and LAG, with examples

A window function calculates across a set of related rows and keeps every row, unlike GROUP BY. This guide runs the common ones on one small table and shows the result: running totals, share of total, LAG for day-over-day change, moving averages with a frame, LAST_VALUE with IGNORE NULLS, and QUALIFY to filter on the result. Each comes with the trap that changes the number without an error.

Google BigQueryAlyona SamovarVadym Kramarenko6 min read

BigQuery window functions: OVER, PARTITION BY, frames and LAG, with examples

A window function calculates across a set of related rows, the window, and returns a value for every row. GROUP BY collapses four days into one total; SUM(revenue) OVER (...) keeps the four days and puts the running total next to each.

Every window function has the same shape:

function(...) OVER (
  PARTITION BY ...   -- which rows belong together
  ORDER BY ...       -- in what order
  ROWS BETWEEN ...   -- which of them, relative to the current row
)
Family Functions Guide
Aggregate SUM, AVG, COUNT, MIN, MAX with OVER this page
Navigation LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE this page, and navigation functions
Numbering ROW_NUMBER, RANK, DENSE_RANK, NTILE, PERCENT_RANK numbering functions

Note: Originally published in April 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

WITH sales AS (
  SELECT DATE '2026-09-01' AS day, 'web' AS channel, 100 AS revenue
  UNION ALL SELECT DATE '2026-09-02', 'web', 140
  UNION ALL SELECT DATE '2026-09-03', 'web', NULL
  UNION ALL SELECT DATE '2026-09-04', 'web', 120
  UNION ALL SELECT DATE '2026-09-01', 'ads', 60
  UNION ALL SELECT DATE '2026-09-02', 'ads', 60
  UNION ALL SELECT DATE '2026-09-03', 'ads', 90
  UNION ALL SELECT DATE '2026-09-04', 'ads', 30
)

Two channels over four days. Web has no revenue recorded on 3 September.

Running total and share of total

Running total and share of total

SELECT
  channel, day, revenue,
  SUM(revenue) OVER (
    PARTITION BY channel ORDER BY day) AS running,
  ROUND(revenue / SUM(revenue) OVER (
    PARTITION BY channel), 3) AS share
FROM sales;
channel day revenue running share
web 2026-09-01 100 100 0.278
web 2026-09-02 140 240 0.389
web 2026-09-03 NULL 240 NULL
web 2026-09-04 120 360 0.333

The difference is ORDER BY. With it, the window is “this row and everything before it”, so SUM is a running total. Without it, the window is the whole partition, so SUM is the channel total and revenue / total is the share. OVER () with nothing inside is the whole result: COUNT(*) OVER () returned 8 on every row.

Hand this to your AI

Your running totals and shares of total come from GROUP BY queries joined back, and every join is one more place to go wrong.

Paste this into your AI assistant. It rewrites those queries on your BigQuery tables as window functions: running totals, share of total, day-over-day change and moving averages, with no join back.

The prompt

rewrite my BigQuery daily revenue by channel query with window functions: running total, share of the day's total, change from the previous day and a weekly moving average, with no self-join

LAG and LEAD: compare with the previous row

LAG and LEAD: compare with the previous row

SELECT
  channel, day, revenue,
  LAG(revenue) OVER (
    PARTITION BY channel ORDER BY day) AS prev_day,
  revenue - LAG(revenue) OVER (
    PARTITION BY channel ORDER BY day) AS change
FROM sales;
channel day revenue prev_day change
ads 2026-09-01 60 NULL NULL
ads 2026-09-02 60 60 0
ads 2026-09-03 90 60 30
ads 2026-09-04 30 90 -60
web 2026-09-04 120 NULL NULL

Two things to know:

  • LAG takes the previous row, not the previous day. If a day is missing from the table, LAG compares with whatever came before it. For a strict day-over-day change, join to a calendar first so every day has a row.
  • A NULL in the previous row gives a NULL change. Web on 4 September has no change because 3 September has no revenue. LAG(revenue IGNORE NULLS) fails with “IGNORE NULLS and RESPECT NULLS are not allowed for analytic function LAG”; use LAST_VALUE(revenue IGNORE NULLS) over a frame that ends one row back if you want the last known value.

LEAD looks forward instead. LAG(revenue, 7) looks seven rows back, and LAG(revenue, 1, 0) returns 0 instead of NULL on the first row.

Moving average: the frame

Moving average: the frame

SELECT
  channel, day, revenue,
  ROUND(AVG(revenue) OVER (
    PARTITION BY channel ORDER BY day
    ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg3
FROM sales;
channel day revenue avg3
web 2026-09-01 100 100
web 2026-09-02 140 120
web 2026-09-03 NULL 120
web 2026-09-04 120 130

ROWS BETWEEN 2 PRECEDING AND CURRENT ROW is a three-row window. On the first two days it has fewer rows, so the “three-day” average is really one or two days: filter those out or label them. And AVG skips the NULL, so 4 September averages two values, not three.

The default frame catches people out

When there is an ORDER BY and no frame, the frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Two consequences, both from our test:

Expression Result on ads, 1 September
LAST_VALUE(revenue) OVER (PARTITION BY channel ORDER BY day) 60, the current row
LAST_VALUE(revenue) OVER (... ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) 90, the last row
SUM(revenue) OVER (PARTITION BY channel ORDER BY revenue) on a 60 120, both 60s at once

LAST_VALUE without a full frame returns the current row, because the window ends there. And RANGE treats rows with the same ORDER BY value as one step, so the running total jumps by both tied rows together. Write the frame out whenever the answer depends on it.

The last known value: LAST_VALUE IGNORE NULLS

The last known value: LAST_VALUE IGNORE NULLS

SELECT
  channel, day, revenue,
  LAST_VALUE(revenue IGNORE NULLS) OVER (
    PARTITION BY channel ORDER BY day) AS last_known
FROM sales;

For web on 3 September this returned 140, the value from the day before, instead of NULL. That fills gaps in a series such as a price list or an exchange rate. Here the default frame is what you want: everything up to the current row.

Filter on a window function: QUALIFY

Filter on a window function: QUALIFY

A window function cannot go in WHERE, because WHERE runs before it. QUALIFY runs after:

SELECT channel, day, revenue
FROM sales
QUALIFY ROW_NUMBER() OVER (
  PARTITION BY channel ORDER BY revenue DESC) <= 2;

This returned the two best days per channel: ads on 3 and 1 September, web on 2 and 4 September. The window is still computed over all rows; QUALIFY only decides which to keep. The order of clauses is in WHERE, HAVING and QUALIFY.

Window function or GROUP BY

Window function or GROUP BY

  • One row per group: GROUP BY with an aggregate. See aggregate functions.
  • Every row, with a group value beside it: a window function.
  • Both: aggregate first in a subquery or CTE, then apply the window to the result. The CTE guide shows the pattern.

On a large table, a window over a partition with millions of rows can fail with “Resources exceeded”, because a partition is processed in one place. Partition by something finer, or filter first.

Where window functions matter in reporting

Where window functions matter in reporting

Day-over-day change, running totals and share of channel are the numbers reports are made of, and they are easy to get subtly wrong: a missing day, a default frame, a tie. Write them once, check them once, and let every report read the same result. In OWOX Data Marts that query is published as a Data Mart, run as written in your own BigQuery project, and delivered to sheets and dashboards. OWOX Data Marts does not transform the data. All function families are mapped in the BigQuery functions overview.

Hand this to your AI

Your running totals and shares of total come from GROUP BY queries joined back, and every join is one more place to go wrong.

Paste this into your AI assistant. It rewrites those queries on your BigQuery tables as window functions: running totals, share of total, day-over-day change and moving averages, with no join back.

The prompt

rewrite my BigQuery daily revenue by channel query with window functions: running total, share of the day's total, change from the previous day and a weekly moving average, with no self-join

FAQ

Frequently Asked Questions

What are the best practices for window functions in BigQuery?

Write the frame out whenever the result depends on it, because the default frame ends at the current row. Break ties in ORDER BY with a second column. Filter before the window where you can, and use QUALIFY to filter on its result. Partition finely enough that no single partition holds millions of rows.

Which window functions does BigQuery have?

Aggregate functions used with OVER (SUM, AVG, COUNT, MIN, MAX), navigation functions (LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE) and numbering functions (ROW_NUMBER, RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST, NTILE).

What is the difference between GROUP BY and a window function?

GROUP BY returns one row per group. A window function returns every row and adds a value calculated over related rows, such as a running total or the channel total beside each day.

What is a practical example of a window function in BigQuery?

Day-over-day change: revenue - LAG(revenue) OVER (PARTITION BY channel ORDER BY day). LAG takes the previous row, not the previous calendar day, so join to a calendar first if days can be missing.

How does the OVER clause work in BigQuery?

PARTITION BY groups the rows, ORDER BY sorts them, and the frame (ROWS BETWEEN ...) picks which rows around the current one are used. With ORDER BY and no frame, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which makes SUM a running total.

What are window functions in BigQuery?

Functions that calculate across a set of related rows, the window, and return a value for every row instead of collapsing them. They are written with an OVER clause.

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.