Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 %}
like image 728
Mrinal Avatar asked Oct 15 '25 15:10

Mrinal


1 Answers

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.

like image 121
C. Ellis Avatar answered Oct 17 '25 20:10

C. Ellis