Engagement Realization and Margin Leakage Audit Pipeline
Generate a robust data audit script to pinpoint fee write-downs, unbilled hours, and engagement margin leakage across professional services practices.
Use this template when scoping or debugging profitability erosion across consulting or advisory portfolios. It generates an enterprise-grade extraction and calculation script that uncovers hidden project write-downs and billing discrepancies.
Role: Lead Professional Services Analytics Architect specializing in practice financial forensics.
Context
- Target practice unit: {{practice_area}}
- Time and billing schema: {{timesheet_schema}}
- Master fee schedules: {{billing_rate_cards}}
- Baseline project margin target: {{target_margin_threshold}}
- Historical audit window: {{variance_window_months}}
- Target database dialect: {{output_db_dialect}}
Task
Write an optimized, production-grade {{output_db_dialect}} and Python extraction script that calculates engagement-level realization rates, identifies unbilled fee leakage, and flags professional services projects that fail to meet {{target_margin_threshold}}.
Method
- Extract raw billable hours, non-billable overhead, and partner write-downs from {{timesheet_schema}} spanning the last {{variance_window_months}}.
- Join logged consultant seniority tiers against standard rates defined in {{billing_rate_cards}} to establish baseline book revenue versus actual billed revenue.
- Compute effective hourly rate (EHR), realization percentage, and project-level margin deviations for all engagements within {{practice_area}}.
- Calculate write-off velocity to differentiate between scope-creep write-downs and client invoice renegotiations.
- Implement an anomaly detection filter identifying engagements where hours logged exceed agreed milestone caps without corresponding change orders.
- Aggregate realization benchmarks grouped by engagement partner, project lead, and client tier.
- Format the complete transformation logic into an idempotent {{output_db_dialect}} query and wrap it in a parameterized Python execution script with logging and transaction control.
Constraints
- MUST implement defensive zero-division guards and null handling across all rate and margin computations.
- MUST NOT rely on hardcoded environment parameters, credentials, or deprecated library functions.
- The script must include full PEP 484 type annotations and modular function docstrings.
- Queries must be optimized to prevent table scans on large transactional billing tables.
Output format
- Component 1: Pipeline architecture and schema dependency summary (150-200 words)
- Component 2: Complete executable script including configuration dictionary, SQL transformation queries, and error handling
- Component 3: Data validation tests and automated alerting thresholds (100-150 words)
Self-review
- Verify all 6 variables are referenced accurately in the generated script logic.
- Ensure realization formulas mathematically account for both pre-bill write-downs and post-bill credits.
- Confirm syntax conforms strictly to {{output_db_dialect}}.
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.