Into dbt
What is dbt (data build tool)?
If Kimball’s framework gives you the architectural blueprint to build data warehouse, and the Analytics Engineers or Data Engineers are builders, then dbt is the toolbox.
dbt owns the T in ELT (Extract, Load, Transform) — it focuses purely on the transform step: turning raw data into usable data, while giving you the tooling to test, version, and document your data models.
Why do we need dbt?
Transforming data usually runs into the same set of problems:
- Scattered, hard-to-maintain SQL scripts: SQL files chained one after another, with no clear structure and no easy way to trace dependencies.
- No testing: Transformed data rarely gets checked for quality (uniqueness, not-null, and so on).
- Painful version control & collaboration: SQL scripts live in standalone files that are hard to review and hard to wire into Git.
- Missing or stale documentation: You can’t remember what you ate yesterday — let alone every table and column in your project and what they’re for if you don’t document (I know, it’s an important boring job).
dbt can solve all of the above problems.
How does dbt work?
- First, create a project with
dbt init. Then configure it throughprofiles.yml. - From there, most of your work in dbt revolves around building and managing data models — SQL files that define how raw data gets processed and transformed.
- Once the models are ready, run them with
dbt run. This command compiles your SQL, deploys it against the targets declared inprofiles.yml, and executes the transformations directly inside your data warehouse. - After the run, test with
dbt testand validate the output to make sure everything behaves as expected. If something's off, this is where you troubleshoot and adjust. - And finally, document things with
dbt docs generate. Almost everything in dbt can be documented via.ymlfiles
The cycle repeats as needed — usually looping back to the modeling step whenever a new data source shows up or existing logic needs an update.
Let’s go deeper !
Project structure of dbt
Here’s what dbt init generates:
analyses: SQL files you don't want to expose downstream, typically used for data quality reports. Not many people use this folder.dbt_project.yml: required in order to run any dbt command, and the place to declare project-specific configuration.macros: works like functions in Python — where you write reusable logic.seeds: a quick, temporary way to upload CSVs and other flat files into dbt. Recommended only when you don't have control over or access to the database.snapshots: captures and versions your data at specific points in time. Useful for tracking the history of a column that keeps getting overwritten.tests: holds your tests. You can write custom ones or use the built-ins to guarantee data quality. dbt ships with tests like unique, not_null, relationship tests, and more.models: where you store data models.
The things that matter in dbt
dbt_project.yml: the most important file in a dbt project.
Every time you run a dbt command, dbt looks for this file first — if it’s missing, the command fails
Key things it contains:
- Project name (must match your profiles.yml— critical for dbt Core users)
- Profile name
- Default materializations
- Variables
Also a place to set project-wide defaults and configuration
profiles.yml
- Within dbt, a profile is like a deployment scenario. That can be development, staging or testing, production, apocalypse — whatever you need.
- A dbt project can have multiple profiles, allowing for different warehouse configurations per deployment scenario. These profiles (or configurations) are defined in the
profiles.ymlfile, which must be created for new projects.
Here’s an example profiles.yml with 3 deployment types (dev, prod and bigquery).
Data models
dbt suggests organizing models into three layers inside models/ folder:
1. staging
Contains two things:
- Source definitions — telling dbt where your raw data lives in the database
- Staging models — a 1:1 copy of each source table with only minimal cleaning applied
Minimal cleaning means things like:
- Fixing data types
- Renaming columns
- Filtering out clearly empty rows
- Removing unnecessary columns
- Standardizing values
- Keep it 1:1 — same number of rows and columns as the raw source. Breaking this rule is occasionally convenient but should be the exception
2. intermediate
Everything that is not raw and not ready to expose to end users.
Get Pham Minh Tuan’s stories in your inbox
Join Medium for free to get updates from this writer.
A catch-all for:
- Complex joins
- Heavy-duty cleaning or standardization
- Data quality processing
No strict guidelines on what goes here — if it doesn’t fit neatly into staging or marts, it belongs in intermediate.
3. marts
Where all the final, consumption-ready tables live.
If it’s in marts, it’s ready for end users.
In a well-governed dbt project, only marts tables should be exposed to BI tools, analysts, and business stakeholders — nothing else.
Typically contains:
- Tables ready for dashboards
- Properly modeled, clean tables
- Often star schemas, but not necessarily
A Note on Conventions
The staging → intermediate → marts structure is dbt's recommendation, but it's not mandatory. You might have seen teams use:
- Medallion architecture naming:
bronze,silver,gold - Numbered layers:
first,second,third,last - Other custom conventions
If your organization already has a convention, follow it. Otherwise, stick with dbt’s default structure.
Most used dbt commands
dbt compile
Looks like it’s doing nothing, but it’s actually super useful. Takes all your models — with their Jinja, ref(), source() calls and everything — and outputs the fully resolved SQL into target/compiled/. No data moves, nothing hits the warehouse. It's just pure SQL sitting there for you to inspect.
Why bother? Two reasons. First, it’s the fastest way to catch Jinja errors — way quicker than waiting for a full dbt run. Second, it's completely free — no compute, no warehouse cost. Good habit to run after making changes.
dbt run
Materializes every model in your project. Views become views, tables become tables, incremental models get incremental logic applied — whatever you configured. Models run in dependency order, so dbt figures out the sequence for you.
This is your go-to during active development when you just want to see your models built.
dbt test
Runs all the tests in your project — generic tests, singular tests, unit tests, all of it. Reports pass/fail at the end. Nothing gets built here, it just validates what’s already in the warehouse.
dbt build ⭐
The most important command. It’s a smart combination of dbt run + dbt test + dbt seed + dbt snapshot, all in one. But it's not just running them sequentially — it's DAG-aware.
It knows the right order, and if something fails along the way, it skips everything downstream of that failure rather than wasting compute on models that are going to break anyway.
This is what you want for CI, production runs, or any time you need confidence that your whole project is solid.
dbt retry
If a dbt build or dbt run fails partway through, don't just re-run the whole thing from scratch. dbt retry re-executes from the point of failure by reading the run_results.json file from the previous run. It automatically identifies which nodes failed and re-runs those nodes plus everything downstream of them.
How it works:
- dbt looks at
target/run_results.jsonfrom the last command - It identifies failed nodes and skipped nodes (anything downstream of a failure)
- It re-runs only those nodes, reusing the same selection criteria from the original command
- If the previous command completed successfully,
dbt retryfinishes as a no-op
Saves a lot of time on big projects, especially when a single model fails deep in the DAG.
dbt commands become much more powerful when paired with flags, especially this one
— select / -s
Lets you run only specific parts of your project instead of everything. There are a few ways to use it:
By model name — just give it the model name (no .sql needed):
dbt run --select stg_green_tripdataBy directory path — everything in a folder:
dbt run --select models/stagingBy tag:
dbt run --select tag:nightlyWith graph operators (the + sign) — this is where it gets really useful. The + lets you pull in upstream or downstream dependencies:
# Run stg_green_tripdata and all upstream dependencies
dbt run --select +stg_green_tripdata# Run fct_trips and all downstream dependencies
dbt run --select fct_trips+
# Run dim_zones plus everything upstream AND downstream
dbt run --select +dim_zones++my_model— buildsmy_modeland everything upstream of it (all its ancestors)my_model+— buildsmy_modeland everything downstream of it (all its descendants)+my_model+— both directions. Everything upstream, the model itself, and everything downstream
References:
https://medium.com/@suraj11nag/introduction-to-dbt-2f2ac93a3961







