Urgent Help Needed: Using d2:condition in a program rule to calculate a set of values

Hi all.

I have a scenario in which I need to calculate the number of YES and display the result. So, I thought of using d2:condition as below:

d2:condition(’(d2:hasValue(‘ovc_ra_hiv_active_sex’) && (#{ovc_ra_hiv_active_sex}==‘Yes’))’,1,0) + d2:condition(’(d2:hasValue(‘ovc_ra_hiv_risk_of_hiv’) && (#{ovc_ra_hiv_risk_of_hiv}==‘Yes’))’,1,0)

But after defining the expression as above, I have a doubt whether this will work due to the single quotes I have used in it. I can clearly see that the parser will confuse with the beginning and closing quotes. Can someone assist me how I can achieve what I want.

Your help is very much appreciated. God Bless!

1 Like


Have you checked out on the documentation and the examples on Play Server?

You can test the examples on play server to see if the output is as desired.


Thanks @jomutsani for the details.

FYI, I have already gone through the documentation and it does not have any examples that matches my scenario.

And, I checked the Play Server now and unfortunately it too does not have any examples that matches my scenario.


Hi there @MSP,
The single quotes might be what causes you the problem, or the parenthesis. We have a unresolved bug in jira that might be relevant: https://jira.dhis2.org/browse/DHIS2-5593

I am sure however that we should be able to help you achieve what you want without using d2:condition(), or a simpler d2:condition() call.

You can try removing the d2:hasValue(‘ovc_ra_hiv_risk_of_hiv’) part of the expression - it does not need to be there, as “Yes” will be the value. Then you can try using double quotes within the expression, like so:

Otherwise you can do the same with a calculation and variables. If you make a program rule variable with the type set to “Calculated value” for each of the fields you want to sum:
Then you can make an assign rule for each of these variables, assigning 1 of the corresponding variable contains ‘Yes’:
Priority: 1
Expression: #{ovc_ra_hiv_active_sex}==‘Yes’
Action: Assign 1 to variable #{cal_ovc_ra_hiv_active_sex}
Then sum up all the #{cal_variables: and assign to wherever you planned to assign in the first place. These will all be 1 if your condition is true.

1 Like

@MSP, just as a side-note, we usually configure Yes/No questions with the numeric values 1 and 0 instead of text values ‘Yes’ and ‘No’, so that we can very easily calculate various results (eg simply sum the data element to get the number of ‘Yes’ responses). I realise this won’t help you with your current problem, as you’ve already configured the program, but it might be worth considering when designing future programs.


Thank you @Markus for your detailed input. I appreciate. I will test this and give an update.

Have a good day!


Hi @Markus. Here is my feedback.

  1. Using d2:condition(’#{ovc_ra_hiv_active_sex}==“Yes”’,1,0) : This one did not work. I got the error like:

Original condition was: d2:condition(’#{ovc_ra_hiv_active_sex}==“Yes”’,1,0) - Evaluation ended up as:d2:condition(’‘Yes’==“Yes”’,1,0) - error message:SyntaxError: missing )

I doubt whether the double quote can work here or not. Kindly correct if I have done anything wrong.

  1. Using Calculate Values: I tried this for 2 of my data elements and it worked. :slight_smile: I guess this could work for me. But the challange is I have 10 data elements to check. This means I should create 10 variables, define 20 rules (one to assign 1 for YES and another one to assign 0 for NO). This approach is not only tedious and timeconsuming but also adds more maintenance work for future changes. If the d2:condition function works, I will be doing a better job. Kindly see, if you can assist with d2:condition error message.

Otherwise, many thanks for sharing your knowledge and experience with me. That is wonderful :pray:

1 Like