Fix fact table grain issue causing inflated product metrics from improper join to returns
harbor run -t snowflake-labs/dbt-fix-product-metricsharbor download snowflake-labs/dbt-fix-product-metricsBuild a product performance summary from sales data. Create a model called fct_product_metrics that aggregates key performance indicators for each product.
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 retail_dw_masterDBT_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:
schema: to $SNOWFLAKE_SCHEMA — do NOT leave it blank. A blank or omitted schema makes Snowflake silently default to PUBLIC, so your models get built in the wrong schema and the verifier cannot find them.Create the model at models/marts/product/fct_product_metrics.sql in the dbt project directory:
/app/dbt_models_duckdb/models/marts/product/fct_product_metrics.sql/app/dbt_models_snowflake/models/marts/product/fct_product_metrics.sqlExplore the staging models in the dbt project to find relevant tables for products, variants, orders, and order line items. You will need to understand the relationships between these tables and determine which columns to use.
The output model should provide a comprehensive product-level summary including identification fields, order counts, unit volumes, revenue, average pricing, and variant information.
Order filtering: Exclude orders with status CANCELLED, RETURNED, or FAILED. All other order statuses represent valid completed sales.
Output columns: product_id, product_name, category, total_orders, units_sold, product_revenue, avg_unit_price, total_variants
Important: avg_unit_price must be calculated as total revenue divided by total units sold (i.e., product_revenue / units_sold -- a weighted average, not a simple AVG(unit_price)).
All products should appear in the output, even those without any sales. Materialize as a table and sort by revenue descending.