Request a demo

BigQuery · Window function

BigQuery PERCENTILE_DISC

PERCENTILE_DISC(x, p) OVER (...) returns the first value of x whose cumulative distribution reaches p, so the answer is always a value from the data. It works on any orderable type, including dates and strings, and like PERCENTILE_CONT it is a window function only in BigQuery.

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

PERCENTILE_DISC sorts the partition and returns the first value whose cumulative distribution, the share of rows less than or equal to it, is at least p. It never interpolates: for 10, 20, 30, 40 and p = 0.5 it returns 20, where PERCENTILE_CONT returns 25. Because it only needs an ordering, the values can be of any orderable type, so a median DATE, TIMESTAMP or STRING works.

Like PERCENTILE_CONT, it is a window function only. The OVER clause may contain PARTITION BY but no ORDER BY or frame, the percentile must be a literal or query parameter between 0 and 1, and the result repeats on every row, so add SELECT DISTINCT for one row per group. NULLs are ignored unless you add RESPECT NULLS, in which case they sort first.

Use it when the answer has to be an actual observation: an order amount that exists, a typical plan name, the date by which half of a cohort signed up. For large tables, APPROX_QUANTILES also returns observed values and runs as an ordinary aggregate.

Syntax

PERCENTILE_DISC(value_expression, percentile [{RESPECT | IGNORE} NULLS]) OVER ([PARTITION BY partition_expression [, ...]])

Arguments and return value

ArgumentDescription
value_expressionAny orderable type: numbers, strings, dates, timestamps.
percentileA NUMERIC, BIGNUMERIC or FLOAT64 literal or query parameter between 0 and 1.
RESPECT NULLS | IGNORE NULLSNULLs are ignored by default. With RESPECT NULLS they are included and sort first.
PARTITION BY partition_expressionOne percentile per group. Optional. ORDER BY and frame clauses are not allowed.

Returns: The type of value_expression.

Examples

Discrete vs interpolated median

SELECT DISTINCT
  PERCENTILE_DISC(amount, 0.5) OVER () AS median_order,
  PERCENTILE_CONT(amount, 0.5) OVER () AS interpolated_median
FROM UNNEST([10, 20, 30, 40]) AS amount;
Result
median_orderinterpolated_median
2025.0

Median signup date per channel

PERCENTILE_DISC works on DATE values. With two dates, the first one already covers half of the rows.

WITH signups AS (
  SELECT 'paid_search' AS channel, DATE '2026-11-02' AS signup_date UNION ALL
  SELECT 'paid_search', DATE '2026-11-05' UNION ALL
  SELECT 'paid_search', DATE '2026-11-11' UNION ALL
  SELECT 'newsletter', DATE '2026-11-03' UNION ALL
  SELECT 'newsletter', DATE '2026-11-20'
)
SELECT DISTINCT
  channel,
  PERCENTILE_DISC(signup_date, 0.5) OVER (PARTITION BY channel) AS median_signup_date
FROM signups
ORDER BY channel;
Result
channelmedian_signup_date
newsletter2026-11-03
paid_search2026-11-05

RESPECT NULLS on strings

Ignoring NULLs, the middle plan of free, pro, team is pro. With RESPECT NULLS the two NULLs sort first and cover 40% of the rows, so the first value that reaches 50% is free.

SELECT DISTINCT
  PERCENTILE_DISC(plan, 0.5) OVER () AS median_ignore_nulls,
  PERCENTILE_DISC(plan, 0.5 RESPECT NULLS) OVER () AS median_respect_nulls
FROM UNNEST(['pro', NULL, 'free', NULL, 'team']) AS plan;
Result
median_ignore_nullsmedian_respect_nulls
profree

Common mistakes

  • Expecting interpolation. For an even number of values PERCENTILE_DISC returns the lower middle value; use PERCENTILE_CONT for the average of the two.
  • Calling it as an aggregate. Without OVER BigQuery rejects it; add OVER () or OVER (PARTITION BY ...) and SELECT DISTINCT.
  • Porting PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY x). That form is a syntax error; write PERCENTILE_DISC(x, 0.5) OVER ().
  • Adding ORDER BY or a frame to the OVER clause. Only PARTITION BY is allowed.

Cost and performance

  • OVER () puts the whole table into a single window partition processed by one worker. On large tables use APPROX_QUANTILES, which also returns observed values, or partition and filter first.

FAQ

What is the difference between PERCENTILE_DISC and PERCENTILE_CONT?

PERCENTILE_DISC returns a value that exists in the data and works on any orderable type. PERCENTILE_CONT interpolates between the two nearest values and works on numbers only.

Can I calculate a median date or string in BigQuery?

Yes, with PERCENTILE_DISC: SELECT DISTINCT PERCENTILE_DISC(signup_date, 0.5) OVER () FROM signups. PERCENTILE_CONT accepts only numeric values.

How do I get one PERCENTILE_DISC value per group?

Partition the window and deduplicate: SELECT DISTINCT channel, PERCENTILE_DISC(x, 0.5) OVER (PARTITION BY channel) FROM t.

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.