Metadata Title MAT Global Common Functions FHIR Library Version 5.0.000 Identifier MATGlobalCommonFunctions Identifier urn:oid:2.16.840.1.113883.4.642.40.61.28.25 Approval Date 2019-08-03 Last Review Date 2019-08-03 Experimental true Jurisdiction Global (Whole world) Steward (Publisher) HL7 International / Clinical Quality Information Description This library is used as an example in the FHIR Quality Measure Implementation Guide Type Logic Library Parameter Name: Measurement Period Use: In Min Cardinality: 0 Max Cardinality: 1 Type: Period Parameter Name: Patient Use: Out Min Cardinality: 0 Max Cardinality: 1 Type: Patient Parameter Name: Inpatient Encounter Use: Out Min Cardinality: 0 Max Cardinality: * Type: Encounter Data Requirement Type: Patient Profile(s): Patient Data Requirement Type: Encounter Profile(s): Encounter Must Support Elements: type, status, period, location, condition, condition.reference, rank Code Filter(s): Path: type ValueSet: Encounter Inpatient Data Requirement Type: Encounter Profile(s): Encounter Must Support Elements: type, period, location, condition, condition.reference, rank Code Filter(s): Path: type ValueSet: Observation Services Data Requirement Type: Encounter Profile(s): Encounter Must Support Elements: type, status, period, location, condition, condition.reference, rank Code Filter(s): Path: type ValueSet: Emergency Department Visit Data Requirement Type: Condition Profile(s): Condition Must Support Elements: id Data Requirement Type: Location Profile(s): Location Must Support Elements: id Data Requirement Type: Provenance Profile(s): Provenance Must Support Elements: target Code Filter(s): Path: target Data Requirement Type: Medication Profile(s): Medication Must Support Elements: id Library Content CQL Content /*#xD; This example is a work in progress and should not be considered a final specification#xD; or recommendation for guidance. This example will help guide and direct the process#xD; of finding conventions and usage patterns that meet the needs of the various stakeholders#xD; in the measure development community.#xD; */#xD; library MATGlobalCommonFunctions version '5.0.000'#xD; #xD; using FHIR version '4.0.1'#xD; #xD; include hl7.fhir.uv.cql.FHIRHelpers version '4.0.1' called FHIRHelpers#xD; #xD; codesystem "LOINC": 'http://loinc.org'#xD; codesystem "SNOMEDCT": 'http://snomed.info/sct'#xD; codesystem "RoleCode": 'http://terminology.hl7.org/CodeSystem/v3-RoleCode'#xD; codesystem "Diagnosis Role": 'http://terminology.hl7.org/CodeSystem/diagnosis-role'#xD; codesystem "RequestIntent": 'http://hl7.org/fhir/request-intent'#xD; codesystem "MedicationRequestCategory": 'http://terminology.hl7.org/CodeSystem/medicationrequest-category'#xD; codesystem "ConditionClinicalStatusCodes": 'http://terminology.hl7.org/CodeSystem/condition-clinical'#xD; codesystem "ConditionVerificationStatusCodes": 'http://terminology.hl7.org/CodeSystem/condition-ver-status'#xD; codesystem "AllergyIntoleranceClinicalStatusCodes": 'http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical'#xD; codesystem "AllergyIntoleranceVerificationStatusCodes": 'http://terminology.hl7.org/CodeSystem/allergyintolerance-verification'#xD; #xD; valueset "Encounter Inpatient": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.666.5.307'#xD; valueset "Emergency Department Visit": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.117.1.7.1.292'#xD; valueset "Observation Services": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1111.143'#xD; #xD; code "Birthdate": '21112-8' from "LOINC" display 'Birth date'#xD; code "Dead": '419099009' from "SNOMEDCT" display 'Dead'#xD; code "ER": 'ER' from "RoleCode" display 'Emergency room'#xD; code "ICU": 'ICU' from "RoleCode" display 'Intensive care unit'#xD; code "Billing": 'billing' from "Diagnosis Role" display 'Billing'#xD; #xD; // Condition Clinical Status Codes - Consider value sets for these#xD; code "active": 'active' from "ConditionClinicalStatusCodes"#xD; code "recurrence": 'recurrence' from "ConditionClinicalStatusCodes"#xD; code "relapse": 'relapse' from "ConditionClinicalStatusCodes"#xD; code "inactive": 'inactive' from "ConditionClinicalStatusCodes"#xD; code "remission": 'remission' from "ConditionClinicalStatusCodes"#xD; code "resolved": 'resolved' from "ConditionClinicalStatusCodes"#xD; #xD; // Condition Verification Status Codes - Consider value sets for these#xD; code "unconfirmed": 'unconfirmed' from ConditionVerificationStatusCodes#xD; code "provisional": 'provisional' from ConditionVerificationStatusCodes#xD; code "differential": 'differential' from ConditionVerificationStatusCodes#xD; code "confirmed": 'confirmed' from ConditionVerificationStatusCodes#xD; code "refuted": 'refuted' from ConditionVerificationStatusCodes#xD; code "entered-in-error": 'entered-in-error' from ConditionVerificationStatusCodes#xD; #xD; code "allergy-active": 'active' from "AllergyIntoleranceClinicalStatusCodes"#xD; code "allergy-inactive": 'inactive' from "AllergyIntoleranceClinicalStatusCodes"#xD; code "allergy-resolved": 'resolved' from "AllergyIntoleranceClinicalStatusCodes"#xD; #xD; // Allergy/Intolerance Verification Status Codes - Consider value sets for these#xD; code "allergy-unconfirmed": 'unconfirmed' from AllergyIntoleranceVerificationStatusCodes#xD; code "allergy-confirmed": 'confirmed' from AllergyIntoleranceVerificationStatusCodes#xD; code "allergy-refuted": 'refuted' from AllergyIntoleranceVerificationStatusCodes#xD; #xD; // MedicationRequest Category Codes#xD; code "Community": 'community' from "MedicationRequestCategory" display 'Community'#xD; code "Discharge": 'discharge' from "MedicationRequestCategory" display 'Discharge'#xD; #xD; parameter "Measurement Period" Interval<DateTime>#xD; default Interval[@2019-01-01T00:00:00.0, @2020-01-01T00:00:00.0)#xD; #xD; context Patient#xD; #xD; define "Inpatient Encounter":#xD; [Encounter: "Encounter Inpatient"] EncounterInpatient#xD; where EncounterInpatient.status = 'finished'#xD; and "LengthInDays"(EncounterInpatient.period) <= 120#xD; and EncounterInpatient.period ends during "Measurement Period"#xD; #xD; define function "ToDate"(Value DateTime):#xD; DateTime(year from Value, month from Value, day from Value, 0, 0, 0, 0, timezoneoffset from Value)#xD; #xD; define function "CalendarAgeInDaysAt"(BirthDateTime DateTime, AsOf DateTime):#xD; days between ToDate(BirthDateTime)and ToDate(AsOf)#xD; #xD; define function "CalendarAgeInDays"(BirthDateTime DateTime):#xD; CalendarAgeInDaysAt(BirthDateTime, Today())#xD; #xD; define function "CalendarAgeInMonthsAt"(BirthDateTime DateTime, AsOf DateTime):#xD; months between ToDate(BirthDateTime)and ToDate(AsOf)#xD; #xD; define function "CalendarAgeInMonths"(BirthDateTime DateTime):#xD; CalendarAgeInMonthsAt(BirthDateTime, Today())#xD; #xD; define function "CalendarAgeInYearsAt"(BirthDateTime DateTime, AsOf DateTime):#xD; years between ToDate(BirthDateTime)and ToDate(AsOf)#xD; #xD; define function "CalendarAgeInYears"(BirthDateTime DateTime):#xD; CalendarAgeInYearsAt(BirthDateTime, Today())#xD; #xD; define function "LengthInDays"(Value Interval<DateTime>):#xD; difference in days between start of Value and end of Value#xD; #xD; define function "ED Visit"(TheEncounter FHIR.Encounter):#xD; singleton from (#xD; [Encounter: "Emergency Department Visit"] EDVisit#xD; where EDVisit.status = 'finished'#xD; and EDVisit.period ends 1 hour or less on or before start of FHIRHelpers.ToInterval(TheEncounter.period)#xD; )#xD; #xD; define function "Hospitalization"(TheEncounter FHIR.Encounter):#xD; ( "ED Visit"(TheEncounter) ) X#xD; return#xD; if X is null then TheEncounter.period#xD; else Interval[start of FHIRHelpers.ToInterval(X.period), end of FHIRHelpers.ToInterval(TheEncounter.period)]#xD; #xD; define function "Hospitalization Locations"(TheEncounter FHIR.Encounter):#xD; ( "ED Visit"(TheEncounter) ) EDEncounter#xD; return#xD; if EDEncounter is null then TheEncounter.location#xD; else flatten { EDEncounter.location, TheEncounter.location }#xD; #xD; define function "Hospitalization Length of Stay"(TheEncounter FHIR.Encounter):#xD; LengthInDays("Hospitalization"(TheEncounter))#xD; #xD; define function "Hospital Admission Time"(TheEncounter FHIR.Encounter):#xD; start of "Hospitalization"(TheEncounter)#xD; #xD; define function "Hospital Discharge Time"(TheEncounter FHIR.Encounter):#xD; end of FHIRHelpers.ToInterval(TheEncounter.period)#xD; #xD; define function "Hospital Arrival Time"(TheEncounter FHIR.Encounter):#xD; start of FHIRHelpers.ToInterval(First(#xD; ( "Hospitalization Locations"(TheEncounter) ) HospitalLocation#xD; sort by start of FHIRHelpers.ToInterval(period)#xD; ).period)#xD; #xD; define function "HospitalizationWithObservation"(TheEncounter FHIR.Encounter):#xD; TheEncounter Visit#xD; let ObsVisit: Last([Encounter: "Observation Services"] LastObs#xD; where LastObs.period ends 1 hour or less on or before start of Visit.period#xD; sort by end of period#xD; ),#xD; VisitStart: Coalesce(start of ObsVisit.period, start of Visit.period),#xD; EDVisit: Last([Encounter: "Emergency Department Visit"] LastED#xD; where LastED.period ends 1 hour or less on or before VisitStart#xD; sort by end of period#xD; )#xD; return Interval[Coalesce(start of EDVisit.period, VisitStart), end of Visit.period]#xD; #xD; define function "HospitalizationWithObservationLengthofStay"(Encounter FHIR.Encounter):#xD; "LengthInDays"("HospitalizationWithObservation"(Encounter))#xD; #xD; /*#xD; *#xD; * CQFMeasures Common Logic#xD; *#xD; */#xD; #xD; define function "Normalize Interval"(choice Choice<FHIR.dateTime, FHIR.Period, FHIR.Timing, FHIR.instant, FHIR.string, FHIR.Age, FHIR.Range>):#xD; case#xD; when choice is FHIR.dateTime then#xD; Interval[FHIRHelpers.ToDateTime(choice as FHIR.dateTime), FHIRHelpers.ToDateTime(choice as FHIR.dateTime)]#xD; when choice is FHIR.Period then#xD; FHIRHelpers.ToInterval(choice as FHIR.Period)#xD; when choice is FHIR.instant then#xD; Interval[FHIRHelpers.ToDateTime(choice as FHIR.instant), FHIRHelpers.ToDateTime(choice as FHIR.instant)]#xD; when choice is FHIR.Age then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(choice as FHIR.Age),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(choice as FHIR.Age) + 1 year)#xD; when choice is FHIR.Range then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((choice as FHIR.Range).low),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((choice as FHIR.Range).high) + 1 year)#xD; when choice is FHIR.Timing then#xD; Message(null as Interval<DateTime>, true, '1', 'Error', 'Cannot compute a single interval from a Timing type')#xD; when choice is FHIR.string then#xD; Message(null as Interval<DateTime>, true, '1', 'Error', 'Cannot compute an interval from a String value')#xD; else#xD; null as Interval<DateTime>#xD; end#xD; #xD; define function "Normalize Abatement"(condition Condition):#xD; if condition.abatement is FHIR.dateTime then#xD; Interval[FHIRHelpers.ToDateTime(condition.abatement as FHIR.dateTime), FHIRHelpers.ToDateTime(condition.abatement as FHIR.dateTime)]#xD; else if condition.abatement is FHIR.Period then#xD; FHIRHelpers.ToInterval(condition.abatement as FHIR.Period)#xD; else if condition.abatement is FHIR.string then#xD; Message(null as Interval<DateTime>, true, '1', 'Error', 'Cannot compute an interval from a String value')#xD; else if condition.abatement is FHIR.Age then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(condition.abatement as FHIR.Age),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity(condition.abatement as FHIR.Age) + 1 year)#xD; else if condition.abatement is FHIR.Range then#xD; Interval[FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((condition.abatement as FHIR.Range).low),#xD; FHIRHelpers.ToDate(Patient.birthDate) + FHIRHelpers.ToQuantity((condition.abatement as FHIR.Range).high) + 1 year)#xD; else if condition.abatement is FHIR.boolean then#xD; Interval[end of "Normalize Interval"(condition.onset), condition.recordedDate)#xD; else null#xD; #xD; define function "Prevalence Period"(condition Condition):#xD; Interval[start of "Normalize Interval"(condition.onset), end of "Normalize Abatement"(condition))#xD; #xD; define function "GetId"(uri String):#xD; Last(Split(uri, '/'))#xD; #xD; #xD; define function "EncounterDiagnosis"(Encounter Encounter):#xD; Encounter.diagnosis D#xD; return singleton from ([Condition] C where C.id = "GetId"(D.condition.reference))#xD; #xD; // Returns the condition that is specified as the principal diagnosis for the encounter#xD; // TODO: BTR 2019-07-30: Shouldn't need the FHIRHelpers reference here, investigate#xD; define function "PrincipalDiagnosis"(Encounter Encounter):#xD; (singleton from (Encounter.diagnosis D where FHIRHelpers.ToInteger(D.rank) = 1)) PD#xD; return singleton from ([Condition] C where C.id = "GetId"(PD.condition.reference))#xD; #xD; // Returns the location for the given location reference#xD; define function GetLocation(reference Reference):#xD; singleton from (#xD; [Location] L where L.id = GetId(reference.reference)#xD; )#xD; #xD; /*#xD; NOTE: Extensions are not the preferred approach, but are used as a way to access#xD; content that is defined by extensions but not yet surfaced in the#xD; CQL model info.#xD; */#xD; define function "GetExtensions"(domainResource DomainResource, url String):#xD; domainResource.extension E#xD; where E.url = ('http://hl7.org/fhir/us/qicore/StructureDefinition/' + url)#xD; return E#xD; #xD; define function "GetExtension"(domainResource DomainResource, url String):#xD; singleton from "GetExtensions"(domainResource, url)#xD; #xD; /*#xD; NOTE: Extensions are not the preferred approach, but are used as a way to access#xD; content that is defined by extensions but not yet surfaced in the#xD; CQL model info.#xD; */#xD; define function "GetExtensions"(element Element, url String):#xD; element.extension E#xD; where E.url = (url)#xD; return E#xD; #xD; define function "GetExtension"(element Element, url String):#xD; singleton from "GetExtensions"(element, url)#xD; #xD; /*#xD; NOTE: Extensions are not the preferred approach, but are used as a way to access#xD; content that is defined by extensions but not yet surfaced in the#xD; CQL model info.#xD; */#xD; define function "GetBaseExtensions"(domainResource DomainResource, url String):#xD; domainResource.extension E#xD; where E.url = ('http://hl7.org/fhir/StructureDefinition/' + url)#xD; return E#xD; #xD; define function "GetBaseExtension"(domainResource DomainResource, url String):#xD; singleton from "GetBaseExtensions"(domainResource, url)#xD; #xD; /*#xD; NOTE: Provenance is not the preferred approach, this is provided only as an illustration#xD; for what using Provenance could look like, and is not a tested pattern#xD; */#xD; define function "GetProvenance"(resource Resource):#xD; singleton from ([Provenance: target in resource.id])#xD; #xD; define function "GetMedicationCode"(request MedicationRequest):#xD; if request.medication is CodeableConcept then#xD; request.medication as CodeableConcept#xD; else#xD; (singleton from ([Medication] M where M.id = GetId((request.medication as Reference).reference))).code#xD; ELM XML Content Encoded data (401484 characters) ELM JSON Content Encoded data (777360 characters) Generated using version 0.4.9 of the sample-content-ig Liquid templates
MAT Global Common Functions FHIR Library
HL7 International / Clinical Quality Information
This library is used as an example in the FHIR Quality Measure Implementation Guide
Code system ConditionClinicalStatusCodes
Code system ConditionVerificationStatusCodes
Code system AllergyIntoleranceClinicalStatusCodes
Code system AllergyIntoleranceVerificationStatusCodes