Is it possible to use periodOffset expression in Aggregate indicators on Custom Forms to display previous Months' data?

Is it possible to use periodOffset expression in Aggregate indicators on Custom Forms to display previous Months’ data?

I have created a few Indicators using periodOffset expression but don’t display any values when added to a form.

What could be the issue?

1 Like

You are right, I tried to reproduce it on play and the indicators stopped working.

To reproduce on play, select a dataset which has working indicators. For example, EPI Stock dataset modify one of the indicators by adding .periodOffset(-1) to one of the data elements in the numerator. When you go to the dataset the indicators will stop working.

@dmaritim thanks!

Hi @dmaritim, Good question.

Unfortunately periodOffset, as well as many other special indicators functions, will not work within the data entry app.

Here’s why (if you’re interested)

When used in analytics, an indicator expression is evaluated in the backend Java code. However, when used in data entry, the indicator expression is passed to the user’s browser and executed there in JavaScript on the fly. Before passing the indicator expression to the frontend, the backend edits the indicator expression and substitutes the numeric values from the database of any constants C{constantUid}, and organisation unit group counts OUG{orgUnitGroupUid}. Then the JavaScript code in the data entry app then substitutes on the fly any data elements from the current data entry form into the indicator expression and uses JavaScript to evaluate the expression.

It used to be that indicator expressions just allowed simple arithmetic that both the backend Java code in analytics and the frontend JavaScript code in data entry would evaluate in the same way. But in order to make indicators more powerful for analytics in the backend, we’ve introduced a sophisticated, third-party Java expression parser (called ANTLR), and defined in the expression grammar additional expression functions like periodOffset. I imagine that when the frontend tries to evaluate the expression in JavaScript and it contains something like .periodOffset(-1), it has no way to evaluate the expression.

We need to explain in the documentation the limitations of indicators within data entry. We have neglected to do this.

Possible workaround

Here is something that might work for you, though I’m not sure if it would or not: you could run predictors in the backend to bring forward previous months’ data into the current period (into newly-defined data elements). Then you could display that data in your custom form. The part I’m not sure about is whether you can display read-only data element values in a custom form but I’m guessing you can? (Personally I’ve not used custom forms very much.)

If this works, note that it would only display data from previous periods after indicators are run on the backend. So in data entry you could not flip between months, enter a value in a previous month, and then flip ahead and expect to see it. But if all your data entry for the previous month is finished before data entry starts for the current month, then you could run predictors on the backend between these two points in time to bring the previous data forward into the current month.

1 Like