ETLMCPData Conveyance Works
Log in Start free
Guides

A date spine for etlmcp tables in PostgreSQL and BigQuery

Days with no delivery have no rows in an etlmcp table. Build a calendar of days, join your data onto it and fill only additive metrics with 0.

An etlmcp table holds a row only for a day and an object (a campaign, an ad, a source / medium) that had data. A day with no spend, no sessions and no orders has no row at all. GA4, for one, leaves out a row whose metrics are all zero unless the request asks for empty rows, and etlmcp does not ask, so a campaign that sat idle on a Sunday is simply missing that Sunday. When you chart such a table, the chart skips the day; when you average over "the last 7 rows", you average over more than 7 days. The fix is a date spine: a list of every day in your range, built in SQL, with your data LEFT JOINed onto it. Fill the additive metrics with 0 on the empty days, and leave the ratios and the non-additive counts empty.

Why the gaps are there

etlmcp loads the rows the platform's report returns and adds none of its own. The PostgreSQL destination inserts exactly the rows of the run, and a run whose report came back empty deletes the pipeline's rows in that date range and writes nothing in their place. Nothing in the load turns a missing day into a zero row, so the gaps you see in the table are the gaps in the report.

A missing row means "nothing happened". It becomes wrong only when a query treats rows as days:

  • A line chart joins the last day with sales to the next day with sales and hides the dip.
  • AVG(cost) over a campaign's rows is the average of its active days, not of the calendar.
  • COUNT(*) of a week's rows is the number of active days, not 7.
  • A window such as ROWS BETWEEN 6 PRECEDING AND CURRENT ROW reaches back 7 rows, which can be weeks.

The table you are joining

In PostgreSQL, etlmcp creates the table you named, in the schema you chose (public when you chose none). It has a date column of type date, the common columns, the platform's extras, and _etlmcp_pipeline_id, which says which pipeline wrote each row. GA4 leaves the ad delivery ones empty: it puts the source / medium in adgroup_id and stores its own metrics as extras. Extras keep their dotted names, such as extra.sessions, and need double quotes in SQL. The PostgreSQL destination lists every column.

The BigQuery destination is coming. When it is available, its table has the same columns with the dots turned into underscores (extra_sessions) and date as a DATE.

PostgreSQL: generate_series

generate_series(start, stop, step) gives one row per value from start to stop. With timestamps and an interval of '1 day', that is one row per day, both ends included. Cast the result to date so it matches the table's date column. Watch two quiet cases: with a positive step it returns no rows when start is after stop, and it returns no rows when any input is NULL. A spine whose start comes from a subquery that found nothing is therefore empty, and so is every report built on it. A step of zero is an error.

This query gives one row per GA4 campaign per day for the first quarter, with sessions and revenue filled where GA4 reported nothing:

WITH spine AS (
  SELECT d::date AS day
  FROM generate_series(timestamp '2026-01-01', timestamp '2026-03-31', interval '1 day') AS d
),
campaigns AS (
  SELECT DISTINCT campaign_id, campaign_name
  FROM public.ga4_daily
  WHERE date BETWEEN '2026-01-01' AND '2026-03-31'
)
SELECT
  s.day,
  c.campaign_id,
  c.campaign_name,
  COALESCE(SUM(f."extra.sessions"), 0) AS sessions,
  COALESCE(SUM(f."extra.revenue"), 0)  AS revenue
FROM spine s
CROSS JOIN campaigns c
LEFT JOIN public.ga4_daily f
  ON f.date = s.day
 AND f.campaign_id IS NOT DISTINCT FROM c.campaign_id
 AND f.campaign_name IS NOT DISTINCT FROM c.campaign_name
GROUP BY s.day, c.campaign_id, c.campaign_name
ORDER BY c.campaign_id, s.day;

Replace public.ga4_daily with your table. The SUM folds the several source / medium rows GA4 gives a campaign on one day into one; drop it, and the GROUP BY, if your table has one row per object per day already. etlmcp stores a missing campaign id or name as an empty string, so plain = works on its tables; IS NOT DISTINCT FROM is a safe default if your table can also hold NULL ids from another source. If one table holds several pipelines, add f._etlmcp_pipeline_id = '…' to the join and to the campaigns subquery.

For a total across all campaigns, skip the cross join and join the spine to a per-day sum.

BigQuery: GENERATE_DATE_ARRAY and UNNEST

GENERATE_DATE_ARRAY(start_date, end_date) returns an array of dates with both ends included. Its step defaults to one day; you can pass another, such as INTERVAL 1 WEEK. It returns an empty array when start is after end with a positive step, and a NULL array when an input is NULL. UNNEST turns the array into rows, one per element; an empty or NULL array gives zero rows.

WITH spine AS (
  SELECT day
  FROM UNNEST(GENERATE_DATE_ARRAY(DATE '2026-01-01', DATE '2026-03-31')) AS day
),
daily AS (
  SELECT date, SUM(extra_sessions) AS sessions, SUM(extra_revenue) AS revenue
  FROM `my_dataset.ga4_daily`
  WHERE date BETWEEN '2026-01-01' AND '2026-03-31'
  GROUP BY date
)
SELECT s.day, COALESCE(d.sessions, 0) AS sessions, COALESCE(d.revenue, 0) AS revenue
FROM spine s
LEFT JOIN daily d ON d.date = s.day
ORDER BY s.day;

What to fill with 0, and what not

COALESCE returns the first of its arguments that is not NULL, so COALESCE(x, 0) turns an empty day into 0 and leaves a real value alone. That is right only for metrics you can add across days, where "no row" and "zero" mean the same thing:

  • Fill: impressions, clicks, cost, conversions, conversion_value, and GA4's extra.sessions, extra.engaged_sessions, extra.page_views, extra.key_events, extra.orders and extra.revenue.
  • Leave NULL: ctr, cpc, cpm and cpa. etlmcp already leaves them empty on a row whose denominator is 0, and on a day with no row they mean nothing either. On a day with no clicks, the cost per click is undefined, not 0, and a 0 drags every average down. Work ratios out from the filled sums instead: see ratios from sums.
  • Leave NULL: GA4's extra.total_users and extra.new_users. They count distinct users and do not add up across days or campaigns, so neither a 0 nor a sum is meaningful. See non-additive metrics.
  • Leave text alone: currency, names and ids. Take them from the object side of the join, as the PostgreSQL query does with campaigns.

Using the spine

With a row for every day, a rolling 7-day sum can use rows again: SUM(sessions) OVER (PARTITION BY campaign_id ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) now covers exactly 7 days. COUNT(*) per week returns 7, and COUNT(*) FILTER (WHERE cost > 0) in PostgreSQL gives the active days. Put the spine query in a view, so that your BI tool reads a table with no gaps.

Pick the spine's end with care. If it runs to today, today is a row of zeros until the day's sync has run, and the most recent days can still change as the platform restates them. The range replace page explains how a later run rewrites those days.

Related

Sources

  1. 9.27. Set Returning Functions — www.postgresql.org, checked
  2. 9.19. Conditional Expressions — www.postgresql.org, checked
  3. Array functions — docs.cloud.google.com, checked
  4. Query syntax — docs.cloud.google.com, checked
  5. Method: properties.runReport — developers.google.com, checked