---
description: Compute a MeasureReport from a Measure whose populations are boolean columns of a SQLQuery Library.
---

> For the complete documentation index, see [llms.txt](https://www.health-samurai.io/docs/aidbox/llms.txt).
> Use it to discover all available pages before guessing URLs.

---
# $evaluate-measure operation

{% hint style="warning" %}
Requires **fhir-schema mode**.
{% endhint %}

{% hint style="info" %}
This functionality is available in Aidbox versions 2611 and later.
{% endhint %}

The `$evaluate-measure` operation computes a MeasureReport for a [Measure](https://hl7.org/fhir/R4/measure.html) over a period. Aidbox evaluates the Measure with SQL: each population points at a boolean column of a [SQLQuery Library](operation-sqlquery-run.md#sqlquery-library), and Aidbox counts the rows where the column is `true`. CQL expressions are not evaluated.

FHIR defines the operation in [`Measure/$evaluate-measure`](https://hl7.org/fhir/R4/measure-operation-evaluate-measure.html).

## Endpoints

| Level    | Endpoint                                                 | Measure source                              |
|----------|----------------------------------------------------------|---------------------------------------------|
| Instance | `GET` or `POST [base]/fhir/Measure/[id]/$evaluate-measure` | The stored Measure with this id             |
| Type     | `GET` or `POST [base]/fhir/Measure/$evaluate-measure`      | The Measure with the canonical in `measure` |

At the type level, Aidbox looks for a stored Measure with the canonical first, then for a Measure from a FHIR package in the [Artifact Registry](../../artifact-registry/artifact-registry-overview.md). A canonical without `|version` resolves to the latest version.

`POST` takes a `Parameters` resource in the body. `GET` takes the same parameters in the query string.

## Parameters

| Parameter     | Required             | Description |
|---------------|----------------------|-------------|
| `periodStart` | yes                  | Start of the measurement period: `YYYY`, `YYYY-MM`, or `YYYY-MM-DD`. The period starts on the first day of it. |
| `periodEnd`   | yes                  | End of the measurement period, in the same formats. The period includes the whole last day, month, or year. |
| `measure`     | at the type level    | Canonical of the Measure: `url` or `url\|version`. |
| `subject`     | no                   | `Patient/<id>`. Repeat it to count several patients. Aidbox keeps only the Library rows whose `patient_id` matches. |
| `reportType`  | no                   | `subject` (or `individual`) returns an individual report for one subject. `population` (or `summary`) returns a summary report. Defaults to `subject` when the request has a subject and to `population` otherwise. |
| `parameters`  | no                   | A `Parameters` resource with values for the other parameters of the Library. |

Aidbox answers `400` for `reportType=subject-list` and for the `practitioner`, `provider`, `location`, and `lastReceivedOn` parameters, which it doesn't support.

## How Aidbox evaluates a Measure

The Library returns one row per patient: a `patient_id` column and a boolean column per population. Aidbox passes the period to the Library as two `dateTime` parameters, `measurement_period_start` and `measurement_period_end`, in UTC with the end excluded: `periodStart=2026&periodEnd=2026` binds `2026-01-01T00:00:00Z` and `2027-01-01T00:00:00Z`. Declare them in `Library.parameter` to use the period in SQL.

Each population of the Measure names its column in an `application/sql` expression:

- `criteria.language` is `application/sql`, or `criteria` carries a [`cqf-alternativeExpression`](https://hl7.org/fhir/R4/extension-cqf-alternativeexpression.html) extension with an `application/sql` expression;
- `expression` is the column name;
- `reference` is the canonical of the SQLQuery Library. All populations use one Library. Without `reference`, Aidbox uses the SQLQuery Library listed in `Measure.library`.

Aidbox reads the scoring from the `cqfm-scoring` extension of a group, then from `group.scoring`, then from `Measure.scoring`. It supports two scorings:

- `cohort`: each population counts the rows where its column is `true`;
- `proportion`: a population counts a row only if the row also belongs to the populations it depends on, as in CQF Measures. For example, `numerator` counts rows that are in `initial-population` and `denominator`, aren't in `denominator-exclusion`, and have `numerator` set to `true`. The report carries `measureScore`: `(numerator - numerator-exclusion) / (denominator - denominator-exclusion - denominator-exception)`. Aidbox omits `measureScore` when the divisor is `0`.

## Example

The Library reads a [ViewDefinition](defining-flat-views-with-view-definitions.md) with the `id`, `gender`, and `birth_date` columns of Patient. A patient is in the initial population when born before the period ends, in the denominator when born before it starts, and in the numerator when also female:

```sql
SELECT id AS patient_id,
       birth_date::date < ((:measurement_period_end)::timestamptz AT TIME ZONE 'UTC')::date AS initial_population,
       birth_date::date < ((:measurement_period_start)::timestamptz AT TIME ZONE 'UTC')::date AS denominator,
       birth_date::date < ((:measurement_period_start)::timestamptz AT TIME ZONE 'UTC')::date
         AND gender = 'female' AS numerator
FROM patient
```

{% stepper %}
{% step %}
Create the SQLQuery Library with the SQL above in `content.data`:

```http
POST /fhir/Library
Content-Type: application/json

{
  "resourceType": "Library",
  "id": "female-patients-lib",
  "url": "https://example.org/Library/female-patients-lib",
  "version": "1.0.0",
  "status": "active",
  "meta": {
    "profile": ["https://sql-on-fhir.org/ig/StructureDefinition/SQLQuery"]
  },
  "type": {
    "coding": [{
      "system": "https://sql-on-fhir.org/ig/CodeSystem/LibraryTypesCodes",
      "code": "sql-query"
    }]
  },
  "parameter": [
    {"name": "measurement_period_start", "use": "in", "type": "dateTime", "min": 1, "max": "1"},
    {"name": "measurement_period_end", "use": "in", "type": "dateTime", "min": 1, "max": "1"}
  ],
  "relatedArtifact": [{
    "type": "depends-on",
    "resource": "https://example.org/ViewDefinition/patient_view",
    "label": "patient"
  }],
  "content": [{
    "contentType": "application/sql",
    "data": "U0VMRUNUIGlkIEFTIHBhdGllbnRfaWQsCiAgICAgICBiaXJ0aF9kYXRlOjpkYXRlIDwgKCg6bWVhc3VyZW1lbnRfcGVyaW9kX2VuZCk6OnRpbWVzdGFtcHR6IEFUIFRJTUUgWk9ORSAnVVRDJyk6OmRhdGUgQVMgaW5pdGlhbF9wb3B1bGF0aW9uLAogICAgICAgYmlydGhfZGF0ZTo6ZGF0ZSA8ICgoOm1lYXN1cmVtZW50X3BlcmlvZF9zdGFydCk6OnRpbWVzdGFtcHR6IEFUIFRJTUUgWk9ORSAnVVRDJyk6OmRhdGUgQVMgZGVub21pbmF0b3IsCiAgICAgICBiaXJ0aF9kYXRlOjpkYXRlIDwgKCg6bWVhc3VyZW1lbnRfcGVyaW9kX3N0YXJ0KTo6dGltZXN0YW1wdHogQVQgVElNRSBaT05FICdVVEMnKTo6ZGF0ZQogICAgICAgICBBTkQgZ2VuZGVyID0gJ2ZlbWFsZScgQVMgbnVtZXJhdG9yCkZST00gcGF0aWVudA=="
  }]
}
```
{% endstep %}

{% step %}
Create a proportion Measure whose populations name the Library columns:

```http
PUT /fhir/Measure/female-patients
Content-Type: application/json

{
  "resourceType": "Measure",
  "id": "female-patients",
  "url": "https://example.org/Measure/female-patients",
  "status": "active",
  "scoring": {
    "coding": [{
      "system": "http://terminology.hl7.org/CodeSystem/measure-scoring",
      "code": "proportion"
    }]
  },
  "group": [{
    "id": "g-1",
    "population": [
      {
        "code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/measure-population", "code": "initial-population"}]},
        "criteria": {
          "language": "application/sql",
          "expression": "initial_population",
          "reference": "https://example.org/Library/female-patients-lib|1.0.0"
        }
      },
      {
        "code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/measure-population", "code": "denominator"}]},
        "criteria": {"language": "application/sql", "expression": "denominator"}
      },
      {
        "code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/measure-population", "code": "numerator"}]},
        "criteria": {"language": "application/sql", "expression": "numerator"}
      }
    ]
  }]
}
```

The `denominator` and `numerator` populations have no `reference`, so they use the Library of `initial-population`.
{% endstep %}

{% step %}
Evaluate the Measure for 2026:

```http
GET /fhir/Measure/female-patients/$evaluate-measure?periodStart=2026&periodEnd=2026
```

With three patients born before 2027, two of them before 2026, and one of those two female, Aidbox returns:

```json
{
  "resourceType": "MeasureReport",
  "status": "complete",
  "type": "summary",
  "measure": "https://example.org/Measure/female-patients",
  "date": "2026-12-03T10:00:00Z",
  "period": {"start": "2026", "end": "2026"},
  "group": [{
    "id": "g-1",
    "population": [
      {"code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/measure-population", "code": "initial-population"}]}, "count": 3},
      {"code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/measure-population", "code": "denominator"}]}, "count": 2},
      {"code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/measure-population", "code": "numerator"}]}, "count": 1}
    ],
    "measureScore": {"value": 0.5}
  }]
}
```

Add `subject=Patient/<id>` to get an individual report for one patient: each count is `1` or `0`, and the report carries `subject`.
{% endstep %}
{% endstepper %}

To run the evaluation on a [read-only replica](../../database/overview.md#postgresql-with-read-only-replica), send the request with the `x-use-ro-replica` header. In Aidbox UI notebooks, a `measure` cell runs this operation and renders the report.

## Errors

| Status | When |
|--------|------|
| `400`  | `periodStart` or `periodEnd` is missing or has another format, `periodEnd` is before `periodStart`, a single-value parameter repeats, `subject` isn't `Patient/<id>`, `reportType=subject` comes with no subject or several subjects, `parameters` sets `measurement_period_start` or `measurement_period_end`, `measure` is missing at the type level, or a Library parameter is missing or has the wrong type. |
| `404`  | The Measure doesn't exist, or a Library dependency doesn't resolve. |
| `422`  | The Measure has no group, a group has no scoring or a scoring other than `proportion` and `cohort`, a population has no code or no single `application/sql` expression, the populations reference different Libraries, the Library is missing or isn't a SQLQuery Library, the Library result has no column for a population or the column isn't boolean, or the Library SQL fails. |

The body of an error response is an OperationOutcome with the reason in `diagnostics`.
