-
Notifications
You must be signed in to change notification settings - Fork 0
Add core_excellent_cohort_revenue model #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| -- Total revenue summary for products in the Excellent rating cohort (avg_rating >= 4.5) | ||
| -- Filters dim_products to rating_tier = 'Excellent' and aggregates revenue, | ||
| -- profit, units sold, and margin metrics at the cohort level. | ||
| with source as ( | ||
|
|
||
| select | ||
| product_id, | ||
| product_name, | ||
| product_category, | ||
| avg_rating, | ||
| rating_tier, | ||
| total_units_sold, | ||
| product_total_revenue, | ||
| product_gross_profit, | ||
| margin_pct | ||
| from {{ ref('dim_products') }} | ||
| where rating_tier = 'Excellent' | ||
|
|
||
| ), | ||
|
|
||
| final as ( | ||
|
|
||
| select | ||
| rating_tier, | ||
| count(product_id) as product_count, | ||
| sum(product_total_revenue) as total_revenue, | ||
| sum(product_gross_profit) as total_gross_profit, | ||
| sum(total_units_sold) as total_units_sold, | ||
| round(avg(avg_rating)::numeric, 2) as avg_rating, | ||
| round(avg(margin_pct)::numeric, 4) as avg_margin_pct | ||
|
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A simple average of per-product margin percentages gives equal weight to a $10-revenue product and a $10M-revenue product. For a cohort revenue model, the standard approach is a weighted average derived directly from the already-aggregated sums: total_gross_profit / NULLIF(total_revenue, 0)This is more analytically sound and avoids a division-by-zero risk in the process. ♻️ Proposed fix — replace simple avg with revenue-weighted margin round(avg(avg_rating)::numeric, 2) as avg_rating,
- round(avg(margin_pct)::numeric, 4) as avg_margin_pct
+ round(
+ (sum(product_gross_profit) / nullif(sum(product_total_revenue), 0))::numeric,
+ 4
+ ) as avg_margin_pct🤖 Prompt for AI AgentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A simple average of per-product margin percentages assigns equal weight to every product regardless of its revenue contribution. For a cohort revenue model this produces a misleading metric. The revenue-weighted equivalent is already computable from the aggregates already in this CTE: sum(product_gross_profit) / nullif(sum(product_total_revenue), 0)This also avoids a potential division-by-zero if ♻️ Proposed fix — replace simple avg with revenue-weighted margin- round(avg(margin_pct)::numeric, 4) as avg_margin_pct
+ round(
+ (sum(product_gross_profit) / nullif(sum(product_total_revenue), 0))::numeric,
+ 4
+ ) as avg_margin_pct🤖 Prompt for AI Agents |
||
| from source | ||
| group by rating_tier | ||
|
|
||
| ) | ||
|
|
||
| select * from final | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
{{ config(materialized='table') }}— intent not enforced.The PR explicitly states this model should use table materialization, but no
{{ config() }}block exists. Without it, dbt uses whatever default is set indbt_project.ymlfor this path (marts/core). If that default isvieworephemeral, the model will silently behave differently from the stated intent, impacting downstream query performance and dependency expectations.🛠️ Proposed fix — add config block
🤖 Prompt for AI Agents
Missing
{{ config(materialized='table') }}— stated intent not enforced.The PR explicitly targets table materialization, but no
{{ config() }}block is present. Without it, dbt resolves materialization fromdbt_project.ymldefaults for this path. If that default isvieworephemeral, the model silently behaves differently from the stated intent, impacting downstream query performance and dependency contracts.🛠️ Proposed fix — add config block
📝 Committable suggestion
🤖 Prompt for AI Agents