Build inventory rebalancing recommendation model across warehouses
harbor run -t snowflake-labs/dbt-multi-warehouse-stock-rebalanceharbor download snowflake-labs/dbt-multi-warehouse-stock-rebalanceYour company operates 6 warehouses across different regions. The inventory team has noticed stockouts in some locations while other warehouses have excess inventory of the same products. They need a data model to identify which products need inventory rebalancing across warehouses to improve fulfillment efficiency.
/app/dbt_models_duckdb/models/marts/inventory/rpt_warehouse_rebalancing.sql/app/dbt_models_snowflake/models/marts/inventory/rpt_warehouse_rebalancing.sqlThis 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.stg_inventory__inventory_levels: variant_id, warehouse_id, QUANTITY_ON_HAND, QUANTITY_AVAILABLE, QUANTITY_RESERVED, UNIT_COST
stg_inventory__warehouses: warehouse_id, IS_ACTIVE
int_sales__order_lines: order_id, sku (use as variant_id), quantity_ordered
int_sales__orders_enriched: ORDER_ID, status, ordered_at
Create a model with one row per product variant that analyzes inventory distribution across all warehouses.
For each variant, calculate:
Analyze sales velocity using the last 90 days of delivered orders (use MAX(ordered_at) from the orders table as the reference date to ensure consistent results):
Calculate metrics that indicate whether inventory is well-distributed:
Create a rebalancing_priority score that identifies which variants need attention most urgently. The score should combine multiple risk factors:
The final score should be scaled to 0-100 range
Classify each variant into rebalancing_action using waterfall logic (check in order, assign first match):
urgent_rebalance
high_priority
rebalance_recommended
monitor
well_distributed
Default: All other cases -> monitor
Your model must include these columns:
variant_idtotal_warehouses_stockedtotal_quantity_on_handtotal_quantity_availabletotal_quantity_reservedavg_unit_costtotal_inventory_valuemax_warehouse_stockmin_warehouse_stocktotal_units_sold_90dtotal_orders_90davg_daily_sales_velocitydays_of_stock_remainingstock_concentration_ratiostock_imbalance_scoreutilization_raterebalancing_priorityrebalancing_actionCOALESCE() for all division operations to avoid inf/nandays_of_stock_remaining: treat NULL as 999stock_imbalance_score and stock_concentration_ratio: treat NULL as 0total_warehouses_stocked: no NULL expectedPERCENT_RANK() over rebalancing_priority for the top 25% threshold in action classification