CMS FHIR Prototype Measure Calculation Tool IG
0.1.0 - CI Build

CMS FHIR Prototype Measure Calculation Tool IG, published by HL7 International - [Some] Work Group. This guide is not an authorized publication; it is the continuous build for version 0.1.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/cqframework/mct-ig/ and changes regularly. See the Directory of published versions

## : MAT Global Common Functions QICore4 - XML Representation

Draft as of 2026-02-27

Raw xml | Download

    <Library xmlns="http://hl7.org/fhir">
      <id value="MATGlobalCommonFunctionsQICore4"/>
      <text>
        <status value="extensions"/>
        <div xmlns="http://www.w3.org/1999/xhtml">
        <table class="grid dict">

            <tr>
                <th scope="row"><b>Id: </b></th>
                <td style="padding-left: 4px;">MATGlobalCommonFunctionsQICore4</td>
            </tr>

            <tr>
                <th scope="row"><b>Url: </b></th>
                <td style="padding-left: 4px;"><a href="Library-MATGlobalCommonFunctionsQICore4.html">MAT Global Common Functions QICore4</a></td>
            </tr>

            <tr>
                <th scope="row"><b>Version: </b></th>
                <td style="padding-left: 4px;">0.1.0</td>
            </tr>

            <tr>
                <th scope="row"><b>Name: </b></th>
                <td style="padding-left: 4px;">MATGlobalCommonFunctionsQICore4</td>
            </tr>

            <tr>
                <th scope="row"><b>Title: </b></th>
                <td style="padding-left: 4px;">MAT Global Common Functions QICore4</td>
            </tr>

            <tr>
                <th scope="row"><b>Status: </b></th>
                <td style="padding-left: 4px;">draft</td>
            </tr>

            <tr>
                <th scope="row"><b>Type: </b></th>
                <td style="padding-left: 4px;">

                            <p style="margin-bottom: 5px;">
                                <b>system: </b> <span><a href="http://terminology.hl7.org/7.0.1/CodeSystem-library-type.html">http://terminology.hl7.org/CodeSystem/library-type</a></span>
                            </p>

                            <p style="margin-bottom: 5px;">
                                <b>code: </b> <span>logic-library</span>
                            </p>

                </td>
            </tr>

            <tr>
                <th scope="row"><b>Date: </b></th>
                <td style="padding-left: 4px;">2026-02-27 18:11:00+0000</td>
            </tr>

            <tr>
                <th scope="row"><b>Publisher: </b></th>
                <td style="padding-left: 4px;">HL7 International - [Some] Work Group</td>
            </tr>

            <tr>
                <th scope="row"><b>Jurisdiction: </b></th>
                <td style="padding-left: 4px;">US</td>
            </tr>

            <tr>
              <td colspan="2">
                <table>
                  <tr><th><a id="cql-content"><b>Content: </b></a> text/cql</th></tr>
                  <tr><td><pre><code class="language-cql">/*
    @update: BTR 2020-03-31 -&amp;gt;
    Incremented version to 5.0.000
    Updated FHIR version to 4.0.1
    Changed timezone keyword to timezoneoffset for use with CQL 1.4
    Removed Normalize Onset in favor of more general Normalize Interval
    Updated CodeSystems for ConditionVerificationStatusCodes and RoleCodes

    @update: BTR 2021-05-13 -&amp;gt;
    Added ActiveCondition Codes and Inactive Condition Codes value sets
    Added function documentation throughout
    Fixed EDVisit not using Last
    Updated prevalence period to use an inclusive boundary if the condition is active
    Added HasStart, HasEnd, Earliest, and Latest functions
    Removed ToDate and Age calculation functions

    @update: BTR 2021-06-25 -&amp;gt;
    Added GetBaseExtension overloads for Element

    @update: BTR 2022-05-24 -&amp;gt;
    Update for 2022 AU content
    Refactor to use CQF.FHIRCommon

    @update: BTR 2022-06-14 -&amp;gt;
    Update to use QICore
    Rename from MATGlobalCommonFunctionsFHIR4 to MATGlobalCommonFunctionsQICore4
    Added fluent functions
    Removed Normalize Interval
    */
    library MATGlobalCommonFunctionsQICore4 version '7.0.000'

    using QICore version '4.1.1'

    include FHIRHelpers version '4.0.013' called FHIRHelpers
    include QICoreCommon version '1.0.000' called QICoreCommon

    valueset &amp;quot;Emergency Department Visit&amp;quot;: 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.117.1.7.1.292'
    valueset &amp;quot;Encounter Inpatient&amp;quot;: 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.666.5.307'
    valueset &amp;quot;Intensive Care Unit&amp;quot;: 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1029.206'
    valueset &amp;quot;Observation Services&amp;quot;: 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1111.143'
    valueset &amp;quot;Outpatient Surgery Service&amp;quot;: 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1110.38'
    valueset &amp;quot;Present on Admission or Clinically Undetermined&amp;quot;: 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1147.197'

    parameter &amp;quot;Measurement Period&amp;quot; Interval&amp;lt;DateTime&amp;gt;
      default Interval[@2022-01-01T00:00:00.0, @2023-01-01T00:00:00.0)

    context Patient

    define &amp;quot;Inpatient Encounter&amp;quot;:
      [Encounter: &amp;quot;Encounter Inpatient&amp;quot;] EncounterInpatient
    		where EncounterInpatient.status = 'finished'
    	    and &amp;quot;LengthInDays&amp;quot;(EncounterInpatient.period) &amp;lt;= 120
    			and EncounterInpatient.period ends during day of &amp;quot;Measurement Period&amp;quot;

    /*
    @description: Returns an interval of date values extracted from the input interval of date-time values
    @comment: This function returns an interval constructed using the `date from` extractor on the start
    and end values of the input date-time interval. Note that using a precision specifier such as `day of`
    as part of a timing phrase is preferred to communicate intent to perform day-level comparison, as well
    as for general readability.
    */
    define function &amp;quot;ToDateInterval&amp;quot;(period Interval&amp;lt;DateTime&amp;gt;):
      Interval[date from start of period, date from end of period]

    /*
    Calculates the difference in calendar days between the start and end of the given interval.
    */
    define function &amp;quot;LengthInDays&amp;quot;(Value Interval&amp;lt;DateTime&amp;gt; ):
      difference in days between start of Value and end of Value

    /*
    Calculates the difference in calendar days between the start and end of the given interval.
    */
    define fluent function lengthInDays(Value Interval&amp;lt;DateTime&amp;gt; ):
      difference in days between start of Value and end of Value

    /*
    Returns the most recent emergency department visit, if any, that occurs 1 hour or less prior to the given encounter.
    */
    define function &amp;quot;ED Visit&amp;quot;(TheEncounter Encounter ):
      Last(
        [Encounter: &amp;quot;Emergency Department Visit&amp;quot;] EDVisit
          where EDVisit.status = 'finished'
            and EDVisit.period ends 1 hour or less on or before start of TheEncounter.period
          sort by end of period
        )

    /*
    Returns the most recent emergency department visit, if any, that occurs 1 hour or less prior to the given encounter.
    */
    define fluent function edVisit(TheEncounter Encounter ):
      Last(
        [Encounter: &amp;quot;Emergency Department Visit&amp;quot;] EDVisit
          where EDVisit.status = 'finished'
            and EDVisit.period ends 1 hour or less on or before start of TheEncounter.period
          sort by end of period
        )

    /*
    Hospitalization returns the total interval for admission to discharge for the given encounter, or for the admission of any immediately prior emergency department visit to the discharge of the given encounter.
    */
    define function &amp;quot;Hospitalization&amp;quot;(TheEncounter Encounter ):
      ( &amp;quot;ED Visit&amp;quot;(TheEncounter) ) X
        return
            if X is null then TheEncounter.period
            else Interval[start of X.period, end of TheEncounter.period)

    /*
    Hospitalization returns the total interval for admission to discharge for the given encounter, or for the admission of any immediately prior emergency department visit to the discharge of the given encounter.
    */
    define fluent function hospitalization(TheEncounter Encounter ):
      ( &amp;quot;ED Visit&amp;quot;(TheEncounter) ) X
        return
            if X is null then TheEncounter.period
            else Interval[start of X.period, end of TheEncounter.period]

    /*
    Returns list of all locations within an encounter, including locations for immediately prior ED visit.
    */
    define function &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter Encounter ):
      ( &amp;quot;ED Visit&amp;quot;(TheEncounter) ) EDEncounter
        return
            if EDEncounter is null then TheEncounter.location
            else flatten { EDEncounter.location, TheEncounter.location }

    /*
    Returns list of all locations within an encounter, including locations for immediately prior ED visit.
    */
    define fluent function hospitalizationLocations(TheEncounter Encounter ):
      ( &amp;quot;ED Visit&amp;quot;(TheEncounter) ) EDEncounter
        return
            if EDEncounter is null then TheEncounter.location
            else flatten { EDEncounter.location, TheEncounter.location }

    /*
    Returns the length of stay in days (i.e. the number of days between admission and discharge) for the given encounter, or from the admission of any immediately prior emergency department visit to the discharge of the encounter
    */
    define function &amp;quot;Hospitalization Length of Stay&amp;quot;(TheEncounter Encounter ):
      LengthInDays(&amp;quot;Hospitalization&amp;quot;(TheEncounter))

    /*
    Returns the length of stay in days (i.e. the number of days between admission and discharge) for the given encounter, or from the admission of any immediately prior emergency department visit to the discharge of the encounter
    */
    define fluent function hospitalizationLengthOfStay(TheEncounter Encounter ):
      LengthInDays(&amp;quot;Hospitalization&amp;quot;(TheEncounter))

    /*
    Returns admission time for an encounter or for immediately prior emergency department visit.
    */
    define function &amp;quot;Hospital Admission Time&amp;quot;(TheEncounter Encounter ):
      start of &amp;quot;Hospitalization&amp;quot;(TheEncounter)

    /*
    Returns admission time for an encounter or for immediately prior emergency department visit.
    */
    define fluent function hospitalAdmissionTime(TheEncounter Encounter ):
      start of &amp;quot;Hospitalization&amp;quot;(TheEncounter)

    /*
    Hospital Discharge Time returns the discharge time for an encounter
    */
    define function &amp;quot;Hospital Discharge Time&amp;quot;(TheEncounter Encounter ):
      end of TheEncounter.period

    /*
    Hospital Discharge Time returns the discharge time for an encounter
    */
    define fluent function hospitalDischargeTime(TheEncounter Encounter ):
      end of TheEncounter.period

    /*
    Returns earliest arrival time for an encounter including any prior ED visit.
    */
    define function &amp;quot;Hospital Arrival Time&amp;quot;(TheEncounter Encounter ):
      start of First(
      	    ( &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter) ) HospitalLocation
      			sort by start of period
      	).period

    /*
    Returns earliest arrival time for an encounter including any prior ED visit.
    */
    define fluent function hospitalArrivalTime(TheEncounter Encounter ):
      start of First(
      	    ( &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter) ) HospitalLocation
      			sort by start of period
      	).period

    /*
    Returns the latest departure time for encounter including any prior ED visit.
    */
    define function &amp;quot;Hospital Departure Time&amp;quot;(TheEncounter Encounter):
    	end of Last(
    	    ( &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter) ) HospitalLocation
    			sort by start of period
    	).period

    /*
    Returns the latest departure time for encounter including any prior ED visit.
    */
    define fluent function hospitalDepartureTime(TheEncounter Encounter):
    	end of Last(
    	    ( &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter) ) HospitalLocation
    			sort by start of period
    	).period

    define function &amp;quot;Emergency Department Arrival Time&amp;quot;(TheEncounter Encounter):
    	start of (
    	    singleton from (
    	        ( &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter) ) HospitalLocation
    				where GetLocation(HospitalLocation.location).type in &amp;quot;Emergency Department Visit&amp;quot;
    		)
    	).period

    define fluent function emergencyDepartmentArrivalTime(TheEncounter Encounter):
    	start of (
    	    singleton from (
    	        ( &amp;quot;Hospitalization Locations&amp;quot;(TheEncounter) ) HospitalLocation
    				where GetLocation(HospitalLocation.location).type in &amp;quot;Emergency Department Visit&amp;quot;
    		)
    	).period

    /*
    Hospitalization with Observation and Outpatient Surgery Service returns the total interval from the start of any immediately prior emergency department visit, outpatient surgery visit or observation visit to the discharge of the given encounter.
    */
    define function &amp;quot;HospitalizationWithObservationAndOutpatientSurgeryService&amp;quot;(TheEncounter &amp;quot;Encounter&amp;quot; ):
      TheEncounter Visit
    	  let ObsVisit: Last([Encounter: &amp;quot;Observation Services&amp;quot;] LastObs
    		  	where LastObs.status = 'finished'
              and LastObs.period ends 1 hour or less on or before start of Visit.period
    			  sort by	end of period
        	),
        	VisitStart: Coalesce(start of ObsVisit.period, start of Visit.period),
        	EDVisit: Last([Encounter: &amp;quot;Emergency Department Visit&amp;quot;] LastED
    			  where LastED.status = 'finished'
              and LastED.period ends 1 hour or less on or before VisitStart
    			  sort by	end of period
        	),
        	VisitStartWithED: Coalesce(start of EDVisit.period, VisitStart),
        	OutpatientSurgeryVisit: Last([Encounter: &amp;quot;Outpatient Surgery Service&amp;quot;] LastSurgeryOP
    			  where LastSurgeryOP.period ends 1 hour or less on or before VisitStartWithED
    			  sort by	end of period
        	)
      	return Interval[Coalesce(start of OutpatientSurgeryVisit.period, VisitStartWithED), end of Visit.period]

    /*
    Hospitalization with Observation and Outpatient Surgery Service returns the total interval from the start of any immediately prior emergency department visit, outpatient surgery visit or observation visit to the discharge of the given encounter.
    */
    define fluent function hospitalizationWithObservationAndOutpatientSurgeryService(TheEncounter &amp;quot;Encounter&amp;quot; ):
      TheEncounter Visit
    	  let ObsVisit: Last([Encounter: &amp;quot;Observation Services&amp;quot;] LastObs
    		  	where LastObs.status = 'finished'
              and LastObs.period ends 1 hour or less on or before start of Visit.period
    			  sort by	end of period
        	),
        	VisitStart: Coalesce(start of ObsVisit.period, start of Visit.period),
        	EDVisit: Last([Encounter: &amp;quot;Emergency Department Visit&amp;quot;] LastED
    			  where LastED.status = 'finished'
              and LastED.period ends 1 hour or less on or before VisitStart
    			  sort by	end of period
        	),
        	VisitStartWithED: Coalesce(start of EDVisit.period, VisitStart),
        	OutpatientSurgeryVisit: Last([Encounter: &amp;quot;Outpatient Surgery Service&amp;quot;] LastSurgeryOP
    			  where LastSurgeryOP.period ends 1 hour or less on or before VisitStartWithED
    			  sort by	end of period
        	)
      	return Interval[Coalesce(start of OutpatientSurgeryVisit.period, VisitStartWithED), end of Visit.period]

    /*
    Hospitalization with Observation returns the total interval from the start of any immediately prior emergency department visit through the observation visit to the discharge of the given encounter
    */
    define function &amp;quot;HospitalizationWithObservation&amp;quot;(TheEncounter Encounter ):
      TheEncounter Visit
      		let ObsVisit: Last([Encounter: &amp;quot;Observation Services&amp;quot;] LastObs
      				where LastObs.status = 'finished'
                and LastObs.period ends 1 hour or less on or before start of Visit.period
      				sort by end of period
      			),
      			VisitStart: Coalesce(start of ObsVisit.period, start of Visit.period),
      			EDVisit: Last([Encounter: &amp;quot;Emergency Department Visit&amp;quot;] LastED
      				where LastED.status = 'finished'
                and LastED.period ends 1 hour or less on or before VisitStart
      				sort by end of period
      			)
      		return Interval[Coalesce(start of EDVisit.period, VisitStart), end of Visit.period]

    /*
    Hospitalization with Observation returns the total interval from the start of any immediately prior emergency department visit through the observation visit to the discharge of the given encounter
    */
    define fluent function hospitalizationWithObservation(TheEncounter Encounter ):
      TheEncounter Visit
      		let ObsVisit: Last([Encounter: &amp;quot;Observation Services&amp;quot;] LastObs
      				where LastObs.status = 'finished'
                and LastObs.period ends 1 hour or less on or before start of Visit.period
      				sort by end of period
      			),
      			VisitStart: Coalesce(start of ObsVisit.period, start of Visit.period),
      			EDVisit: Last([Encounter: &amp;quot;Emergency Department Visit&amp;quot;] LastED
      				where LastED.status = 'finished'
                and LastED.period ends 1 hour or less on or before VisitStart
      				sort by end of period
      			)
      		return Interval[Coalesce(start of EDVisit.period, VisitStart), end of Visit.period]

    /*
    Hospitalization with Observation Length of Stay returns the length in days from the start of any immediately prior emergency department visit through the observation visit to the discharge of the given encounter
    */
    define function &amp;quot;HospitalizationWithObservationLengthofStay&amp;quot;(TheEncounter &amp;quot;Encounter&amp;quot; ):
      &amp;quot;LengthInDays&amp;quot;(&amp;quot;HospitalizationWithObservation&amp;quot;(TheEncounter))

    /*
    Hospitalization with Observation Length of Stay returns the length in days from the start of any immediately prior emergency department visit through the observation visit to the discharge of the given encounter
    */
    define fluent function hospitalizationWithObservationLengthofStay(TheEncounter &amp;quot;Encounter&amp;quot; ):
      &amp;quot;LengthInDays&amp;quot;(&amp;quot;HospitalizationWithObservation&amp;quot;(TheEncounter))

    /*
    First Inpatient Intensive Care Unit returns the first intensive care unit for the given encounter, without considering any immediately prior emergency department visit.
    */
    define function &amp;quot;FirstInpatientIntensiveCareUnit&amp;quot;(Encounter Encounter ):
      First((Encounter.location)HospitalLocation
      			where GetLocation(HospitalLocation.location).type in &amp;quot;Intensive Care Unit&amp;quot;
      				and HospitalLocation.period during Encounter.period
      			sort by start of period
      	)

    /*
    First Inpatient Intensive Care Unit returns the first intensive care unit for the given encounter, without considering any immediately prior emergency department visit.
    */
    define fluent function firstInpatientIntensiveCareUnit(Encounter Encounter ):
      First((Encounter.location)HospitalLocation
      			where GetLocation(HospitalLocation.location).type in &amp;quot;Intensive Care Unit&amp;quot;
      				and HospitalLocation.period during Encounter.period
      			sort by start of period
      	)

    /*
    Returns the Condition resources referenced by the diagnosis element of the Encounter
    */
    define function &amp;quot;EncounterDiagnosis&amp;quot;(Encounter Encounter ):
      Encounter.diagnosis D
    	return singleton from ([Condition] C where C.id = D.condition.reference.getId())

    /*
    Returns the Condition resources referenced by the diagnosis element of the Encounter
    */
    define fluent function encounterDiagnosis(Encounter Encounter ):
      Encounter.diagnosis D
        return singleton from ([Condition] C where C.id = D.condition.reference.getId())

    /*
    Returns the Condition resource for the given reference
    @deprecated
    */
    define function &amp;quot;GetCondition&amp;quot;(reference Reference):
      singleton from ([Condition] C where C.id = reference.reference.getId())

    /*
    Returns the Condition resource for the given reference
    */
    define fluent function getCondition(reference Reference):
      singleton from ([Condition] C where C.id = reference.reference.getId())

    /*
    Returns the condition that is specified as the principal diagnosis for the encounter
    */
    define function &amp;quot;PrincipalDiagnosis&amp;quot;(Encounter Encounter ):
    	singleton from ((Encounter.diagnosis D where D.rank = 1) PD
          return singleton from ([Condition] C where C.id = PD.condition.reference.getId())
    	)

    /*
    Returns the condition that is specified as the principal diagnosis for the encounter
    */
    define fluent function principalDiagnosis(Encounter Encounter ):
    	singleton from ((Encounter.diagnosis D where D.rank = 1) PD
          return singleton from ([Condition] C where C.id = PD.condition.reference.getId())
    	)

    /*
    Returns the Location resource specified by the given reference
    */
    define function &amp;quot;GetLocation&amp;quot;(reference Reference ):
      singleton from (
        [Location] L where L.id = reference.reference.getId()
      )

    /*
    Returns the Location resource specified by the given reference
    */
    define fluent function getLocation(reference Reference ):
      singleton from (
        [Location] L where L.id = reference.reference.getId()
      )

    /*
    Returns the medication code for the given MedicationRequest
    */
    define function &amp;quot;GetMedicationCode&amp;quot;(request MedicationRequest ):
      if request.medication is Concept then
      	  request.medication as Concept
      	else
      	  (singleton from ([Medication] M where M.id = (request.medication as Reference).reference.getId())).code

    /*
    Returns the medication code for the given MedicationRequest
    */
    define fluent function getMedicationCode(request MedicationRequest ):
      if request.medication is Concept then
      	  request.medication as Concept
      	else
      	  (singleton from ([Medication] M where M.id = (request.medication as Reference).reference.getId())).code

    </code></pre></td></tr>
                </table>
              </td>
            </tr>

        </table>
    </div>
      </text>
      <extension
                 url="http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-softwaresystem">
        <valueReference>🔗
          <reference value="Device/cqf-tooling"/>
        </valueReference>
      </extension>
      <url
           value="http://cms.gov/fhir/mct/Library/MATGlobalCommonFunctionsQICore4"/>
      <version value="0.1.0"/>
      <name value="MATGlobalCommonFunctionsQICore4"/>
      <title value="MAT Global Common Functions QICore4"/>
      <status value="draft"/>
      <type>
        <coding>
          <system value="http://terminology.hl7.org/CodeSystem/library-type"/>
          <code value="logic-library"/>
        </coding>
      </type>
      <date value="2026-02-27T18:11:00+00:00"/>
      <publisher value="HL7 International - [Some] Work Group"/>
      <contact>
        <telecom>
          <system value="url"/>
          <value value="http://hl7.org/Special/committees/[something]"/>
        </telecom>
      </contact>
      <jurisdiction>
        <coding>
          <system value="urn:iso:std:iso:3166"/>
          <code value="US"/>
        </coding>
      </jurisdiction>
      <content>
        <contentType value="text/cql"/>
        <data
              value=" "/>
      </content>
    </Library>