BigQuery · Window function
BigQuery PERCENT_RANK
PERCENT_RANK() returns each row's relative rank in its window partition as a FLOAT64 from 0 to 1, computed as (rank - 1) / (rows - 1). Use it to say 'this request was slower than 90% of the others' or to flag the extreme rows of a group.
PERCENT_RANK is RANK rescaled to the range 0 to 1: (RANK - 1) / (rows in partition - 1). The first row of a partition gets 0, the last gets 1, and tied rows share a value because they share a rank. A partition with a single row returns 0.
Read the value as the share of the other rows that rank below the current one. With ORDER BY latency_ms, a PERCENT_RANK of 0.9 means 90% of the other requests were faster. Filter on it with QUALIFY, for example QUALIFY PERCENT_RANK() OVER (ORDER BY latency_ms) >= 0.9 for the slowest tenth.
ORDER BY is required and a window frame is not allowed. The related CUME_DIST counts the current row and its ties as well, so its smallest possible value is 1/n rather than 0. To get the value at a given percentile, rather than the percentile of each row, use PERCENTILE_CONT or APPROX_QUANTILES.
Syntax
PERCENT_RANK() OVER ([PARTITION BY partition_expression [, ...]] ORDER BY expression [{ASC | DESC}] [, ...])Arguments and return value
| Argument | Description |
|---|---|
PARTITION BY partition_expression | Computes percent ranks within each group. Optional. |
ORDER BY expression | Required. The first row in this order gets 0, the last gets 1. |
Returns: FLOAT64 between 0 and 1. 0 for a partition with one row.
Examples
Percent rank of order amounts
Five rows, so each step is 1/4. The two orders of 45 tie and share 0.25.
WITH orders AS (
SELECT order_id, amount
FROM UNNEST([STRUCT(1001 AS order_id, 20 AS amount), (1002, 45), (1003, 45), (1004, 80), (1005, 150)])
)
SELECT
order_id,
amount,
PERCENT_RANK() OVER (ORDER BY amount) AS pct_rank
FROM orders
ORDER BY amount, order_id;| order_id | amount | pct_rank |
|---|---|---|
| 1001 | 20 | 0.0 |
| 1002 | 45 | 0.25 |
| 1003 | 45 | 0.25 |
| 1004 | 80 | 0.75 |
| 1005 | 150 | 1.0 |
Per pipeline, including a one-row partition
WITH runs AS (
SELECT 'orders_daily' AS pipeline, 1 AS run_id, 340 AS runtime_s UNION ALL
SELECT 'orders_daily', 2, 410 UNION ALL
SELECT 'orders_daily', 3, 380 UNION ALL
SELECT 'users_sync', 4, 95
)
SELECT
pipeline,
run_id,
runtime_s,
PERCENT_RANK() OVER (PARTITION BY pipeline ORDER BY runtime_s) AS runtime_pct_rank
FROM runs
ORDER BY pipeline, runtime_s;| pipeline | run_id | runtime_s | runtime_pct_rank |
|---|---|---|---|
| orders_daily | 1 | 340 | 0.0 |
| orders_daily | 3 | 380 | 0.5 |
| orders_daily | 2 | 410 | 1.0 |
| users_sync | 4 | 95 | 0.0 |
Flag the slowest tenth of requests
WITH requests AS (
SELECT request_id, latency_ms
FROM UNNEST([
STRUCT('r01' AS request_id, 120 AS latency_ms), ('r02', 95), ('r03', 310), ('r04', 88),
('r05', 102), ('r06', 2400), ('r07', 140), ('r08', 99), ('r09', 131), ('r10', 860), ('r11', 115)
])
)
SELECT
request_id,
latency_ms,
PERCENT_RANK() OVER (ORDER BY latency_ms) AS pct_rank
FROM requests
QUALIFY pct_rank >= 0.9
ORDER BY latency_ms;| request_id | latency_ms | pct_rank |
|---|---|---|
| r10 | 860 | 0.9 |
| r06 | 2400 | 1.0 |
Common mistakes
- Reading PERCENT_RANK as the share of rows at or below the current one. It starts at 0 and leaves the current row out; CUME_DIST includes it.
- Trusting percent ranks of tiny partitions. With two rows the only values are 0 and 1, and one row always gets 0.
- Expecting a threshold to select an exact share of rows. Ties share a value, so
>= 0.9can return more or fewer rows than 10%. - Omitting ORDER BY.
PERCENT_RANK() OVER ()fails withWindow ORDER BY is required for analytic function percent_rank.
Cost and performance
- Like other ranking functions, PERCENT_RANK without PARTITION BY sorts the whole input on one worker. On a large table, compute a percentile threshold once with APPROX_QUANTILES and compare each row with it.
FAQ
How do I calculate a percentile rank in BigQuery?
Use PERCENT_RANK() OVER (ORDER BY metric), or OVER (PARTITION BY group_col ORDER BY metric) per group. Multiply by 100 for a percentage.
What is the difference between PERCENT_RANK and CUME_DIST?
PERCENT_RANK is (rank - 1) / (n - 1) and starts at 0. CUME_DIST is the share of rows that come before the current row or tie with it, so it always counts the current row and is never 0.
How do I get the value at the 90th percentile instead?
Use PERCENTILE_CONT(x, 0.9) OVER () for an exact, interpolated value or APPROX_QUANTILES(x, 100)[OFFSET(90)] in a GROUP BY query. See PERCENTILE_CONT.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
