Introduction to CQL on FHIR
Clinical Quality Language (CQL) is a high-level, domain-specific language focused on clinical quality and is designed to be both human-readable and machine-processable. When combined with FHIR, CQL enables the expression of clinical knowledge in a way that can be executed against FHIR data sources.
Why CQL + FHIR?
- Standardization: CQL provides a standard way to express clinical logic
- Interoperability: Works seamlessly with FHIR data models
- Quality Measures: Essential for eCQMs (electronic Clinical Quality Measures)
- Clinical Decision Support: Enables sophisticated CDS rules
Key Components
CQL Libraries
Reusable collections of CQL expressions and functions
FHIR Resources
Patient data represented as FHIR resources (Patient, Observation, etc.)
Measure Resources
FHIR Measure resources that define quality measures
Example: Breast Cancer Screening Quality Measure
A worked example from CMS125 Breast Cancer Screening, using the 2026 FHIR version of the measure (QI-Core 6.0.0). The excerpts below are taken from the published CQL, which Open Quality carries in full. Checked October 2026.
1. Measure Definition
Clinical Criteria:
- Initial population: female patients aged 42 to 74 at the end of the measurement period, with a qualifying outpatient encounter in the period
- Denominator: the initial population
- Numerator: a mammogram ending between October 1 two years before the measurement period and the end of the period (27 months for a calendar-year period)
- Exclusions: hospice, palliative care, age 66+ with advanced illness and frailty or living long-term in a nursing home, bilateral mastectomy, or left and right unilateral mastectomies
2. CQL Library Structure
library CMS125FHIRBreastCancerScreening version '0.4.000'
using QICore version '6.0.0'
include FHIRHelpers version '4.4.000' called FHIRHelpers
include QICoreCommon version '4.0.000' called QICoreCommon
include AdultOutpatientEncounters version '4.19.000' called AdultOutpatientEncounters
include Hospice version '6.18.000' called Hospice
include Status version '1.15.000' called Status
include PalliativeCare version '1.18.000' called PalliativeCare
include AdvancedIllnessandFrailty version '1.27.000' called AIFrailLTCF
valueset "Mammography": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.108.12.1018'
valueset "Bilateral Mastectomy": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1005'
// ...six more mastectomy value sets in the full library
parameter "Measurement Period" Interval<DateTime>
context Patient
define "Initial Population":
AgeInYearsAt(date from end of "Measurement Period") in Interval[42, 74]
and Patient.sex = '248152002'
and exists AdultOutpatientEncounters."Qualifying Encounters"
define "Denominator":
"Initial Population"
define "Denominator Exclusions":
Hospice."Has Hospice Services"
or exists "Bilateral Mastectomy Procedure"
// ...or bilateral mastectomy diagnosis, or right AND left unilateral
// mastectomy, or the frailty and palliative care exclusions
or AIFrailLTCF."Is Age 66 or Older with Advanced Illness and Frailty"
or AIFrailLTCF."Is Age 66 or Older Living Long Term in a Nursing Home"
or PalliativeCare."Has Palliative Care in the Measurement Period"
define "Numerator":
exists ( ( ( [ObservationClinicalResult: "Mammography"] ).isDiagnosticStudyPerformed ( ) ) Mammogram
where Mammogram.effective.toInterval ( ) ends during day of
Interval["October 1 Two Years Prior to the Measurement Period", end of "Measurement Period"]
)
define "October 1 Two Years Prior to the Measurement Period":
DateTime((year from start of "Measurement Period" - 2), 10, 1, 0, 0, 0, 0, 0)3. FHIR Measure Resource
{
"resourceType": "Measure",
"id": "breast-cancer-screening",
"url": "http://example.org/Measure/breast-cancer-screening",
"version": "1.0.0",
"name": "BreastCancerScreening",
"title": "Breast Cancer Screening",
"status": "active",
"experimental": false,
"date": "2025-01-01",
"description": "Percentage of women 42-74 years of age who had a mammogram to screen for breast cancer",
"purpose": "To measure the rate of breast cancer screening in eligible women",
"scoring": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-scoring",
"code": "proportion",
"display": "Proportion"
}]
},
"type": [{
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-type",
"code": "process",
"display": "Process"
}]
}],
"improvementNotation": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-improvement-notation",
"code": "increase",
"display": "Increased score indicates improvement"
}]
},
"library": [
"http://example.org/Library/BreastCancerScreening"
],
"group": [{
"population": [{
"code": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-population",
"code": "initial-population"
}]
},
"criteria": {
"language": "text/cql-identifier",
"expression": "Initial Population"
}
}, {
"code": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-population",
"code": "denominator"
}]
},
"criteria": {
"language": "text/cql-identifier",
"expression": "Denominator"
}
}, {
"code": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-population",
"code": "numerator"
}]
},
"criteria": {
"language": "text/cql-identifier",
"expression": "Numerator"
}
}, {
"code": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/measure-population",
"code": "denominator-exclusion"
}]
},
"criteria": {
"language": "text/cql-identifier",
"expression": "Denominator Exclusions"
}
}]
}]
}4. Sample FHIR Data
Example patient data that would be evaluated by this measure:
// Patient Resource. The 2026 measure reads Patient.sex (SNOMED 248152002),
// not Patient.gender; see the QI-Core 6 Patient profile for how it is carried.
{
"resourceType": "Patient",
"id": "patient-example",
"gender": "female",
"birthDate": "1968-03-15"
}
// Observation Resource (Mammogram)
// 24606-6 is in the Mammography value set (checked against tx.fhir.org)
{
"resourceType": "Observation",
"id": "mammogram-example",
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "24606-6",
"display": "MG Breast Screening"
}]
},
"subject": {
"reference": "Patient/patient-example"
},
"effectiveDateTime": "2024-06-15",
"valueString": "Screening mammogram completed - no abnormalities detected"
}Implementation Steps
Step 1: Set Up CQL Execution Engine
Choose and configure a CQL execution engine. Popular options include:
- cql-execution: JavaScript-based CQL engine by MITRE
- CQL Evaluator: Java-based engine integrated with HAPI FHIR
- Clinical Quality Framework (CQF): Reference implementation
npm install cql-execution cql-exec-fhirStep 2: Translate CQL to ELM
CQL must be translated to Expression Logical Model (ELM) JSON for execution:
# The reference translator is the Java CQL-to-ELM translator in
# github.com/cqframework/clinical_quality_language. MADiE and most
# measure engines run it for you; run it yourself for local testing.Step 3: Execute Measure Evaluation
Use the $evaluate-measure operation on a FHIR server:
POST [base]/Measure/breast-cancer-screening/$evaluate-measure
Content-Type: application/fhir+json
{
"resourceType": "Parameters",
"parameter": [{
"name": "periodStart",
"valueDate": "2024-01-01"
}, {
"name": "periodEnd",
"valueDate": "2024-12-31"
}, {
"name": "subject",
"valueString": "Patient/patient-example"
}, {
"name": "reportType",
"valueCode": "subject"
}]
}Step 4: Process MeasureReport
The server returns a MeasureReport resource with evaluation results:
{
"resourceType": "MeasureReport",
"status": "complete",
"type": "individual",
"measure": "http://example.org/Measure/breast-cancer-screening",
"subject": {
"reference": "Patient/patient-example"
},
"period": {
"start": "2024-01-01",
"end": "2024-12-31"
},
"group": [{
"population": [{
"code": {
"coding": [{
"code": "initial-population"
}]
},
"count": 1
}, {
"code": {
"coding": [{
"code": "denominator"
}]
},
"count": 1
}, {
"code": {
"coding": [{
"code": "numerator"
}]
},
"count": 1
}]
}],
"evaluatedResource": [{
"reference": "Patient/patient-example"
}, {
"reference": "Observation/mammogram-example"
}]
}Best Practices
✓ Do's
- ✅ Use standard value sets from VSAC (Value Set Authority Center)
- ✅ Include version numbers in library declarations
- ✅ Write comprehensive unit tests for CQL logic
- ✅ Document clinical rationale for each criterion
- ✅ Use FHIRPath expressions when possible
- ✅ Reuse the shared CMS libraries (QICoreCommon, Status, Hospice, PalliativeCare, AdvancedIllnessandFrailty)
✗ Don'ts
- ❌ Hardcode codes instead of using value sets
- ❌ Ignore data quality and completeness issues
- ❌ Create overly complex nested logic
- ❌ Skip validation against test patient data
- ❌ Forget to handle missing or null data
- ❌ Neglect performance optimization for large datasets
Testing & Validation
CQL Test Framework
The CQL Framework provides comprehensive testing capabilities:
Latest Testing Resources:
- CQL Tests Results Repository →
Comprehensive test suite with expected results for CQL implementations
- CQ Framework GitHub Organization →
Official CQL specifications, tools, and reference implementations
Example Test Case
{
"name": "Patient Meets Numerator - Recent Mammogram",
"description": "55-year-old female with mammogram 6 months ago should be in numerator",
"patientData": {
"resourceType": "Patient",
"id": "test-patient-1",
"gender": "female",
"birthDate": "1969-01-01"
},
"observations": [{
"resourceType": "Observation",
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "24604-1"
}]
},
"effectiveDateTime": "2024-06-01"
}],
"measurementPeriod": {
"start": "2024-01-01",
"end": "2024-12-31"
},
"expectedResults": {
"initialPopulation": true,
"denominator": true,
"numerator": true,
"denominatorExclusion": false
}
}Common CQL Patterns
Date Range Queries
define "Observations in Measurement Period":
[Observation] O
where O.effective during "Measurement Period"
and O.status in { 'final', 'amended', 'corrected' }Value Set Membership
define "Diabetes Diagnoses":
[Condition: "Diabetes"] DiabetesCondition
where DiabetesCondition.clinicalStatus ~ "active"
and DiabetesCondition.verificationStatus ~ "confirmed"Age Calculations
define "Patient Age at Start":
AgeInYearsAt(start of "Measurement Period")
define "Eligible Age Range":
"Patient Age at Start" >= 50 and "Patient Age at Start" < 75Exists vs Count
// Use exists for boolean checks (more efficient)
define "Has Recent Lab":
exists ([Observation: "Lab Test"] O where O.effective during "Recent Period")
// Use count when you need the actual number
define "Number of Encounters":
Count([Encounter] E where E.period during "Measurement Period")Additional Resources
Need Help Implementing CQL Quality Measures?
I can help you design, implement, and validate CQL-based quality measures for your organization.