Build An Incremental dbt Model
Writes an incremental dbt model with correct unique key, late-arriving handling, and backfill strategy.
Prompt
ROLE: You are an analytics engineer building production dbt models. CONTEXT: Create an incremental model named [MODEL_NAME] from source [SOURCE_TABLE] (schema [SCHEMA]). Target warehouse: [DATABASE_ENGINE]. Grain: one row per [GRAIN]. Late-arriving data window: up to [LATENESS]. Unique/business key: [UNIQUE_KEY]. TASK: 1. Decide the incremental strategy (append, merge/upsert, insert_overwrite by partition) and justify it for this grain. 2. Write the dbt model SQL with the proper config block, the is_incremental() filter using an updated-at watermark with a safety lookback for late data, and the unique_key. 3. Add dbt tests (unique, not_null, relationships, accepted_values) for the key columns in YAML. 4. Explain how a full-refresh backfill behaves vs an incremental run. 5. Note idempotency: running twice must not duplicate or drift. OUTPUT FORMAT: Strategy rationale -> Model ```sql``` -> schema.yml tests -> Backfill behavior -> Idempotency notes. CONSTRAINTS: The is_incremental() predicate must include a lookback to catch late-arriving rows. Use merge with unique_key to stay idempotent on the chosen engine. No SELECT *; select explicit columns. State the watermark column and its timezone assumption.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueForces explicit intermediate reasoning instead of jumping to a conclusion, which improves accuracy on hard tasks.
Learn this techniquePins the response to a defined structure so it drops straight into your workflow.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Data Analysis & SQL
Translate Business Questions Into SQL
Turns a plain-English stakeholder question into a correct, well-commented SQL query against a known schema.
Optimize A Slow SQL Query
Diagnoses why a query is slow and rewrites it with targeted, explained optimizations and an index plan.
Debug A SQL Query That Returns Wrong Results
Systematically finds the logic error producing incorrect numbers and delivers a corrected, verified query.
Explain An Unfamiliar SQL Query In Plain English
Reverse-engineers a complex inherited query into a clear narrative, business meaning, and risk list.