FHIR Chat · If not empty, do something · fhirpath

Stream: fhirpath

Topic: If not empty, do something


view this post on Zulip Grey Faulkenberry (Feb 06 2022 at 02:36):

I'm trying to decide how best to structure a complex path expression to do a calculation. Specifically, I'm using it in a Measure to create a MeasureReport. The input resource is a Bundle with multiple QuestionnaireResponses as entries, and some of my calculations require me to pull specific questions from each of the questionnaires. My issue is that sometimes the questions may be answered and other times they won't be. If they're not, then I need to not count them in my calculation. As an example, assume I'm working with 4 questions. They each can have values from 1-5, or you can choose not to answer (in this case, it is not associated with a numerical value). If either of the first two questions are 2-5, you get 3 points, if either of the 2nd questions are 3-5, you get 2 points. So my expression looks something like

(entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question1')).answer.valueCoding.extension.valueDecimal.select(iif($this > 1, 1, 0)) + entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question2')).answer.valueCoding.extension.valueDecimal.select(iif($this > 1, 1, 0))).select(iif($this > 0, 3, 0)) + (entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question3')).answer.valueCoding.extension.valueDecimal.select(iif($this > 2, 1, 0)) + entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question4')).answer.valueCoding.extension.valueDecimal.select(iif($this > 2, 1, 0))).select(iif($this > 0, 2, 0))

It's verbose, but it does the job. Except when something is missing. If there is no answer for question 2 (or the answer is, "I select not to answer", which shows up as a valueString), then the value for the second item is [], which causes the entire expression to return []. I need to somehow check if it's an empty set, and if so, turn it into 0. Is there a suggested method for doing this? I thought about something like:

entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question2')).answer.valueCoding.extension.valueDecimal.select(iif($this > 1, 1, 0))).select(iif($this > 0, 3, 0)).select(iif($this.exists(), $this, 0))

But it didn't look like this was allowed in the FHIRPath spec. Does anyone have a suggestion on how this should be done?

view this post on Zulip Brian Postlethwaite (Feb 06 2022 at 22:47):

Are all those questions from the same questionnaire?

view this post on Zulip Brian Postlethwaite (Feb 06 2022 at 22:58):

For how you've described the clause, I'd be using count() or exists() along with a where clause instead of the iif right at the end.
Something more like this
iif(entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question3')).answer.valueCoding.extension.where(valueDecimal > 2).exists(), 1, 0)

view this post on Zulip Grey Faulkenberry (Feb 07 2022 at 01:23):

@Brian Postlethwaite , no, they're not from the same Questionnaire. So if I'm trying to get the value from the question, I would be using something like

iif(entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question3')).answer.valueCoding.extension.where(valueDecimal > 2).exists(), entry.resource.ofType(QuestionnaireResponse).item.where(linkId.contains('/question3')).answer.valueCoding.extension.valueDecimal, 0)

view this post on Zulip Brian Postlethwaite (Feb 07 2022 at 01:27):

That should work.

view this post on Zulip Grey Faulkenberry (Feb 07 2022 at 01:34):

Thanks!


Last updated: Apr 12 2022 at 19:14 UTC