Skip to content

Add percentile_cont and percentile_disc macros#1074

Open
tripleaceme wants to merge 2 commits intodbt-labs:mainfrom
tripleaceme:feature/percentile-macros
Open

Add percentile_cont and percentile_disc macros#1074
tripleaceme wants to merge 2 commits intodbt-labs:mainfrom
tripleaceme:feature/percentile-macros

Conversation

@tripleaceme
Copy link
Copy Markdown

@tripleaceme tripleaceme commented Mar 11, 2026

Summary

  • Adds cross-database percentile_cont and percentile_disc macros with adapter.dispatch support
  • Default implementation uses WITHIN GROUP (ORDER BY ...) syntax (Postgres, Redshift, Snowflake, Databricks)
  • BigQuery override uses navigation function syntax: percentile_cont(column, value) OVER()
  • Includes integration tests with seed data and assert_equal checks

Usage

-- Continuous percentile (e.g. median)
select {{ dbt_utils.percentile_cont('amount', 0.5) }} as median_amount
from orders

-- Discrete percentile
select {{ dbt_utils.percentile_disc('score', 0.75) }} as p75_score
from results

Test plan

  • Integration tests pass on Postgres
  • Integration tests pass on BigQuery
  • Integration tests pass on Snowflake
  • Integration tests pass on Redshift
  • Integration tests pass on Databricks

Closes #1053

@tripleaceme tripleaceme requested a review from a team as a code owner March 11, 2026 11:19
Introduces percentile_cont and percentile_disc macros with adapter dispatch
for cross-database compatibility. Default implementation uses WITHIN GROUP
syntax (Postgres, Redshift, Snowflake, Databricks), BigQuery override uses
navigation function syntax with OVER().

Closes dbt-labs#1053
@tripleaceme tripleaceme force-pushed the feature/percentile-macros branch from 797b505 to 10ecf7d Compare March 11, 2026 11:23
Redshift does not support exact percentile_disc. Added a Redshift-specific
override that uses approximate percentile_disc instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding percentile_cont and percentile_disc macros

1 participant