Seeking Help with DHIS2 Program Rule for Medicine Adherence Calculation
I’m back again, not with a solution this time, but with a request for help and a possible feature suggestion for the DHIS2 program rule.
I am currently using the d2:daysBetween function, but we need it to include the end date (+1 day) in our calculations. This is critical for accurately determining medicine adherence rates for our patients.
Background
We collect medicine consumption data for 10 patients, recording the most recent and second most recent dates of medicine use. The d2:daysBetween function helps us calculate the interval between these dates, which is then used to compute adherence rates.
Since there is no built-in function to include the end date, I had to improvise using: [(d2:daysBetween( #{Date of second most recent}, #{Date of most recent})) + 1]
The Challenge
When calculating total adherence across patients, the system incorrectly adds +1 even when no data is available.
For example:
If only 5 patients have data and the total days between is 100, the system outputs 105 instead of 100—because it still sums up the extra +1 for non-existent data.
The mobile app doesn’t handle this properly when referring to assigned data elements.
Possible Solutions
A new function in DHIS2 for daysBetween + end date A function that does not add +1 when no data is present (though tricky to implement) The mobile app properly handling program rules with assigned calculated data elements
I’d love to hear from the DHIS2 community! Has anyone encountered this issue before? Any workarounds or thoughts on implementing a better solution?
Thank you for the beautifully written and organized detailed post.
I totally understand your point.
Maybe we could use a function that will check that there is data? What would be one data element in the form that confirms that the data is available? For example, d2:hasValue
thanks for all the details and for the feature report.
The suggestion from Gassim could make it work. Another option (not sure if it would work) could be using the function d2:addDays so the expression end up being (d2:daysBetween( #{Date of second most recent}, d2:addDays(#{Date of most recent}, 1)). I think it shouldn’t be evaluated if there is no data for those dataElements.
But I am most interested in the error you are facing in the mobile app. Could you give us more details about it?
Thank you for your responses. I will try out both suggestions and provide feedback on the results.
@vgarciabnz Regarding the mobile app, if I choose to calculate the adherence percentage based on a calculated data element (assigned value program rule action), it does not function properly within the mobile app. Therefore, I must create a program rule that includes the full calculation for each calculated data element field.
I tested the suggested solution and have some feedback. There’s both good news and bad news.
Good news:@vgarciabnz, your formula works! It successfully resolves the issue of adding (1) even when there is no data in the date cell. I have updated the formula From: ((d2:daysBetween( #{P1 Date of second most recent }, #{P1 Date of most recent} )) + 1) + ((d2:daysBetween( #{P2 Date of second most recent }, #{P2 Date of most recent} )) + 1) … up to P10
To: (((d2:daysBetween( #{P1 Date of second most recent }, d2:addDays( #{PW 1 Date of most recent},1 ))) + (d2:daysBetween( #{P2 Date of second most recent }, d2:addDays( #{PW 2 Date of most recent}, 1 ))) … up to P10)
Bad News:
While the current program rule formula functions well on the web app, it does not work on the mobile application.
I observed that some entries have responded to the update, and line listing visualizations reflect these changes for certain values. However, only about half of the data entries have updated correctly, while the other half still show incorrect calculations.
Additionally, I noticed unusual behavior in the capture app regarding previous entries, as shown in the images below. The first image displays the values when simply viewing the entries, while the second image shows the values changing to the correct ones when selecting edit event.
Image Note
Values in Green box are correct values based on updated program rule.
Values in Red box are incorrect values due to adding (+1) to fields with no date values
I will also like to bring this to your notice:
I also noticed an issue between the DHIS2 Mobile app and the Web Capture app. Entries made on the mobile app could not be edited on the web capture app—any changes made would appear to save but would not actually update. The only way to apply corrections was to edit them using the same DHIS2 mobile app that was originally used for data entry. I don’t know if this was one by design.
I encountered a similar issue before when I updated a program rule. Some previously entered data updated to the changes however for some entries we had to reopen the entries on the mobile app and sync for the changes to reflect on the web app and dashboard. I’m not sure if the same approach will work for this issue, but I will test it.
If you have a better solution, I’m open to suggestions.