{{ config(materialized="table", unique_key=["date", "id_deal"]) }} with int_dates as (select * from {{ ref("int_dates") }}), int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}), int_core__deal as (select * from {{ ref("int_core__deal") }}) select distinct d.year_number as year, d.month_of_year as month, d.day_of_month as day, d.date_day as date, u.id_deal, deal.main_deal_name, deal.main_billing_country_iso_3_per_deal, d.month_start_date as first_day_month, d.month_end_date as last_day_month from int_core__unified_user u inner join int_dates d on d.date_day >= date(u.created_date_utc) inner join int_core__deal deal on deal.id_deal = u.id_deal where -- include only up-to yesterday now()::date > d.date_day and ( -- keep all last day of the month d.date_day = d.month_end_date -- keep yesterday or now()::date = d.date_day + 1 )