I would like to change column names in dbt model using mapping table:

So instead this one:

to have table with NewColumnName values.
How I can do this. Thanks in advance!
You could:
for loop to iterate over the old column names in the old table, and the values of the newcolumnname column in the mapping table; andSo, one possible approach would be:
{% set old_column_names = dbt_utils.get_filtered_columns_in_relation(from=ref('old_names')) %}
{% set new_column_names = dbt_utils.get_column_values(ref('mapping_table'), 'newcolumnname') %}
select
{% for old_column_name in old_column_names %}
{{ old_column_name }} as {{ new_column_names[loop.index0] }}
{% if not loop.last -%} , {% endif -%}
{% endfor %}
from {{ ref('old_names') }}
This is assuming that the order and number of the columns from the base model will always be the same as the one in the mapping table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With