Reporting Module - Build cohorts with relative dates

3 views
Skip to first unread message

Victor Garcia

unread,
Feb 18, 2015, 7:35:38 AM2/18/15
to implem...@openmrs.org
Hi all,

I am trying to build cohorts with relative dates, but I am not success. The purpose of this is to be able to create a cohort that, for example, retrieves all encounters within the last month.

I know that this can be done by manually inserting start and end date at running time, but I am looking for something like a keyword ({today}, {now}, ...) that can be defined as a parameter. Is it possible?

Thanks in advance,

Víctor

Michael Seaton

unread,
Feb 18, 2015, 12:16:17 PM2/18/15
to implem...@openmrs.org
Hi Victor,

This is possible, but requires a bit of a convoluted solution and is not (easily) accessible via the UI directly, though could be accomplished fairly easily if you set up your cohorts in code.

The following contextual parameters are always available for use when evaluating any definitions in the reporting framework:

${now}
${start_of_today}
${end_of_today}
${start_of_last_month}
${end_of_last_month}
${generatedBy}
${generationDate}

These were added to the framework after the general paradigm was established that definitions can map parameters between each other, or they can be entered at runtime, so support was never initially built in for referring to these "context" parameters easily within definitions.

The way you would need to accomplish this is via a wrapper definition.  For example, say you wanted to find all patients who had any encounters in the past week.  Here's a general approach that could work:
  • Create a new Encounter Cohort Definition, with parameters for "On Or After" and "On Or Before", as you would normally do (and any other constraints set as needed).
  • Wrap this in a new Composition Cohort Definition by:
    • Create a Composition Cohort Definition
    • Add in Encounter Cohort Definition you created in step 1 as the only search, with the following mappings (adjust the specifics as appropriate, these are examples):
      • On Or After:  start_of_today-6d
      • On Or Before: end_of_today
    • Set the composition string to simply that single search

If you are doing this in code, it is straightforward and will work in a normal way.  When you set up your parameter mappings, simply set them to:
"onOrAfter=${start_of_today-6d},onOrBefore=${end_of_today}"

However, if you are trying to do this via the UI, there is currently a "feature" (it is by design) that only allows for setting up expression-based parameter mappings if the enclosing definition has parameters itself.  "Luckily" there is no validation around this when you save the definition or when you add and remove parameters, so you can get around this a bit clumsily when you set up your composition cohort definition by:

1. Add in some dummy date parameter (eg. use the quick link for "date")
2. Add your Encounter Cohort search, and set up your parameter mappings as above (don't refer to the date parameter you added here, but use the built-in parameters)
3. Remove the dummy parameter you added in step 1.

This will (should) work.  You will note that you will not be able to see things correctly in edit mode unless you add back in that dummy date parameter.  So to make edits, you will need to add that dummy parameter, make your change, and then remove it again.

Hope this helps.  Improvements to make this work better are very welcome :)

Regards,
Mike
--
OpenMRS Implementers: http://om.rs/implist
Post: implem...@openmrs.org | Unsubscribe: implementers...@openmrs.org
Manage your OpenMRS subscriptions: http://om.rs/id
 
Visit OpenMRS Talk at http://om.rs/talk for chat and discussions!
To unsubscribe from this group and stop receiving emails from it, send an email to implementers...@openmrs.org.

Victor Garcia

unread,
Feb 19, 2015, 11:46:27 AM2/19/15
to implem...@openmrs.org
Hi Michael,

and first of all, thanks for such a detailed explanation, it was really helpful.

I tried the tricky way via the UI and it worked as expected for cohort
queries. But when trying with encounter queries, it seems that
composition encounter queries don't behave in the same way that
composition cohort definitions do.

So here, what would it be your recommendation? I don't know if it is
better to change the behaviour of composition encounter queries or
directly go to code.

Michael Seaton

unread,
Feb 19, 2015, 12:16:20 PM2/19/15
to implem...@openmrs.org
Hi Victor,

Yeah, unfortunately no appropriate editor page was ever created for the
Encounter Composition Query.

I think new code would need to be written for:

1. Modifying the mapped parameters widget to enable use of expressions,
referring to the context parameters below, even if no compatible
parameters are in the enclosing definition

2. Adapt the composition cohort definition editor to work for other
composition queries or create new pages to enable creating / editing
these pages.

I'm not personally spending much time writing new features in the 1.x
line of the reporting module UI, but I am happy to discuss, review and
merge in these enhancements as implementations need them.

Thanks,
Mike
Reply all
Reply to author
Forward
0 new messages