Well… I’m trying to write a simple if but it wasn’t work …
I’ve crerated a macro that receives three parameters and I need to test the value of parameters to decide something in my code…
Example:
{% macro convert_data_type(datasetpreraw,datasetraw,tablename) %}
{# Define a query que irá obter a lista de colunas convertidas #}
{% set dataset %}
'{{ datasetraw }}'
{% endset %}
{% set table_ %}
'{{ tablename }}'
{% endset %}
...
{{ log(dbt_utils.pretty_log_format(dataset), info=True) }}
{{ log(dbt_utils.pretty_log_format(table_), info=True) }}
{% if dataset == 'analytics_reporting' and table_ in ['raw_kwai_ad_data_reports','raw_kwai_ad_set_reports','raw_kwai_campaigns_reports','raw_kwai_video_reports'] %}
DATE(TIMESTAMP_MILLIS(cast(time as int64))) as datepart
{% elif dataset == 'analytics_reporting' and table_ == 'raw_teads_campaign_reports' %}
safe_cast (format_date('%Y-%m-%d', parse_date('%Y/%m/%d', '' || day))as date) as datepart
{{ log(dbt_utils.pretty_log_format("Teste de falha do log"), info=True) }}
{% elif dataset == 'analytics_reporting' %}
date(concat(substr(cast(ga_date as string),1,4),'-',substr(cast(ga_date as string),5,2),'-',substr(cast(ga_date as string),7,2))) as datepart
{% elif dataset == 'raw_adjust' %}
date as datepart
{% elif dataset == 'raw_data_royal' %}
date(created_at) as datepart
...
{% else %}
{{ log(dbt_utils.pretty_log_format("Nao entrei no if"), info=True) }}
{% endif %}
from {{ datasetpreraw }}.{{ tablename }}
{% endmacro %}
but the code always exit by Else condition…
I’ve tried several combinations:
{% set dataset %}
{{ datasetraw }}
{% endset %}
{% set table_ %}
{{ tablename }}
{% endset %}
{% if dataset == 'analytics_reporting' and table_ in ['raw_kwai_ad_data_reports','raw_kwai_ad_set_reports','raw_kwai_campaigns_reports','raw_kwai_video_reports'] %}
DATE(TIMESTAMP_MILLIS(cast(time as int64))) as datepart
{% elif dataset == 'analytics_reporting' and table_ == 'raw_teads_campaign_reports' %}
safe_cast (format_date('%Y-%m-%d', parse_date('%Y/%m/%d', '' || day))as date) as datepart
{{ log(dbt_utils.pretty_log_format("Teste de falha do log"), info=True) }}
{% elif dataset == 'analytics_reporting' %}
date(concat(substr(cast(ga_date as string),1,4),'-',substr(cast(ga_date as string),5,2),'-',substr(cast(ga_date as string),7,2))) as datepart
{% elif dataset == 'raw_adjust' %}
date as datepart
{% elif dataset == 'raw_data_royal' %}
date(created_at) as datepart
...
{% else %}
{{ log(dbt_utils.pretty_log_format("Nao entrei no if"), info=True) }}
{% endif %}
or
{% set dataset %}
{{ datasetraw }}
{% endset %}
{% set table_ %}
{{ tablename }}
{% endset %}
{% if dataset == analytics_reporting and table_ in [ raw_kwai_ad_data_reports , raw_kwai_ad_set_reports , raw_kwai_campaigns_reports , raw_kwai_video_reports ] %}
DATE(TIMESTAMP_MILLIS(cast(time as int64))) as datepart
{% elif dataset == analytics_reporting and table_ == raw_teads_campaign_reports %}
safe_cast (format_date('%Y-%m-%d', parse_date('%Y/%m/%d', '' || day))as date) as datepart
{{ log(dbt_utils.pretty_log_format("Teste de falha do log"), info=True) }}
{% elif dataset == analytics_reporting %}
date(concat(substr(cast(ga_date as string),1,4),'-',substr(cast(ga_date as string),5,2),'-',substr(cast(ga_date as string),7,2))) as datepart
{% elif dataset == raw_adjust %}
date as datepart
{% elif dataset == raw_data_royal %}
date(created_at) as datepart
...
{% else %}
{{ log(dbt_utils.pretty_log_format("Nao entrei no if"), info=True) }}
{% endif %}
but none of them can make the comparison of the variable’s content work in the if conditions
I know it must be a stupid doubt but I really need I help here…