Skip to content
Alpha: see what works today →
Menu

decisions/medication-matching.md on GitHub, at the commit these pages were built from

Medication matching: which copies are one medication

Status: accepted, 2026-10-31, by the maintainer's ruling on #293 (decision d of the care-gap epic, #287). It is the note medications.md says rules M15 and M16 still need. It depends on medications.md (rows "exposure start", "exposure end", "no end" and "no period": the period it compares), one-meaning-per-term.md (row "1. clinical state and workflow state": M14's period test for conditions, which this follows) and naming.md (row "document, finding": a version that tightens a rule retracts nothing).

Question

The matcher joins two medication records into one entry when they name one drug: one NDC (R5), one RxNorm product code a drug products table lists (R11), or a brand and its generic by a brand generics table (R10). Up to the rule list's version 17 it does so whatever their dates. A person who took metformin from 2019 to 2021 and started it again in 2026 then has one metformin entry, and a rule that reads the entry reads one course: in case c7b0 of the experiment on #288 (comment) the 2019 course disappeared behind the 2026 one. When are two copies of one drug one medication?

What makes it hard: a pod has no server of record to say which copies are one course; sources date a medication differently (a start of use, a prescription's written date, a supply), or not at all; and monthly fills of one prescription leave gaps between them that a course does not have.

What others do

Read on 2026-10-31.

Sources: https://cql.hl7.org/09-b-cqlreference.html (Collapse, Meets); https://github.com/cqframework/ecqm-content-qicore-2024/blob/07c68e5e76d96d167627469f0face771c702023f/input/cql/CumulativeMedicationDuration.cql; https://hl7.org/fhir/R4/medicationrequest-definitions.html; https://hl7.org/fhir/R4/medicationdispense-definitions.html.

Choice

thing prior art what we do borrowed or diverged Rules that cite it
same course R8's period test (M14) two medication records join only when the drug test passes, unchanged (R5's NDC, R11's product code, R10's brand and generic), and their exposure periods share at least one day; rule M15 (queries/matcher/same-ndc-code-and-overlapping-exposure-periods.rq, same-product-code-in-current-tables-and-overlapping-exposure-periods.rq, same-brand-generic-and-overlapping-exposure-periods.rq) borrowed from M14; diverged from CQL's collapse, which merges periods that only meet
a period CumulativeMedicationDuration from cascade:medicationExposureStart to cascade:medicationExposureEnd, both days included; with no end, on with no end while cascade:medicationExposureOngoing holds, and the start day alone otherwise (medications.md, rows "exposure start" to "no period") borrowed; a course with no end that is not active is not known to have lasted past its start, as medications.md row "no end" reads it
an ongoing course M14's active condition runs on to today: compared as having no end, so it joins any later course of the drug borrowed; "today" is read as no end so the comparison reads no clock, and a run, a recheck and a fixture agree whatever day they run; the two differ only for a course whose start is after the day the matcher runs
no dates M14: no onset, no join a record with no exposure start joins no other medication record, and one whose end is before its start has no exposure and joins none borrowed
partial dates medications.md row "a partial date"; R8 and M23 read a partial date as every day it covers a start given to the year or month is read as its last day, an end as its first (cascade:medicationExposureStart, cascade:medicationExposureEnd), so two records join only when their overlap is certain, and a record "since 2019" does not join one that ended in June 2019 diverged from R8 and M23: the maintainer's ruling (2026-10-31: no dates, no join; any gap, no join) applied to an uncertain date, where a join that hides a course costs more than a pair a person confirms on "What needs review" No rule cites this row.
a gap CQL collapse merges intervals that meet any break, even one day, keeps two records apart; no tolerance in days diverged: a tolerance is a clinical judgment of how long a break still makes one course, which a pod should not make for the person
names R5, R10, R11 under version 17 the rules and their justifications keep their names (cascade:SameMedicationCodeRule, cascade:SameProductCodeRule, cascade:SameBrandGenericRule), each with a new query in version 18; each term's comment says what it means from version 18 ours No rule cites this row.
what is kept apart M16's "possibly the same medication" a pair of one product or one NDC that the period test keeps apart is listed on "What needs review" as possibly the same medication (queries/questions/entry/What needs review.rq, rule M16), so a person can join a course with a gap ours

Why

Two courses are two medications. A course is what a person took from a start to an end. Joining every copy of one drug made the entry show one course, the latest arrival's, and the earlier course was lost to every reader of the view (c7b0). Copies that describe one course overlap: a hospital's list and a clinic's prescription of the same metformin share the days it was taken. So "same drug and a shared day" keeps copies of one course together and courses apart. We treat this as the same course for now: a working rule, until a source that says which course a copy belongs to is held.

The period is the one already derived. Item 5 defined a medication's exposure as CumulativeMedicationDuration does, conservatively: a partial start is its last day, a partial end its first, and a supply counts from the earliest day it may. A second definition for matching would let a view and a rule disagree on when a drug was taken.

No tolerance. CQL merges intervals that meet, and a care measure may allow a gap of days before a course counts as stopped. Both are judgments about clinical meaning, and the pod holds facts (medical-knowledge-in-the-model.md). A break of one day is a break in what the sources said, and the pair goes to a person, not into one entry.

Same rule names. Version 18 tightens three rules; it does not add rules. M18 is built for this: the open that first runs version 18 asserts again every matcher judgment its rules still reach, and files one finding naming the matcher judgments it no longer reaches, the pairs now apart. New rule names would make that finding name every medication matcher judgment of version 17, including the pairs the new rules join again, and file each again under a new matcher judgment. Which query a matcher judgment was judged by is already on its assertion (rule list version and query hash, M4d), as it was when versions 7, 12 and 14 changed the queries of R5, R6 and R8.

Measured. Each comparison was timed in Oxigraph JS on a pod of 16,500 records with 3,000 medications, each with an exposure start (a third ended, a third ongoing, a third with a start alone), in clusters of up to 20 copies of a drug, three runs each. Bound to a step's records, the NDC rule took 2 to 5 ms (version 17: 2 to 3), the product code rule 72 to 265 ms (79 to 175), the brand generic rule 2 to 5 ms (4 to 8); run over the whole pod, 526 to 1,213 ms (382 to 787), 759 to 2,452 ms (570 to 1,207) and 195 to 583 ms (184 to 566).

Alternatives that lost

Still open

What would change our mind