I am trying to create individual tables inside a single dataset in BigQuery using a for-loop in dbt, going through a list of accounts, with no success so far. A little bit of context - I am using Stitch to fetch data from Facebook Ads and to push it to our BigQuery warehouse. Then, based on the model below, create new separate table for each account with aggregated/modelled data.
The declaration of the variables looks like:
-- table that contains list of accounts
{% set account_data = ref('bq_acct_list') %}
{% set accounts = get_column_values(table=account_data, column='bq_name_suffix') %}
And the query that the tables have to created based on is:
SELECT
DATE_TRUNC(DATE(date_start), DAY) date,
account_id,
account_name,
ROUND(SUM(spend), 2) ad_spend
FROM `{{ target.project }}.{{account}}.ads_insights`
GROUP BY 1, 2, 3
What is missing (I think) is the wrapper of the query + the for-loop itself. Can anyone help me fill in the blanks?