FHIR Chat · Fetch latest n Observations and Loop through it #CQF-Ruler · cql

Stream: cql

Topic: Fetch latest n Observations and Loop through it #CQF-Ruler


view this post on Zulip Gautham Reddy Lingampally (Nov 01 2021 at 21:04):

Hi,

I am working a POC using cqf-ruler to see if our rule based clinical decisions can be centralized. As part of this POC, I have stumbled upon one of the use case where we need to check if one of the latest 2 PTH observation results are <= 400. I know we could use First() to get a latest observation. But here I need latest 2 observation. Can someone help me with this?

Something like this.....
define "latestPTHObservation"
[Observation: code = 'FR_PTH-I'] O
where O.effective < Now() - 90 days
sort by (effective as dateTime) desc
count 2

view this post on Zulip Corey Sanders (Nov 02 2021 at 14:24):

There are probably more elegant ways to do this, but I think something like the following will meet your need.

define MostRecentTwoObservations:
  ObservationsOrderedMostRecent[0]
  union
  ObservationsOrderedMostRecent[1]

define ObservationsOrderedMostRecent:
     [Observation:code = 'FR_PTH-I'] o where O.effective < Now() - 90 days sort by (effective as dateTime) desc

view this post on Zulip Michael Riley (Nov 03 2021 at 16:10):

I don't think there's a slicing-type operator so you're stuck doing a union-of-elements like this.

view this post on Zulip JP (Nov 03 2021 at 17:53):

You can build a slice function with Intervals:

define function Slice(list List<Any>, range Interval<Integer>) returns List<Any>:
  ((expand { range }) X return point from X) I
    return list[I]

define "List Items":
  { 'One', 'Two', 'Three', 'Four', 'Five'}

define "Sliced List":
  Slice("List Items", Interval[0, 1])

view this post on Zulip Michael Riley (Nov 03 2021 at 19:02):

What is the type of "I" at this point? The "expand" operator is only vaguely touched on in the author's guide, would be good to have more elaboration on them.

view this post on Zulip JP (Nov 03 2021 at 19:42):

"point from" returns a single point from the interval, and the interval is of type Integer, so I is an integer.

view this post on Zulip JP (Nov 03 2021 at 19:43):

A first-class Slice function would be cool though.

view this post on Zulip Paul Denning (Nov 04 2021 at 14:44):

The name "Slice" is easily confused with the FHIR concept of slicing http://hl7.org/fhir/profiling.html#slicing so I would avoid naming a CQL function "Slice" at least in the CQL spec.

view this post on Zulip Bryn Rhodes (Nov 04 2021 at 21:38):

There is a Take operator that will do exactly this, so it would be:
Take(ObservationsOrderedMostRecent, 2)

view this post on Zulip Gautham Reddy Lingampally (Nov 05 2021 at 17:13):

Thank you! Bryn. I also want to loop trough the list and check if the observation value is less than x. Is there a for loop kind of a thing in CQL?
Currently, I am doing ObservationsOrderedMostRecent[0].value.value <= x or ObservationsOrderedMostRecent[1].value.value <= x but want have them in a loop if possible.

view this post on Zulip Bryn Rhodes (Nov 06 2021 at 19:00):

That would be just a where clause:

ObservationsOrderedMostRecent O
  where O.value <= x

view this post on Zulip Gautham Reddy Lingampally (Nov 08 2021 at 13:40):

Thank you! Bryn.

view this post on Zulip Gautham Reddy Lingampally (Nov 10 2021 at 17:31):

Hi Bryn,

I have stumbled upon another scenario, to filter MedicationRequests based on the medication reference. I am trying to write a CQL which translates to below FHIR call.
MedicationRequest?category=community&status=active,completed&medication.code:text=Tums
I came up with below 2 CQLs based on the another thread "resolving reference", but it doesn't seem to work on CQE-ruler (I am using dstu3).

exists (
    [MedicationRequest: category = "HomeMeds"] MR
    with [MR.medication -> Medication] M
      such that M.code = "Tums"
        and (MR.status = 'active' or MR.status = 'completed')
        and M.ingredient[0].amount.numerator.value < 1890
        and MR.dosageInstruction[0].dose.value < 6)

and

exists (
     [MedicationRequest: category = "HomeMeds"] MR
     with [Medication: "Tums"] M such that M.id in Last(Split(MR.medication.reference, '/'))   )

Can you please help me with this?


Last updated: Apr 12 2022 at 19:14 UTC