Mrinal
Mrinal

Reputation: 115

Nested variables in dbt_project.yml file of dbt

Below is my code in dbt_project.yml file

vars:
    # Variable 
    project1:
vendor:
    ABC
    DEF

ABC:
    model:
        name: model123
    case_types:
        name: CASE1
        name: CASE2
        name: CASE3
        name: CASE4

the way i am trying to access this in model is below. When the below code is run the values in src are ('model' & 'case_types') respectively. How do i access values of these( 'model123, CASE1, CASE2....)

{% set vars1 =  var('ABC') %}
{% for src in vars1 %}
  {{log(src, True)}}
{% endfor %}

Upvotes: 4

Views: 3489

Answers (2)

C. Ellis
C. Ellis

Reputation: 71

You can reference nested variables using square bracket notation following the var('') command that references the top-most variable.

For example, if I have a dbt_project.yml file with the following:

vars:
  model_1:
    start_date: 2020-01-31
    end_date: 2020-12-31
  model_2:
    start_date: 2021-01-31
    end_date: 2021-12-31

I could reference the sub variables by:

SELECT *
FROM table
WHERE 
  model_number = 1 
  AND as_of_date BETWEEN '{{ var("model_1")["start_date"] }}'::DATE AND '{{ var("model_1")["end_date"] }}'::DATE

I have not, however, been able to reference a variable within a {% %} block.

Upvotes: 7

Dat Nguyen
Dat Nguyen

Reputation: 316

I don't think dbt currently supporting the nested variables

It's just 'nested' as scope where the variable can be used

vars:
  # The `start_date` variable will be accessible in all resources
  start_date: '2016-06-01'

  # The `platforms` variable is only accessible to resources in the my_dbt_project project
  my_dbt_project:
    platforms: ['web', 'mobile']

  # The `app_ids` variable is only accessible to resources in the snowplow package
  snowplow:
    app_ids: ['marketing', 'app', 'landing-page']

Official doc here

Upvotes: 0

Related Questions