Estimated Time: 40 minutes
You're working on a health data analytics platform that tracks Community Health Worker (CHW) activities across multiple regions in Kenya. CHWs visit households to provide maternal health services, child health assessments, and family planning services.
The analytics team needs a monthly aggregated view of CHW performance to power their dashboards. Currently, they have a detailed fact table with every visit/activity, but they need monthly rollups for better performance and analysis.
Build a dbt model that aggregates CHW activity data by month and CHW, calculating key performance metrics.
You need to create/complete three files:
- Aggregate data from the fact table
- Calculate required metrics
- Use proper dbt incremental strategy
- Add appropriate configurations
- Implement the month assignment logic (see business requirements)
- Make it reusable across models
- Document the model and columns
- Add data quality tests
Before you start coding, review these files:
business_requirements.md- What metrics to calculate and business rulesschema_documentation.md- Source table schemas and column descriptionssample_data.sql- Sample data showing what the source tables containexpected_output.md- What your final model output should look like
- Read
business_requirements.mdcarefully - Note the special month assignment rule (26th cutoff)
- Identify which source columns you need
- Open
macros/month_assignment.sql - Implement the month assignment logic
- Handle edge cases (NULL dates, year boundaries)
- Open
starter_code/chw_activity_monthly.sql - Add proper dbt config block
- Write SQL to aggregate data
- Use your macro for month assignment
- Add GROUP BY and aggregation logic
- Create
schema.ymlfile - Document model purpose and columns
- Add at least 3 tests (not_null, unique, relationships, etc.)
Your model should:
- Materialize as an incremental table
- Use
delete+insertincremental strategy - Set
unique_keyto prevent duplicates - Handle late-arriving data properly
- Be performant
Your SQL should:
- Be readable and well-formatted
- Use CTEs (Common Table Expressions) for clarity
- Handle NULL values appropriately
- Use the
ref()function to reference source tables - Comment complex logic
Your solution should:
- Not create duplicates (same CHW + month appears once)
- Handle edge cases (NULL dates, deleted records, etc.)
- Include appropriate tests in schema.yml
💡 Month Assignment Logic: Activities on/after the 26th of a month are assigned to the NEXT month.
💡 Incremental Strategy: delete+insert is best when you need to handle late-arriving data or updates to historical records.
💡 Window Functions: You might need these for deduplication or ranking (though not required for basic solution).
💡 Testing: Think about what could go wrong. Duplicates? NULLs in key fields? Negative counts?
You'll find a skleton dbt model in starter_code/chw_activity_monthly.sql. It has:
- Basic structure
- Some starter SQL to build upon
You'll find a macro template in macros/month_assignment.sql with:
- Function signature
- Ensure all three files are complete
- Review your code for obvious errors
- Check that you've addressed all business requirements