Implement FIFO inventory valuation with pure SQL dbt models using window functions and range-based joins
harbor run -t snowflake-labs/fifo-inventory-cogsharbor download snowflake-labs/fifo-inventory-cogsYour company uses the First-In-First-Out (FIFO) method for inventory costing. This task requires implementing a comprehensive FIFO inventory system with cost variance analysis, inventory aging, and turnover metrics using pure SQL dbt models.
This task supports two database backends, controlled by the DB_TYPE environment variable:
DB_TYPE=duckdb): Local DuckDB database at path $DUCKDB_PATH (default: /app/database/retail.duckdb)DB_TYPE=snowflake): Snowflake cloud database. Connection details are provided via environment variables:
SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, SNOWFLAKE_PASSWORDSNOWFLAKE_DATABASE, SNOWFLAKE_SCHEMA, SNOWFLAKE_WAREHOUSE, SNOWFLAKE_ROLEretail_dw_mastertype: duckdbpath: set to $DUCKDB_PATHtype: snowflakeaccount, user, database, schema, warehouse, role from environment variables/app/dbt_models_duckdb/app/dbt_models_snowflakeRun dbt deps before running models.
DATEDIFF('day', start_date, end_date) for date differences (works on both backends).TO_VARCHAR(date, 'YYYY-MM') for date formatting (works on both backends).strftime, date_diff, CROSS JOIN LATERAL.CAST(x AS DOUBLE) for float conversions.CEIL (not CEILING) for ceiling function.macros/utils/generate_schema_name.sql to route all models to the default schema.Create a multi-model dbt solution for FIFO inventory analysis. All models should be in models/marts/inventory/ and use schema inventory_analytics.
Note: The dbt project prefixes custom schemas with main_, so inventory_analytics becomes main_inventory_analytics in the database. For Snowflake, override the generate_schema_name macro to route all models to the default main schema.
stg_fifo_transactions.sqlPrepare inventory transactions for FIFO processing. Only RECEIPT and PICK transactions are relevant for FIFO costing.
Output columns:
| Column | Description |
|---|---|
| transaction_id | Original TRANSACTION_ID |
| warehouse_id | Warehouse identifier |
| variant_id | Product variant identifier |
| transaction_type | 'RECEIPT' or 'PICK' |
| transaction_timestamp | TRANSACTION_TIMESTAMP |
| transaction_date | TRANSACTION_DATE |
| quantity | ABS(QUANTITY) - always positive |
| unit_cost | UNIT_COST (0 if NULL) |
| row_num | ROW_NUMBER() partitioned by warehouse_id, variant_id, transaction_type ordered by timestamp, transaction_id |
int_receipt_layers.sqlCreate receipt layer tracking with cumulative quantities.
Output columns:
| Column | Description |
|---|---|
| receipt_id | TRANSACTION_ID of the receipt |
| warehouse_id | Warehouse identifier |
| variant_id | Product variant identifier |
| receipt_timestamp | TRANSACTION_TIMESTAMP |
| receipt_date | TRANSACTION_DATE |
| receipt_qty | Original quantity received |
| unit_cost | Cost per unit |
| cumulative_qty_before | Sum of prior receipt quantities (0 for first) |
| cumulative_qty_after | cumulative_qty_before + receipt_qty |
| days_since_receipt | Days between receipt_date and current_date (use '2025-12-31' as reference) |
| age_bucket | 'Current (0-30)', 'Aging (31-90)', 'Slow (91-180)', 'Obsolete (180+)' |
Requirements:
TRANSACTION_TIMESTAMP ASC, then TRANSACTION_ID ASC (deterministic tie-breaking)int_pick_consumption.sqlTrack cumulative pick consumption per warehouse-variant.
Output columns:
| Column | Description |
|---|---|
| pick_id | TRANSACTION_ID of the pick |
| warehouse_id | Warehouse identifier |
| variant_id | Product variant identifier |
| pick_timestamp | TRANSACTION_TIMESTAMP |
| pick_date | TRANSACTION_DATE |
| pick_qty | Quantity picked |
| consumption_start | Cumulative consumption before this pick |
| consumption_end | consumption_start + pick_qty |
int_fifo_allocation.sqlAllocate each pick to receipt layers using FIFO range-overlap logic.
Output columns:
| Column | Description |
|---|---|
| pick_id | TRANSACTION_ID of the pick |
| pick_timestamp | TRANSACTION_TIMESTAMP |
| pick_date | TRANSACTION_DATE |
| warehouse_id | Warehouse identifier |
| variant_id | Product variant identifier |
| receipt_id | TRANSACTION_ID of consumed receipt |
| receipt_unit_cost | Unit cost from receipt |
| allocated_qty | Quantity from this receipt for this pick |
| allocation_cost | allocated_qty * receipt_unit_cost |
| pick_total_qty | Total quantity of the pick |
| total_available_before_pick | Total receipts before pick timestamp |
| is_fully_fulfilled | TRUE if pick <= total_available_before_pick |
FIFO Logic:
Critical: Receipts must be consumed in strict chronological order. No pick should consume from receipt B before exhausting available quantity from earlier receipt A.
fifo_cogs_monthly.sqlMonthly COGS aggregation with cost analysis.
Output columns:
| Column | Description |
|---|---|
| year_month | Format YYYY-MM |
| category_name | Product category ('Uncategorized' if NULL) |
| warehouse_id | Warehouse identifier |
| total_picks | Count of distinct pick transactions |
| total_units_requested | Sum of pick quantities |
| total_units_fulfilled | Sum of allocated quantities |
| total_units_unfulfilled | total_units_requested - total_units_fulfilled |
| total_cogs | Sum of allocation costs |
| avg_fifo_unit_cost | total_cogs / total_units_fulfilled (NULL if 0) |
| fulfillment_rate | total_units_fulfilled / total_units_requested (0 if no requests) |
| pick_count_fully_fulfilled | Count of picks where is_fully_fulfilled = TRUE |
| pick_count_partial | Count of picks where is_fully_fulfilled = FALSE |
Requirements:
ending_inventory_valuation.sqlRemaining inventory with aging analysis.
Output columns:
| Column | Description |
|---|---|
| warehouse_id | Warehouse identifier |
| variant_id | Product variant identifier |
| category_name | Product category |
| sku | Product SKU from PRODUCT_VARIANTS |
| total_units_remaining | Remaining quantity across all layers |
| total_value | Sum of (remaining_qty * unit_cost) per layer |
| weighted_avg_cost | total_value / total_units_remaining |
| layer_count | Number of receipt layers with remaining qty |
| oldest_layer_date | Earliest receipt date with remaining inventory |
| newest_layer_date | Latest receipt date with remaining inventory |
| avg_days_in_inventory | Average days since receipt weighted by remaining qty |
| current_units | Units in 'Current (0-30)' bucket |
| aging_units | Units in 'Aging (31-90)' bucket |
| slow_units | Units in 'Slow (91-180)' bucket |
| obsolete_units | Units in 'Obsolete (180+)' bucket |
| obsolete_value | Value of obsolete inventory |
Requirements:
inventory_turnover_analysis.sqlInventory turnover and efficiency metrics.
Output columns:
| Column | Description |
|---|---|
| warehouse_id | Warehouse identifier |
| category_name | Product category |
| total_receipts_qty | Sum of all receipt quantities |
| total_picks_qty | Sum of all pick quantities |
| total_cogs | Sum of COGS from picks |
| ending_inventory_qty | Current remaining inventory |
| ending_inventory_value | Current inventory value |
| avg_inventory_value | (total_receipts_value + ending_inventory_value) / 2 |
| inventory_turnover_ratio | total_cogs / avg_inventory_value (NULL if 0) |
| days_inventory_outstanding | 365 / inventory_turnover_ratio (NULL if 0) |
| fulfillment_efficiency | total_picks_qty / total_receipts_qty |
| slow_moving_flag | TRUE if days_inventory_outstanding > 90 OR turnover_ratio < 2 |
Requirements:
Use existing source definitions:
{{ source('inventory', 'INVENTORY_TRANSACTIONS') }}{{ source('product', 'PRODUCT_VARIANTS') }}{{ source('product', 'PRODUCTS') }}{{ source('product', 'PRODUCT_CATEGORIES') }}Your solution must satisfy: