What if you could fix messy data docs with just one simple trick?
Why Doc blocks for reusable descriptions in dbt? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have many data models and columns in your project. You want to explain what each column means, but you have to write the same description again and again for similar columns in different models.
Writing descriptions manually for every column is slow and boring. It's easy to make mistakes or forget to update some descriptions. This causes confusion for anyone reading your data documentation.
Doc blocks let you write a description once and reuse it everywhere. This keeps your documentation consistent and saves time. When you update the doc block, all places using it update automatically.
description: 'User ID representing the unique identifier for a user' description: 'User ID representing the unique identifier for a user'
docs: user_id: 'User ID representing the unique identifier for a user' columns: - name: user_id description: "{{ doc('user_id') }}" - name: creator_id description: "{{ doc('user_id') }}"
You can maintain clear, consistent, and up-to-date data documentation effortlessly across your entire project.
A data analyst quickly understands that both user_id and creator_id columns refer to the same concept without confusion, thanks to shared doc blocks.
Writing descriptions once and reusing them saves time.
Doc blocks keep documentation consistent and easy to update.
Everyone on the team understands data better with clear docs.
Practice
doc blocks in dbt?Solution
Step 1: Understand what doc blocks do
Doc blocks let you write descriptions once and reuse them in multiple places.Step 2: Identify the main purpose
This saves time and keeps documentation consistent across your project.Final Answer:
To write descriptions once and reuse them across models -> Option AQuick Check:
Doc blocks = reusable descriptions [OK]
- Thinking doc blocks create tables
- Confusing doc blocks with SQL performance tools
- Using doc blocks to define variables
customer_description in dbt?Solution
Step 1: Recall the correct keyword for doc blocks
The correct keyword to define doc blocks isdocs:.Step 2: Match the syntax
docs: customer_description: "Description text here" usesdocs:followed by the doc block name and description, which is correct.Final Answer:
docs:\n customer_description: "Description text here" -> Option AQuick Check:
Define doc blocks with docs: [OK]
- Using 'doc:' instead of 'docs:'
- Adding extra underscores in keyword
- Misnaming the block keyword
docs: sales_desc: "This model contains sales data aggregated by month."What will be the output of this Jinja code in a model description?
{{ doc("sales_desc") }}Solution
Step 1: Understand what
This Jinja function inserts the text from the doc block named{{ doc("sales_desc") }}doessales_desc.Step 2: Match the doc block content
The doc blocksales_desccontains the description "This model contains sales data aggregated by month." so that text will be output.Final Answer:
This model contains sales data aggregated by month. -> Option DQuick Check:
doc("name") outputs doc block text [OK]
- Expecting the literal string instead of description
- Confusing doc block name with output
- Assuming an error if doc block exists
docs: product_info: "Details about product sales."But when you use
{{ doc("product_info") }} in your model, it shows an error. What is the likely cause?Solution
Step 1: Check common causes of doc block errors
One common cause is a mismatch between the doc block name and the name used indoc().Step 2: Verify usage rules
Doc blocks are defined in YAML files, not inside SQL files, and descriptions can have spaces. Runningdbt compileis standard but not usually the cause of this error.Final Answer:
The doc block name is misspelled in thedoc()call -> Option BQuick Check:
Name mismatch causes doc block errors [OK]
- Defining doc blocks inside SQL files
- Assuming spaces cause errors
- Skipping dbt compile step
Solution
Step 1: Define reusable description in docs block
Place the description underdocs:in a YAML file likeschema.ymlwith a name likecustomer_history.Step 2: Reuse description with doc() function
Use{{ doc("customer_history") }}in the description field of any model to reuse the text.Final Answer:
Define the doc block under docs: in schema.yml, then use {{ doc("customer_history") }} in model descriptions -> Option CQuick Check:
docs: + doc() = reusable descriptions [OK]
- Writing descriptions repeatedly in SQL files
- Using macros instead of doc blocks for descriptions
- Confusing ref() with doc()
