Build Slowly Changing Dimension Validity Ranges from Daily Page Snapshots
Last updated: November 29, 2026
Quick Overview
You receive a daily snapshot table (snapshot_date, page_id, category, country) for Facebook Pages. Convert it to a type 2 dimension with one row per page per contiguous period of unchanged attributes: valid_from, valid_to (exclusive, null if current), handling pages that disappear and reappear.
Meta
November 29, 20260
0
0 solved
You receive a daily snapshot table (snapshot_date, page_id, category, country) for Facebook Pages. Convert it to a type 2 dimension with one row per page per contiguous period of unchanged attributes: valid_from, valid_to (exclusive, null if current), handling pages that disappear and reappear.
Meta's warehouse keeps many dimensions as daily full snapshots, which are expensive to query for history. Converting them to validity ranges is a classic data engineering onsite task: it tests change detection with shift, gaps-and-islands grouping, and careful handling of missing days and the current open-ended row.
What the Interviewer Expects
- Detect changes by comparing attributes with the previous snapshot per page
- Treat a missing day (page absent) as breaking the run
- Group contiguous unchanged runs with a cumulative sum of change flags
- Produce valid_from and exclusive valid_to, null for the current version
- Handle null attribute values as comparable
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you build this incrementally each day instead of from full history?
- How would you query the dimension as of a given date efficiently?
- What if a page's attribute flips back and forth daily due to a bug?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
One row per page per snapshot day when the page exists. Attributes compared: category and country, where null equals null. If a page is missing for a day, the version ends at the first missing day; wh...
Approach
Sort by page and date. A new version starts when (a) it is the page's first row, (b) the previous row is not exactly one day earlier, or (c) any attribute differs from the previous row. Nulls are fill...