Build a dbt project that produces monthly product return rate reporting table with return metrics, revenue impact, and return reason analysis
harbor run -t snowflake-labs/dbt-product-return-rate-analysisharbor download snowflake-labs/dbt-product-return-rate-analysisYou are building a product return rate reporting system using dbt. Your project lives at /app/dbt_project.
/app/dbt_models_duckdb/ (reference project)/app/dbt_models_snowflake/ (reference project)/app/dbt_project/Implement three models in a layered architecture: staging, intermediate, and mart.
This task supports both DuckDB and Snowflake. Run echo $DB_TYPE in a shell BEFORE you write any code or spawn any subagents — the live env determines which backend the verifier runs against. Do NOT assume a default from this prose. Both /app/dbt_models_duckdb/ and /app/dbt_models_snowflake/ exist on disk; the verifier only checks the project matching the live $DB_TYPE.
DB_TYPE=duckdb$DUCKDB_PATH (default: /app/database/retail.duckdb)DB_TYPE=snowflakeSNOWFLAKE_ACCOUNTSNOWFLAKE_USERSNOWFLAKE_PASSWORDSNOWFLAKE_DATABASE - The clone database to useSNOWFLAKE_SCHEMASNOWFLAKE_WAREHOUSESNOWFLAKE_ROLE (optional)Note: For Snowflake, the entrypoint automatically creates a clone database and sets SNOWFLAKE_DATABASE. The clone is destroyed when the task completes.
You must configure dbt to connect to the database:
profiles.yml in the dbt project directory with profile name dbt_projectDBT_PROFILES_DIR environment variable to the directory containing profiles.ymlConfigure with type: duckdb and the database path from $DUCKDB_PATH.
Configure with type: snowflake using password authentication:
main.int_sales__orders_enriched (from reference project): order_id, ordered_at, customer_id, statusmain.int_sales__order_lines (from reference project): order_line_id, order_id, product_id, sku, quantity_ordered, line_total{{ source('orders', 'RETURN_LINES') }} (schema ORDERS): order_line_id, quantity_returned, reason_id, created_at (return date){{ source('orders', 'RETURN_REASONS') }} (schema ORDERS): reason_id, reason_name (use as return reason; treat NULL as 'UNKNOWN' for top-reason logic)Define the orders source with tables RETURN_LINES and RETURN_REASONS in schema ORDERS (e.g. in models/sources.yml).
stg_product_returns.sqlLocation: models/staging/stg_product_returns.sql
Purpose: Clean and normalize return data with order context
Requirements:
main.int_sales__order_lines with main.int_sales__orders_enriched on order_id{{ source('orders', 'RETURN_LINES') }} on order_line_id{{ source('orders', 'RETURN_REASONS') }} on reason_idproduct_id IS NOT NULL, status NOT IN ('CANCELLED', 'FAILED'), quantity_returned > 0days_to_return: EXTRACT(EPOCH FROM (return_date - ordered_at)) / 86400.0 where return_date >= ordered_atCOALESCE(sku, product_id) for skuCOALESCE(reason_name, 'UNKNOWN') for return_reasonreturn_revenue_impact: line_total * (quantity_returned / NULLIF(quantity_ordered, 0))Output columns:
order_line_id (VARCHAR)order_id (VARCHAR)product_id (VARCHAR)sku (VARCHAR)customer_id (VARCHAR)ordered_at (TIMESTAMP)return_date (TIMESTAMP)quantity_ordered (INTEGER)quantity_returned (INTEGER)line_total (DECIMAL(18,2))return_reason (VARCHAR)days_to_return (DOUBLE)return_revenue_impact (DECIMAL(18,2))int_product_return_metrics.sqlLocation: models/intermediate/int_product_return_metrics.sql
Purpose: Calculate product-level return metrics with percentiles and cohort analysis
Requirements:
stg_product_returns via {{ ref('stg_product_returns') }}product_id, sku, month_start (where month_start = date_trunc('month', ordered_at)::DATE)units_ordered: SUM(quantity_ordered) from order lines (join back to main.int_sales__order_lines and main.int_sales__orders_enriched with same filters)units_returned: SUM(quantity_returned)return_rate: units_returned / NULLIF(units_ordered, 0)return_revenue_impact: SUM(return_revenue_impact)customers_affected: COUNT(DISTINCT customer_id)avg_days_to_return: AVG(days_to_return)PERCENTILE_CONT):
p25_days_to_return: 25th percentile of days_to_returnp50_days_to_return: 50th percentile (median) of days_to_returnp75_days_to_return: 75th percentile of days_to_returnp25_return_rate: 25th percentile of return rates across order lines (calculate per order line first: quantity_returned / NULLIF(quantity_ordered, 0), then percentile)p50_return_rate: 50th percentilep75_return_rate: 75th percentilefast_returns (INTEGER): count where days_to_return <= 7medium_returns (INTEGER): count where 7 < days_to_return <= 30slow_returns (INTEGER): count where days_to_return > 30top_return_reason: most common return_reason (by count of return lines)return_reason_count: count of return lines for top reasonROW_NUMBER() with ORDER BY COUNT(*) DESC, return_reason to break tiesOutput columns:
month_start (DATE)product_id (VARCHAR)sku (VARCHAR)units_ordered (INTEGER)units_returned (INTEGER)return_rate (DECIMAL(10,6))return_revenue_impact (DECIMAL(18,2))customers_affected (INTEGER)avg_days_to_return (DECIMAL(12,4))p25_days_to_return (DECIMAL(12,4))p50_days_to_return (DECIMAL(12,4))p75_days_to_return (DECIMAL(12,4))p25_return_rate (DECIMAL(10,6))p50_return_rate (DECIMAL(10,6))p75_return_rate (DECIMAL(10,6))fast_returns (INTEGER)medium_returns (INTEGER)slow_returns (INTEGER)top_return_reason (VARCHAR)return_reason_count (INTEGER)rpt_product_return_rates_monthly.sqlLocation: models/marts/product/rpt_product_return_rates_monthly.sql
Purpose: Final reporting table with trend analysis and cohort metrics
Requirements:
int_product_return_metrics via {{ ref('int_product_return_metrics') }}int_product_return_metricsprev_month_return_rate (DECIMAL(10,6)): LAG(return_rate) OVER (PARTITION BY product_id ORDER BY month_start)mom_return_rate_change (DECIMAL(10,6)): return_rate - prev_month_return_rate; NULL when no previous monthmom_return_rate_change_pct (DECIMAL(10,4)): (mom_return_rate_change / NULLIF(prev_month_return_rate, 0)) * 100; NULL when no previous month or prev_month_return_rate = 0first_return_month (DATE): MIN(month_start) OVER (PARTITION BY product_id) where units_returned > 0months_since_first_return (INTEGER): DATEDIFF('month', first_return_month, month_start); NULL when no returns yetis_first_return_month (BOOLEAN): month_start = first_return_monthfast_return_rate (DECIMAL(10,6)): fast_returns / NULLIF(units_returned, 0)medium_return_rate (DECIMAL(10,6)): medium_returns / NULLIF(units_returned, 0)slow_return_rate (DECIMAL(10,6)): slow_returns / NULLIF(units_returned, 0)units_returned = 0, set return_rate = 0, top_return_reason = NULL, return_reason_count = 0, all days-to-return metrics = NULL, all return velocity metrics = 0, all trend metrics = NULL.Output columns (in order):
month_start (DATE)product_id (VARCHAR)sku (VARCHAR)units_ordered (INTEGER)units_returned (INTEGER)return_rate (DECIMAL(10,6))return_revenue_impact (DECIMAL(18,2))customers_affected (INTEGER)avg_days_to_return (DECIMAL(12,4))p25_days_to_return (DECIMAL(12,4))p50_days_to_return (DECIMAL(12,4))p75_days_to_return (DECIMAL(12,4))p25_return_rate (DECIMAL(10,6))p50_return_rate (DECIMAL(10,6))p75_return_rate (DECIMAL(10,6))fast_returns (INTEGER)medium_returns (INTEGER)slow_returns (INTEGER)fast_return_rate (DECIMAL(10,6))medium_return_rate (DECIMAL(10,6))slow_return_rate (DECIMAL(10,6))top_return_reason (VARCHAR)return_reason_count (INTEGER)prev_month_return_rate (DECIMAL(10,6))mom_return_rate_change (DECIMAL(10,6))mom_return_rate_change_pct (DECIMAL(10,4))first_return_month (DATE)months_since_first_return (INTEGER)is_first_return_month (BOOLEAN)product_id is NULL.status NOT IN ('CANCELLED', 'FAILED').quantity_returned > 0.return_rate = 0, top_return_reason = NULL, return_reason_count = 0, all days-to-return metrics = NULL, all return velocity counts = 0, all trend metrics = NULL.days_to_return calculations, include only returns where return_date >= ordered_at.PERCENTILE_CONT (continuous).product_id and order by month_start.return_rate between 0 and 1 (inclusive).units_returned <= units_ordered for every row.return_revenue_impact = 0 when units_returned = 0; non-negative when units_returned > 0.customers_affected >= 0.sku must not be NULL (use product_id when source sku is NULL).units_returned > 0: top_return_reason and return_reason_count must be non-NULL and return_reason_count > 0.p25 <= p50 <= p75 for both days and return rates.units_returned > 0: fast_return_rate + medium_return_rate + slow_return_rate = 1.0 (within 0.0001 tolerance).units_ordered and units_returned within tolerances used by the grader.dbt run --select int_sales__orders_enriched int_sales__order_lines before your models.view, intermediate as table, mart as table.analytics (e.g. schema: analytics in profiles.yml).