Skip to content

Commit

Permalink
Merge pull request #2341 from opensafely-core/enforcing-index-date-in…
Browse files Browse the repository at this point in the history
…-tutorial

Ensure index date is accounted for in clinical event queries in the tutorial
  • Loading branch information
eli-miriam authored Jan 7, 2025
2 parents 6002a58 + 17579ba commit 9342b84
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/tutorial/more-complex-transformations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ There is another codelist, [`DMRES_COD`][5], that contains a single code indicat

To find the patients with an unresolved diagnosis, we need to find the date of each patient's latest diabetes diagnosis event (if any) and the date of each patient's latest diabetes resolved event (if any), and take only patients where there is a diabetes diagnosis event and no subsequent diabetes resolved event.

We can find the latest event for each patient matching a codelists:
We can find the latest event before the index date for each patient matching a codelists:

```ehrql
from ehrql import codelist_from_csv, show
Expand All @@ -189,12 +189,14 @@ resolved_codes = codelist_from_csv("codelists/nhsd-primary-care-domain-refsets-d
last_diagnosis_date = (
clinical_events.where(clinical_events.snomedct_code.is_in(diabetes_codes))
.sort_by("date")
.where(clinical_events.date <= index_date)
.last_for_patient()
.date
)
last_resolved_date = (
clinical_events.where(clinical_events.snomedct_code.is_in(resolved_codes))
.sort_by("date")
.where(clinical_events.date <= index_date)
.last_for_patient()
.date
)
Expand Down Expand Up @@ -225,12 +227,14 @@ resolved_codes = codelist_from_csv("codelists/nhsd-primary-care-domain-refsets-d
last_diagnosis_date = (
clinical_events.where(clinical_events.snomedct_code.is_in(diabetes_codes))
.sort_by("date")
.where(clinical_events.date <= index_date)
.last_for_patient()
.date
)
last_resolved_date = (
clinical_events.where(clinical_events.snomedct_code.is_in(resolved_codes))
.sort_by("date")
.where(clinical_events.date <= index_date)
.last_for_patient()
.date
)
Expand Down Expand Up @@ -272,12 +276,14 @@ is_registered = (
last_diagnosis_date = (
clinical_events.where(clinical_events.snomedct_code.is_in(diabetes_codes))
.sort_by("date")
.where(clinical_events.date <= index_date)
.last_for_patient()
.date
)
last_resolved_date = (
clinical_events.where(clinical_events.snomedct_code.is_in(resolved_codes))
.sort_by("date")
.where(clinical_events.date <= index_date)
.last_for_patient()
.date
)
Expand Down

0 comments on commit 9342b84

Please sign in to comment.