shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Date spines

Intermediate · 4 min read

In two sentences

Data only has rows for days when something happened, so a day with no sales simply doesn't exist in the result. A date spine is a list of every day you report on; you join your data onto it, and the empty days show up as 0 instead of disappearing.

See it

Daily revenue straight from GROUP BY:

dayrevenue
04-01120
04-0280
04-0495

04-03 is missing. A chart draws a straight line over it, lag() compares 04-04 with 04-02 and calls it "the previous day", and a 7-day average over 7 rows covers 8 days.

With a spine:

dayrevenue
04-01120
04-0280
04-030
04-0495

Without a spine the empty day disappears and the chart skips it; with a spine it shows up as 0

In SQL

Generate the days with sequence and turn the array into rows with UNNEST:

SQL
WITH spine AS (
  SELECT d
  FROM UNNEST(sequence(DATE '2026-04-01', DATE '2026-04-30', INTERVAL '1' DAY)) AS t(d)
),
daily AS (
  SELECT CAST(order_ts AS date) AS d, sum(amount) AS revenue
  FROM orders
  GROUP BY 1
)
SELECT s.d, coalesce(daily.revenue, 0) AS revenue
FROM spine s
LEFT JOIN daily ON daily.d = s.d
ORDER BY s.d;

Per entity (every product, every day), cross join the spine with the entities first:

SQL
SELECT p.product_id, s.d
FROM products p
CROSS JOIN spine s

Then LEFT JOIN the facts. For a level (stock on hand) a missing day doesn't mean 0, it means "same as before". Carry the last known value forward:

SQL
last_value(stock) IGNORE NULLS OVER (PARTITION BY product_id ORDER BY d
                                     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

When to use it

situationspine?
a report or chart by day, week or monthyes
lag / lead, growth vs. the previous period, rolling windowsyes: "previous row" must mean "previous period"
retention or cohort grids (every cohort × every period)yes, spine × cohorts
a daily snapshot per entity (stock, balance, status)yes, spine × entities, carry the value forward
a list of transactions or eventsno: there's nothing to fill
the source was down and didn't collect that daynot with 0: the truth is "unknown", leave it NULL and flag it

Best practices

  • One shared calendar table (dim_date: one row per day, with week, month, fiscal period, holidays) instead of a sequence in every query. Every report then agrees on what "a week" is.
  • Decide the fill rule per measure, not per query. Flows (revenue, orders) fill with 0; levels (stock, balance, price) carry forward; anything that wasn't measured stays NULL.
  • Bound the spine by the reporting period, and stop at yesterday for daily jobs, so today's partial day doesn't look like a drop.

Common mistakes

  • Spine from the data's own min/max. If the first order is on 04-03, a report for April starts on 04-03. Build the spine from the reporting period, not the data.
  • Joining a timestamp to a date. order_ts = d matches only midnight. Cast to date first, or aggregate first.
  • Filling every gap with 0. Revenue: 0 is right. Stock level, account balance, price: 0 is wrong; carry forward.
  • Huge spines. sequence in Trino returns at most 10,000 elements. For long ranges at fine grain, build the spine from a calendar table.

What an interviewer asks next

  • "Month-over-month growth when a month had no sales?" With a spine, that month is 0 and the next month's growth is undefined (divide by zero). Say how you'd show it.
  • "Rolling 7-day active users?" Spine first, then a window over 7 days, not 7 rows.

Go further

The Trino array docs list sequence for dates and timestamps. dbt projects use the date_spine macro from dbt-utils for the same thing.

Practise it

Month-over-Month Revenue Growth with Missing Months, then Rolling 7-Day Active Users for Every Day, then Daily Stock Levels with Gap Filling (hard).