To create an expression specifically requesting information that a MedicationDispense intentionally did not occur for a medical, patient or system reason, use the profile QICore-medicationnotdispensed.
// connectathon/fhir4/cql/AdvancedIllnessandFrailtyExclusion_FHIR4-4.0.000.cql
define "Dementia Medications Dispensed":
["MedicationDispense": "Dementia Medications"] MedicationDispense
where MedicationDispense.status in { 'active', 'completed', 'on-hold' }
See the patterns page for implementation and usage patterns.