The source() function helps you tell dbt where your raw data tables are. It connects your project to the original data so you can use it safely and clearly.
0
0
source() function for raw tables in dbt
Introduction
When you want to use raw data tables from your database in your dbt models.
When you want to document and track the source of your data for clarity and auditing.
When you want to build transformations on top of raw tables without hardcoding table names.
When you want to make your dbt project easier to maintain by referencing sources consistently.
Syntax
dbt
source('source_name', 'table_name')
source_name is the name you gave to your data source in your sources.yml file.
table_name is the raw table name inside that source.
Examples
This selects all data from the
customers table inside the raw_data source.dbt
select * from {{ source('raw_data', 'customers') }}
This gets the
id and name columns from the orders table in the sales_db source, filtering for completed orders.dbt
select id, name from {{ source('sales_db', 'orders') }} where status = 'completed'
Sample Program
This example shows how to define a source called raw_data with a table customers in the sources.yml file. Then, in a dbt model, it selects the id and email of active customers from that source.
dbt
/* In your sources.yml file */ version: 2 sources: - name: raw_data schema: raw_data tables: - name: customers /* In your dbt model SQL file */ select id, email from {{ source('raw_data', 'customers') }} where active = true
OutputSuccess
Important Notes
Always define your sources in sources.yml before using source() in your models.
Using source() helps dbt track data lineage and improves documentation.
Summary
source() connects your dbt models to raw tables safely.
It uses names from your sources.yml file for clarity and maintenance.
It helps you write cleaner, more understandable SQL in dbt projects.