Using CQL with FHIR
1.0.0 - STU1 International flag

This page is part of the Using CQL with FHIR Implementation Guide (v1.0.0: STU1) based on FHIR (HL7® FHIR® Standard) R4. This is the current published version. For a full list of available versions, see the Directory of published versions

Library: Example (Experimental)

Official URL: http://hl7.org/fhir/uv/cql/Library/Example Version: 1.0.0
Active as of 2024-05-31 Computable Name: Example
Other Identifiers: OID:2.16.840.1.113883.4.642.40.37.28.4

Example CQL Library used to demonstrate common patterns used in CQL with FHIR-based models

Related Artifacts

depends-onFHIR model informationhttp://fhir.org/guides/cqf/common/Library/FHIR-ModelInfo|4.0.1
depends-onLibrary FHIRHelpershttp://fhir.org/guides/cqf/common/Library/FHIRHelpers|4.0.1
depends-onCode system LOINCLogical Observation Identifiers, Names and Codes (LOINC)
depends-onCode system CDC Race and Ethnicity CodesCDC Race and Ethnicity
depends-onCode system SNOMED CTSNOMED CT (all versions)
depends-onCode system SNOMED CT:2017-09http://snomed.info/sct|http://snomed.info/sct/731000124108/version/201709
depends-onCode system Antenatal Care Conceptshttp://example.org/fhir/CodeSystem/anc-codes-example
depends-onCode system ICD-11International Classification of Diseases, 11th Revision Mortality and Morbidity Statistics (MMS)
depends-onValue set Antithrombotic Therapyhttp://example.org/fhir/ValueSet/antithrombotic-therapy-example
depends-onValue set Medical Reasonhttp://example.org/fhir/ValueSet/medical-reason-example
depends-onValue set Absent or Unknown Allergies - IPShttp://example.org/fhir/uv/ips/ValueSet/absent-or-unknown-allergies-uv-ips
depends-onValue set Encounter Inpatient SNOMEDCT Value Sethttp://example.org/fhir/ValueSet/encounter-inpatient|20160929
depends-onValue set Flexible Sigmoidoscopyhttp://example.org/fhir/ValueSet/flexible-sigmoidoscopy

Parameters

Measurement Periodin01Period
Patientout01Patient
Xout01boolean
Blood Pressure Observations Within 30 Daysout0*Observation
Blood Pressure Observations Within 30 Days (refined)out0*Observation
Blood Pressure With Slicesout0*Observation
Blood Pressure With Slices (refined)out0*Observation
Patient Birth Sex Is Maleout01Patient
Patient Birth Sex Is Male (refined)out01Patient
Patient With Race Categoryout01Patient
Patient With Race Category (refined)out01Patient
Antithrombotic Not Administeredout0*MedicationAdministration
Antithrombotic Class Not Administeredout0*MedicationAdministration
Antithrombotics Not Administeredout0*MedicationAdministration
Flexible Sigmoidoscopy Performedout0*Procedure

Data Requirements

Type: Patient (Patient)
Type: Observation (Observation)
FilterValue
codeOne of these codes: Logical Observation Identifiers, Names and Codes (LOINC) 85354-9: Blood pressure panel with all children optional
Type: MedicationAdministration (MedicationAdministration)
FilterValue
medicationIn ValueSet http://example.org/fhir/ValueSet/antithrombotic-therapy-example
Type: MedicationAdministration (MedicationAdministration)
Type: Procedure (Procedure)
FilterValue
codeIn ValueSet http://example.org/fhir/ValueSet/flexible-sigmoidoscopy

Contents

text/cql

library Example

using FHIR version '4.0.1'

include FHIRHelpers version '4.0.1'

codesystem LOINC: 'http://loinc.org'
codesystem "CDC Race and Ethnicity Codes": 'urn:oid:2.16.840.1.113883.6.238'
codesystem "SNOMED CT": 'http://snomed.info/sct'
codesystem "SNOMED CT:2017-09": 'http://snomed.info/sct' version 'http://snomed.info/sct/731000124108/version/201709'

valueset "Antithrombotic Therapy": 'http://example.org/fhir/ValueSet/antithrombotic-therapy-example'
valueset "Medical Reason": 'http://example.org/fhir/ValueSet/medical-reason-example'
valueset "Absent or Unknown Allergies - IPS": 'http://hl7.org/fhir/uv/ips/ValueSet/absent-or-unknown-allergies-uv-ips'
valueset "Encounter Inpatient SNOMEDCT Value Set": 'http://example.org/fhir/ValueSet/encounter-inpatient' version '20160929'
valueset "Flexible Sigmoidoscopy": 'http://example.org/fhir/ValueSet/flexible-sigmoidoscopy'

code "Blood Pressure": '85354-9' from LOINC
code "Diastolic blood pressure": '8462-4' from "LOINC" display 'Diastolic blood pressure'
code "Systolic blood pressure": '8480-6' from "LOINC" display 'Systolic blood pressure'
code "American Indian or Alaska Native": '1002-5' from "CDC Race and Ethnicity Codes"
code "Alaska Native": '1735-0' from "CDC Race and Ethnicity Codes"

codesystem "Antenatal Care Concepts": 'http://example.org/fhir/CodeSystem/anc-codes-example'
codesystem "ICD-11": 'http://id.who.int/icd/release/11/mms'

code "Tiredness Code": 'ANC.B5.DE40' from "Antenatal Care Concepts" display 'Tiredness'
code "MB22.7": 'MB22.7' from "ICD-11" display 'Tiredness'
code "Venous foot pump, device (physical object)": '442023007' from "SNOMED CT"

concept "Tiredness": { "Tiredness Code", "MB22.7" } display 'Tiredness'

parameter "Measurement Period" Interval<DateTime>

context Patient

define X:
  @2014-01-01 30 days or less before Today()

define "Blood Pressure Observations Within 30 Days":
  [Observation: "Blood Pressure"] O
    where O.status = 'final'
      and (
        (O.effective as dateTime).value 30 days or less before Today()
          or (O.effective as Period) starts 30 days or less before Today()
      )

define fluent function toInterval(choice Choice<FHIR.dateTime, FHIR.Period>):
  case
    when choice is FHIR.dateTime then
      Interval[FHIRHelpers.ToDateTime(choice as FHIR.dateTime), FHIRHelpers.ToDateTime(choice as FHIR.dateTime)]
    when choice is FHIR.Period then
      FHIRHelpers.ToInterval(choice as FHIR.Period)
    else null as Interval<DateTime>
  end

define "Blood Pressure Observations Within 30 Days (refined)":
  [Observation: "Blood Pressure"] O
    where O.status = 'final'
      and O.effective.toInterval() starts 30 days or less before Today()

define "Blood Pressure With Slices":
  [Observation: "Blood Pressure"] BP
    where (singleton from (BP.component C where C.code ~ "Systolic blood pressure")).value < 140 'mm[Hg]'
      and (singleton from (BP.component C where C.code ~ "Diastolic blood pressure")).value < 90 'mm[Hg]'

define fluent function systolic(observation Observation):
  singleton from (observation.component C where C.code ~ "Systolic blood pressure")

define fluent function diastolic(observation Observation):
  singleton from (observation.component C where C.code ~ "Diastolic blood pressure")

define "Blood Pressure With Slices (refined)":
  [Observation: "Blood Pressure"] BP
    where BP.systolic().value < 140 'mm[Hg]'
      and BP.diastolic().value < 90 'mm[Hg]'

define "Patient Birth Sex Is Male":
  Patient P
    let birthsex: singleton from (
        P.extension E where E.url.value = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex'
    ).value as FHIR.code
    where birthsex = 'M'

define fluent function birthsex(patient Patient):
  (singleton from (patient.extension E where E.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex')).value as FHIR.code

define "Patient Birth Sex Is Male (refined)":
  Patient P
    where P.birthsex() = 'M'

define "Patient With Race Category":
  Patient P
    let
      race: singleton from (
        P.extension E where E.url.value = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race'
      ),
      ombCategory: race.extension E where E.url.value = 'ombCategory',
      detailed: race.extension E where E.url.value = 'detailed'
    where (ombCategory O return O.value as FHIR.Coding) contains "American Indian or Alaska Native"
      and (detailed O return O.value as FHIR.Coding) contains "Alaska Native"
    
define fluent function race(patient Patient):
  (singleton from (patient.extension E where E.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race')) race
    let 
      ombCategory: race.extension E where E.url = 'ombCategory' return E.value as Coding,
      detailed: race.extension E where E.url = 'detailed' return E.value as Coding,
      text: singleton from (race.extension E where E.url = 'text' return E.value as string)
    return { ombCategory: ombCategory, detailed: detailed, text: text }

define "Patient With Race Category (refined)":
  Patient P
    where P.race().ombCategory contains "American Indian or Alaska Native"
      and P.race().detailed contains "Alaska Native"

define "Antithrombotic Not Administered":
  [MedicationAdministration: "Antithrombotic Therapy"] NotAdministered
    where NotAdministered.status = 'not-done'
      and NotAdministered.statusReason in "Medical Reason"

define "Antithrombotic Class Not Administered":
  [MedicationAdministration] NotAdministered
    where NotAdministered.medication.notDoneValueSet() = "Antithrombotic Therapy".id
      and NotAdministered.status = 'not-done'
      and NotAdministered.statusReason in "Medical Reason"

define fluent function notDoneValueSet(element Element):
  (singleton from (element.extension E where E.url = 'http://hl7.org/fhir/StructureDefinition/cqf-notDoneValueSet')).value as uri

define fluent function notDoneValueSet(element Choice<CodeableConcept, Reference>):
  case
    when element is CodeableConcept then (element as CodeableConcept).notDoneValueSet()
    when element is Reference then (element as Reference).notDoneValueSet()
    else null
  end

define "Antithrombotics Not Administered":
  "Antithrombotic Not Administered"
    union "Antithrombotic Class Not Administered"

define function "Includes Or Starts During"(Condition Condition, Encounter Encounter):
      Interval[Condition.onset, Condition.abatement] includes Encounter.period
         or Condition.onset during Encounter.period

define "Flexible Sigmoidoscopy Performed":
  [Procedure: "Flexible Sigmoidoscopy"] FlexibleSigmoidoscopy
    where FlexibleSigmoidoscopy.status = 'completed'
      and FlexibleSigmoidoscopy.performed.toInterval() ends 5 years or less on or before end of "Measurement Period"

Content not shown - (application/elm+xml, size = 1Mb )

Content not shown - (application/elm+json, size = 934Kb )