fix(compare): census-sourced FSM/EAL benchmarks; never fall back across measure definitions

The FSM chip anchored against disadvantaged_pct (a different measure,
FSM6+CLA) whenever fsm_pct was null — which it always was, since the
performance df has no fsm_pct. New fact_census_benchmarks mart supplies
pupil-weighted FSM/EAL means per phase; the KS2-column medians that
produced a bogus 50% 'secondary disadvantaged' anchor are gone.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0146VHeLAWjDVE2B5uU67jCB
This commit is contained in:
Tudor
2026-07-16 19:05:15 +01:00
co-authored by Claude Fable 5
parent 9773483221
commit 1d855f3c17
7 changed files with 142 additions and 24 deletions
@@ -133,6 +133,16 @@ models:
- name: year
tests: [not_null]
- name: fact_census_benchmarks
description: >
State-school context benchmarks from the pupil census — one row per
phase (primary/secondary), latest census year. fsm_pct/eal_pct are
pupil-weighted means; consumers label them "state-school average
(computed from our dataset)", never "England average".
columns:
- name: phase
tests: [not_null, unique]
- name: fact_admissions
description: School admissions — one row per URN per year
columns:
@@ -0,0 +1,39 @@
{{ config(materialized='table') }}
-- Mart: state-school context benchmarks from the pupil census — one row per
-- phase, latest census year. Computed at import time (never per request).
-- fsm_pct / eal_pct are pupil-weighted means, i.e. "what % of pupils", not
-- "the median school" — this matches how DfE quotes national FSM/EAL rates.
-- Consumers must label these "state-school average (computed from our
-- dataset)" (spec §8.6), never "England average".
with latest as (
select max(year) as year from {{ ref('fact_pupil_characteristics') }}
),
classified as (
select
case
when p.phase_type_grouping ilike '%primary%' then 'primary'
when p.phase_type_grouping ilike '%secondary%' then 'secondary'
end as phase,
p.total_pupils,
p.fsm_pct,
p.eal_pct,
l.year
from {{ ref('fact_pupil_characteristics') }} p
join latest l on p.year = l.year
where p.total_pupils is not null and p.total_pupils > 0
)
select
phase,
max(year) as year,
round((sum(fsm_pct * total_pupils) filter (where fsm_pct is not null)
/ nullif(sum(total_pupils) filter (where fsm_pct is not null), 0))::numeric, 1) as fsm_pct,
round((sum(eal_pct * total_pupils) filter (where eal_pct is not null)
/ nullif(sum(total_pupils) filter (where eal_pct is not null), 0))::numeric, 1) as eal_pct,
round(percentile_cont(0.5) within group (order by total_pupils))::integer as median_pupils
from classified
where phase is not null
group by phase