Fix running total using ROWS instead of RANGE causing incorrect cumulative calculations
harbor run -t snowflake-labs/dbt-fix-inventory-balanceharbor download snowflake-labs/dbt-fix-inventory-balanceBuild a dbt model called fct_inventory_balance that tracks cumulative sales for products over time.
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 models/marts/inventory/fct_inventory_balance.sql with these columns:
/app/dbt_models_duckdb/models/marts/inventory/fct_inventory_balance.sql/app/dbt_models_snowflake/models/marts/inventory/fct_inventory_balance.sqlColumns:
product_id - Product identifier from products tableproduct_name - Product name from products tableorder_date - Order date (cast ORDERED_AT column to DATE type from orders table)order_line_id - Order line identifierunits_sold - Quantity from order lines (use quantity_ordered)revenue - Line total from order linescumulative_units_sold - Running total of units per product up to and including current datecumulative_revenue - Running total of revenue per product up to and including current dateprev_day_cumulative_revenue - The maximum cumulative revenue from the PREVIOUS calendar date for this product (NULL for first date). All rows on the same date should show the same prev_day value.daily_change - Difference between current cumulative and previous day's cumulative revenuerank_in_day - Ranking of order lines within same product and date, ordered by order_line_id ascendingdays_since_last_sale - Number of days between current order_date and the previous DISTINCT order_date for this product (NULL for first date). Use LAG over distinct (product_id, order_date) pairs, not over raw rows.running_avg_revenue_per_line - Cumulative revenue divided by cumulative units sold (handle division by zero)cumulative_percentile - PERCENT_RANK of the product's current cumulative revenue compared to ALL products' current cumulative revenue on the same dateData Filtering: Only include order lines where the order status is NOT 'cancelled' or 'refunded'. Check the status column in the orders table.
Join Path: Use staging tables: stg_orders__order_lines, stg_orders__orders, stg_product__product_variants, stg_product__products. Join order lines to orders on order_id, then join to product variants on variant_id from order lines, then join to products on product_id from product variants.
Window Functions:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW to ensure same-date values matchprev_day_cumulative_revenue, calculate the maximum cumulative from the previous date using a self-join or window functionrank_in_day, use ROW_NUMBER partitioned by product and date, ordered by order_line_iddays_since_last_sale, use LAG on order_date then calculate DATEDIFFcumulative_percentile, use PERCENT_RANK partitioned by order_date, ordered by cumulative_revenueSchema Configuration: Also create models/marts/inventory/schema.yml that defines a test ensuring cumulative_revenue is never negative.
Materialize as a table.
The model uses these staging tables (explore each to understand available columns):