When dimension history must be preserved for point-in-time analytics.
Slowly Changing Dimension AI Prompt
This prompt builds a Type 2 Slowly Changing Dimension pattern for attributes that require full history. It is useful when descriptive records such as customers, products, or account metadata change over time and reporting must reflect both current and historical truth. The answer should separate tracked and non-tracked changes clearly.
Implement a Type 2 Slowly Changing Dimension (SCD2) for the table {{dim_table}} in {{database_type}}.
Natural key: {{natural_key}}
Tracked attributes (trigger new version): {{tracked_columns}}
Non-tracked attributes (overwrite in place): {{non_tracked_columns}}
1. Table design:
- Add columns: surrogate_key (BIGINT IDENTITY), valid_from (DATE), valid_to (DATE), is_current (BOOLEAN)
- valid_to for current rows = '9999-12-31' (sentinel value)
- is_current = TRUE for current rows (redundant but improves query performance)
2. Initial load: INSERT all rows with valid_from = first_seen_date, valid_to = '9999-12-31', is_current = TRUE
3. Incremental merge logic:
For each incoming row:
a. NEW RECORD (natural key not in dim): INSERT with valid_from = today, valid_to = '9999-12-31', is_current = TRUE
b. CHANGED RECORD (tracked columns differ from current version):
- UPDATE existing current row: valid_to = today - 1, is_current = FALSE
- INSERT new row: valid_from = today, valid_to = '9999-12-31', is_current = TRUE
c. UNCHANGED RECORD: no action
d. DELETED RECORD (exists in dim but not in source): optionally set is_current = FALSE
4. Point-in-time query:
SELECT * FROM {{dim_table}} WHERE {{natural_key}} = 'X' AND valid_from <= '{{as_of_date}}' AND valid_to > '{{as_of_date}}'
5. Current records query:
SELECT * FROM {{dim_table}} WHERE is_current = TRUE
(Always faster than the date range query — index on is_current)
6. Non-tracked attribute updates: UPDATE current row in-place, no new version needed
Return: CREATE TABLE DDL, MERGE statement, point-in-time query, and current records query.When to use this prompt
When implementing SCD2 logic in a warehouse or dbt model.
When a team needs a reusable template for versioned dimensions.
When both current and as-of historical views are required.
What the AI should return
Return the table DDL, incremental SCD2 load logic, and example queries for current and point-in-time records. Clearly show how new, changed, unchanged, and optionally deleted records are handled. Include notes on performance, indexing, and how non-tracked attributes are updated in place.
How to use this prompt
Open your data context
Load your dataset, notebook, or working environment so the AI can operate on the actual project context.
Copy the prompt text
Use the copy button above and paste the prompt into the AI assistant or prompt input area.
Review the output critically
Check whether the result matches your data, assumptions, and desired format before moving on.
Chain into the next prompt
Once you have the first result, continue deeper with related prompts in Data Warehouse Patterns.
Frequently asked questions
What does the Slowly Changing Dimension prompt do?+
It gives you a structured data warehouse patterns starting point for data engineer work and helps you move faster without starting from a blank page.
Who is this prompt for?+
It is designed for data engineer workflows and marked as beginner, so it works well as a guided starting point for that level of experience.
What type of prompt is this?+
Slowly Changing Dimension is a template. You can copy it as-is, adapt it, or use it as one step inside a larger workflow.
Can I use this outside MLJAR Studio?+
Yes. The prompt text works in other AI tools too, but MLJAR Studio is the best fit when you want local execution, visible Python code, and reusable notebooks.
What should I open next?+
Natural next steps from here are Data Vault Design, Fact Table Loading Pattern, Medallion Architecture Design.