Nonprofit Donor Cohort Retention and Decay Pipeline Builder
Builds a production-ready SQL and Python data pipeline to calculate donor retention curves and churn rates across campaign tiers.
Use this template when developing recurring analytical workflows to evaluate non-profit donor decay, multi-year retention rates, and gift size transitions across historical fundraising drives.
Role: Lead Nonprofit Analytics Architect with deep expertise in donor lifetime modeling and CRM warehousing.
Context
- Mission Sector: {{nonprofit_cause_sector}}
- Source Platform: {{primary_crm_platform}}
- Historical Analysis Range: {{historical_window_years}}
- Sustaining Gift Cutoff: {{recurring_gift_threshold}}
- Time Granularity: {{cohort_granularity}}
- Target SQL Engine: {{reporting_database_dialect}}
Task
Create a dual SQL and Python data pipeline script that transforms transactional giving history from {{primary_crm_platform}} into cohort-based donor retention matrices, churn indicators, and multi-year donor survival curves.
Method
- Formulate a Common Table Expression (CTE) in {{reporting_database_dialect}} to aggregate individual transaction logs by unique donor ID and acquisition date.
- Bucket each donor into an initial acquisition cohort based on {{cohort_granularity}} across {{historical_window_years}}.
- Calculate activity flags for subsequent periods by checking giving events against {{recurring_gift_threshold}}.
- Write a pivot query to compute active retention percentages per cohort index period.
- Develop a Python wrapper script using sqlalchemy to execute the database query and extract the cohort matrix.
- Compute average cohort decay rates, year-over-year retention velocity, and lapse probabilities using pandas.
- Format the resulting retention matrix into an exportable summary table and log key attrition metrics.
Constraints
- MUST write dialect-specific SQL conforming precisely to {{reporting_database_dialect}} syntax standards.
- MUST NOT rely on proprietary third-party libraries outside of standard open-source Python analytics packages.
- Database queries MUST account for recurring vs. one-time gifts using {{recurring_gift_threshold}} logic.
- Calculations must handle dormant donor reactivation without artificially inflating new acquisition cohorts.
Output format
Deliver the solution in the following sequence:
- SQL Query Specification: Formatted, documented SQL CTEs ready for database deployment.
- Python Pipeline Script: End-to-end Python script containing query execution, matrix calculation, and cohort exports.
- Operational Notes: Instructions on parameterizing cohort timeframes and interpreting calculated decay metrics.
Self-review
- Ensure SQL window functions correctly partition by donor identifier without duplicate counts.
- Confirm that Python transformations handle sparse matrices where older cohorts lack recent activity.
- Verify all 6 provided variables are explicitly referenced within the queries or pipeline code.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.