Cheatsheets

๐Ÿ“‹ NPBlue Cheatsheets 4 sheets

Condensed, scannable reference cards โ€” commands, flags, and syntax you can copy-paste mid-task, distilled from NPBlue's full guides.

dbt Cheatsheet

Core commands, flags, and Jinja/YAML syntax youโ€™ll actually reach for mid-task. For the full explanations, see the dbt guides.

Commands

CommandDescription
dbt runCompiles and executes every modelโ€™s SQL against the warehouse, in DAG order. Does not run tests.
dbt testRuns generic + singular tests. Non-zero exit code on any failure โ€” usable directly as a CI gate.
dbt buildInterleaves seeds, models, snapshots, and their tests in one dependency-ordered run. Downstream models are SKIPped after a failed test.
dbt seedLoads CSVs from seeds/ as warehouse tables. Insert-only by default โ€” doesnโ€™t reconcile deletes/edits.
dbt snapshotExecutes {% snapshot %} blocks, comparing current source state vs. the last captured version.
dbt debugValidates dbt_project.yml, profiles.yml, and a live connection. Run this first in new CI setups.
dbt run-operation <macro>Invokes a macro directly, outside the model-build lifecycle.
dbt cleanDeletes local dirs in clean-targets (target/, dbt_packages/). Touches nothing in the warehouse.
dbt depsDownloads packages declared in packages.yml into dbt_packages/.
dbt docs generateProduces manifest.json and catalog.json in target/.
dbt docs serveHosts the generated docs site locally (default localhost:8080).
dbt compileResolves Jinja/refs without executing โ€” writes to target/compiled/.
dbt parseStructural manifest only, no live warehouse connection needed.
dbt source freshnessChecks raw source staleness against loaded_at_field; writes target/sources.json.
dbt lsLists/selects nodes โ€” models, tests by tag, etc.
dbt init <project>Scaffolds a new project.

Flags

FlagApplies toPurpose
--selectrun, test, build, seed, snapshot, lsScopes to a model/graph selection
--excludebuild, runExcludes matching nodes
--full-refreshrun, seedForces incremental/seed rebuild from scratch
--targetrun, debugChooses the warehouse/environment connection
--threadsrunControls model concurrency
--store-failurestestMaterializes failing rows into a queryable table
--indirect-selection=cautious|eagertestControls whether multi-model tests run on partial selections
--fail-fastbuildStops the entire run at the first failure
--vars '{"key": "value"}'run, etc.Overrides project vars for a single invocation
--args '{"k": "v"}'run-operationPasses macro arguments as JSON
--config-dirdebugShows where profiles.yml is being read from
--no-connectiondebugValidates config files without a live connection test
--profiles-diranyOverrides the location of profiles.yml
--debuganyPrints compiled SQL + connection details

Selection syntax

SyntaxMeaning
model_nameJust that model
model_name+Model + everything downstream
+model_nameModel + everything upstream
+model_name+Model + both directions
tag:xAll models tagged x
source:*All sources
test_type:singular / test_type:genericFilter by test type
resource_type:snapshotFilter by resource type
staging.*All models under a folder path
Terminal window
dbt run --select stg_orders+
dbt test --select unique_stg_orders_order_id --store-failures
dbt build --select tag:finance --fail-fast

Syntax

Model config

{{ config(materialized='incremental', unique_key='order_id') }}
models:
my_project:
staging:
+materialized: view
marts:
+materialized: table

Incremental pattern:

{{ config(materialized='incremental', unique_key='event_id') }}
select event_id, user_id, event_type, occurred_at
from {{ ref('stg_events') }}
{% if is_incremental() %}
where occurred_at > (select max(occurred_at) from {{ this }})
{% endif %}

source() and ref()

select id as order_id from {{ source('raw', 'orders') }} where status != 'test'
select * from {{ ref('stg_customers') }}
select * from {{ ref('finance_project', 'monthly_revenue') }} -- cross-project (dbt Mesh)
version: 2
sources:
- name: raw
database: analytics_raw
schema: raw_app_data
loaded_at_field: _fivetran_synced
freshness:
warn_after: {count: 12, period: hour}
error_after: {count: 24, period: hour}
tables:
- name: orders
- name: customers

ref() resolves at compile time, not runtime โ€” a typo fails immediately with a clear error instead of a confusing runtime SQL error. Only staging models should call source() directly โ€” everything downstream should go through ref().

Macros

-- macros/cents_to_dollars.sql
{% macro cents_to_dollars(column_name) %}
({{ column_name }} / 100.0)
{% endmacro %}

Called as {{ cents_to_dollars('amount_in_cents') }}. Macro returning a config dict:

{% macro standard_incremental_config(unique_key) %}
{{ return({
'materialized': 'incremental',
'unique_key': unique_key,
'incremental_strategy': 'merge'
}) }}
{% endmacro %}

Tests

Generic tests (built-in):

models:
- name: stg_orders
columns:
- name: order_id
tests: [unique, not_null]
- name: status
tests:
- accepted_values:
values: ['pending', 'shipped', 'delivered', 'cancelled', 'returned']
- name: customer_id
tests:
- relationships:
to: ref('stg_customers')
field: customer_id

Singular test (fails if the query returns any rows):

-- tests/assert_no_negative_completed_order_totals.sql
select order_id, total_amount from {{ ref('stg_orders') }}
where status = 'completed' and total_amount < 0

Custom generic test macro:

-- macros/test_not_negative.sql
{% test not_negative(model, column_name) %}
select * from {{ model }} where {{ column_name }} < 0
{% endtest %}

Snapshots

{% snapshot customers_snapshot %}
{{ config(
target_schema='snapshots',
unique_key='customer_id',
strategy='timestamp',
updated_at='updated_at',
) }}
select * from {{ source('raw', 'customers') }}
{% endsnapshot %}

check strategy variant: strategy='check', check_cols=['region', 'email', 'plan_tier']. Managed columns added automatically: dbt_scd_id, dbt_valid_from, dbt_valid_to.

var() and Jinja

dbt_project.yml
vars:
start_date: '2024-01-01'
where order_date >= '{{ var("start_date") }}'
where event_date >= '{{ var("lookback_start_date", "2020-01-01") }}' -- inline fallback
Terminal window
dbt run --select customer_orders --vars '{"start_date": "2025-06-01"}'

Loop + conditional:

{% set status_list = ['completed', 'shipped', 'delivered'] %}
select * from {{ ref('stg_orders') }}
where status in (
{% for status in status_list %}
'{{ status }}'{% if not loop.last %},{% endif %}
{% endfor %}
)

Use var() for values that could reasonably be committed to git; use env_var() for secrets or environment-specific infra details.

Project config

dbt_project.yml
name: my_project
version: 1.0
profile: my_profile
model-paths: ["models"]
models:
my_project:
staging: { materialized: view }
marts: { materialized: table }
# ~/.dbt/profiles.yml (never committed)
my_project:
target: dev
outputs:
dev:
type: postgres
host: localhost
user: analyst
password: "{{ env_var('DBT_PASSWORD') }}"
dbname: analytics
schema: dbt_dev
threads: 4
packages.yml
packages:
- package: dbt-labs/dbt_utils
version: [">=1.1.0", "<2.0.0"]

Gotchas