{
  "resourceType": "OperationDefinition",
  "id": "Observation-lastn",
  "text": {
    "status": "generated",
    "div": "\u003cdiv xmlns\u003d\"http://www.w3.org/1999/xhtml\"\u003e\u003cp class\u003d\"res-header-id\"\u003e\u003cb\u003eGenerated Narrative: OperationDefinition Observation-lastn\u003c/b\u003e\u003c/p\u003e\u003ca name\u003d\"Observation-lastn\"\u003e \u003c/a\u003e\u003ca name\u003d\"hcObservation-lastn\"\u003e \u003c/a\u003e\u003cp\u003eURL: [base]/Observation/$lastn\u003c/p\u003e\u003ch3\u003eParameters\u003c/h3\u003e\u003ctable class\u003d\"grid\"\u003e\u003ctr\u003e\u003ctd\u003e\u003cb\u003eUse\u003c/b\u003e\u003c/td\u003e\u003ctd\u003e\u003cb\u003eName\u003c/b\u003e\u003c/td\u003e\u003ctd\u003e\u003cb\u003eScope\u003c/b\u003e\u003c/td\u003e\u003ctd\u003e\u003cb\u003eCardinality\u003c/b\u003e\u003c/td\u003e\u003ctd\u003e\u003cb\u003eType\u003c/b\u003e\u003c/td\u003e\u003ctd\u003e\u003cb\u003eBinding\u003c/b\u003e\u003c/td\u003e\u003ctd\u003e\u003cb\u003eDocumentation\u003c/b\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003eIN\u003c/td\u003e\u003ctd\u003emax\u003c/td\u003e\u003ctd/\u003e\u003ctd\u003e0..1\u003c/td\u003e\u003ctd\u003e\u003ca href\u003d\"datatypes.html#positiveInt\"\u003epositiveInt\u003c/a\u003e\u003c/td\u003e\u003ctd/\u003e\u003ctd\u003e\u003cdiv\u003e\u003cp\u003e\u003ccode\u003emax\u003c/code\u003e is an optional input parameter to the \u003cem\u003elastn\u003c/em\u003e query operation. It is used to specify the maximum number of Observations to return from each group. For example for the query \u0026quot;Fetch the last 3 results for all vitals for a patient\u0026quot; \u003ccode\u003emax\u003c/code\u003e \u003d 3.\u003c/p\u003e\n\u003c/div\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003eOUT\u003c/td\u003e\u003ctd\u003ereturn\u003c/td\u003e\u003ctd/\u003e\u003ctd\u003e1..1\u003c/td\u003e\u003ctd\u003e\u003ca href\u003d\"bundle.html\"\u003eBundle\u003c/a\u003e\u003c/td\u003e\u003ctd/\u003e\u003ctd\u003e\u003cdiv\u003e\u003cp\u003eThe set of most recent N Observations that match the \u003cem\u003elastn\u003c/em\u003e query search criteria.\u003c/p\u003e\n\u003c/div\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\u003cdiv\u003e\u003cp\u003eThe key differences between this query operation and simply searching Observation using the combination of \u003ccode\u003e_count\u003c/code\u003e and \u003ccode\u003e_sort\u003c/code\u003e parameters are:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eThe \u003cem\u003elastn\u003c/em\u003e query returns \u003cstrong\u003eonly\u003c/strong\u003e the last N resource grouped by code. Using the _count query method doesn\u0027t restrict the total matches so you may need to page through several \u0026quot;A\u0026quot; Observations before getting to Observation \u0026quot;B\u0026quot;.\u003c/li\u003e\n\u003cli\u003eThe server is responsible for grouping the observations by codes. This frees the client from needing to determine which codes she should ask for.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eThis operation cannot be performed on observations that the user is not authorized to see. It is assumed that the server has identified and secured the context appropriately, and can either associate the authorization context with a single patient, or determine whether the context has the rights to the nominated patient, if there is one. If there is no nominated patient (e.g. the operation is invoked at the system level) and the context is not associated with a single patient record, then the server should return an error. Specifying the relationship between the context, a user and patient records is outside the scope of this specification.\u003c/p\u003e\n\u003c/div\u003e\u003c/div\u003e"
  },
  "extension": [
    {
      "url": "http://hl7.org/fhir/StructureDefinition/structuredefinition-fmm",
      "valueInteger": 3
    },
    {
      "url": "http://hl7.org/fhir/StructureDefinition/structuredefinition-standards-status",
      "valueCode": "normative"
    },
    {
      "url": "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg",
      "valueCode": "oo"
    }
  ],
  "url": "http://hl7.org/fhir/OperationDefinition/Observation-lastn",
  "version": "6.0.0-ballot4",
  "name": "Lastn",
  "title": "Last N Observations Query",
  "status": "active",
  "kind": "operation",
  "experimental": false,
  "date": "2026-06-14T21:56:23+00:00",
  "publisher": "HL7 International / Orders and Observations",
  "contact": [
    {
      "telecom": [
        {
          "system": "url",
          "value": "http://hl7.org/fhir"
        },
        {
          "system": "email",
          "value": "fhir@lists.hl7.org"
        }
      ]
    },
    {
      "telecom": [
        {
          "system": "url",
          "value": "http://www.hl7.org/Special/committees/orders"
        }
      ]
    }
  ],
  "description": "The *lastn query* meets the common need for searching for the most recent or last n\u003dnumber of observations for a subject. For example, retrieving the last 5 temperatures for a patient to view trends or fetching the most recent laboratory results or vitals signs. To ask a server to return the last n\u003dnumber of observations, the *lastn* query uses the [normal search parameters](observation.html#search) defined for the Observation resource. However, rather than their normal use, they are interpreted as inputs - i.e.. instead of requiring that the resources literally contain the search parameters, they are passed to a server algorithm of some kind that uses them to determine the most appropriate matches.\n\nThe request for a lastn query SHALL include:\n\n* A `$lastn` operation parameter\n* A subject using either the `patient` or `subject` search parameter\n* A `category` parameter and/or a search parameter that contains a code element in its FHIRpath expression. ( e.g., `code` or `code-value-concept`)\n\nThe request for a lastn query MAY include:\n\n* Other Observation search parameters and modifiers\n\nThe response from a lastn query is a set of observations:\n\n* Filtered by additional parameters\n   * If not explicitly filtered by status then will include statuses of \u0027entered-in-error\u0027\n* Ordered first by “equivalent” `Observation.code` value\n   * Codes SHALL be considered equivalent if the `coding.value` *and* `coding.system` are the same.\n   * Text only codes SHALL be treated and grouped based on the text.\n   * For codes with translations (multiple codings), the code translations are assumed to be equal and the grouping by code SHALL follow the transitive property of equality. For example:\n\n  |Observation.code for observation a|Observation.code for observation b|Observation.code for observation c|number of groups [codes/text in each group]|\n  |---|---|---|---|\n  |a|b|c | 3 [a],[b],[c]|\n  |a|b|a,c | 2 [a.c],[b]|\n  |a|b|a,b | 1 [a,b]|\n  |\u0027textM\u0027|\u0027Text\u0027|\u0027t e x t\u0027|3 [\u0027text\u0027],[\u0027Text\u0027],[\u0027t e x t\u0027]|\n\n* The ordering of the “equivalent” code groups is not specified.\n* Ordered secondly (within each code group) from most recent effective time to the oldest effective time. For further guidance, refer here on [sorting](https://www.hl7.org/fhir/search.html#_sort).\n* Limited to the number of requested responses per group specified by the optional *max* query parameter\n  * In case of a tie—when the effective times for more than one Observation in the same code group—both will be returned. Therefore, more Observations may be returned than is specified in *max*. For example, 4 Observations instead of 3 if the 3rd and 4th most recent observation had the same effective time.\n* If no maximum number is given then only the most recent Observation in each group is returned.\n\nNote that the individual Observation.code values used for grouping might not be explicit in the request, for example if a category of Observations is requested, or if a code is specified with the `:below` modifier.\n\nThe set of returned observations should represent distinct real-world observations and not the same observation with changes in status or versions. If there are no matches, the *lastn* query SHALL return an empty search set with no error, but may include an operation outcome with further advice.",
  "jurisdiction": [
    {
      "coding": [
        {
          "system": "http://unstats.un.org/unsd/methods/m49/m49.htm",
          "code": "001",
          "display": "World"
        }
      ]
    }
  ],
  "affectsState": false,
  "code": "lastn",
  "comment": "The key differences between this query operation and simply searching Observation using the combination of `_count` and `_sort` parameters are:\r\r* The *lastn* query returns **only** the last N resource grouped by code. Using the _count query method doesn\u0027t restrict the total matches so you may need to page through several \"A\" Observations before getting to Observation \"B\".\r* The server is responsible for grouping the observations by codes. This frees the client from needing to determine which codes she should ask for.\r\rThis operation cannot be performed on observations that the user is not authorized to see. It is assumed that the server has identified and secured the context appropriately, and can either associate the authorization context with a single patient, or determine whether the context has the rights to the nominated patient, if there is one. If there is no nominated patient (e.g. the operation is invoked at the system level) and the context is not associated with a single patient record, then the server should return an error. Specifying the relationship between the context, a user and patient records is outside the scope of this specification.",
  "resource": [
    "Observation"
  ],
  "system": false,
  "type": true,
  "instance": false,
  "parameter": [
    {
      "name": "max",
      "use": "in",
      "min": 0,
      "max": "1",
      "documentation": "`max` is an optional input parameter to the *lastn* query operation. It is used to specify the maximum number of Observations to return from each group. For example for the query \"Fetch the last 3 results for all vitals for a patient\" `max` \u003d 3.",
      "type": "positiveInt"
    },
    {
      "name": "return",
      "use": "out",
      "min": 1,
      "max": "1",
      "documentation": "The set of most recent N Observations that match the *lastn* query search criteria.",
      "type": "Bundle"
    }
  ]
}