Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. dbt_utils highlights

dbt · Macros, Packages & Advanced

dbt_utils highlights

Mediumdbt-33
dbt_utilssurrogate_keydate_spineunion_relations

Question

Explain dbt_utils helpers: generate_surrogate_key, date_spine, and union_relations.

Solution

dbt_utils is the most common community package. Three interview favorites:

1. generate_surrogate_key

Builds a hashed surrogate key from one or more columns (replaces older surrogate_key naming in many versions):

select
    {{ dbt_utils.generate_surrogate_key(['customer_id', 'order_id']) }} as order_sk,
    customer_id,
    order_id
from {{ ref('stg_orders') }}

Use for wide composite grains or when source keys are messy.

2. date_spine

Generates a continuous date (or timestamp) series, great for filling gaps in time-series marts:

{{ dbt_utils.date_spine(
    datepart="day",
    start_date="cast('2020-01-01' as date)",
    end_date="cast('2025-01-01' as date)"
) }}

Then left-join facts to the spine so missing days appear as zeros/nulls intentionally.

3. union_relations

Unions many similarly shaped relations (e.g. monthly shards or shard-per-source tables) with optional column harmonization:

{{ dbt_utils.union_relations(
    relations=[ref('events_a'), ref('events_b')]
) }}

Mental model

generate_surrogate_key → stable hashed PK helpers
date_spine             → calendar scaffold for sparse facts
union_relations        → vertical stitch of lookalike tables

Interview tip: Name these three quickly; they show you have used real dbt projects, not only ref and run.

PreviousNext