DHIS2 Program Rule Mathematical Functions

Hi everyone, I am building a tracker program on version 2.38.0 .
I am trying to raise a base number to a power (exponent) using program variables.
e.g.

#{ Variable - A } = 2
#{ Variable - B } = 5

#{ Variable - C } = ( #{ Variable - A } ) ^ ( #{ Variable - B } )

#{ Variable - C } = ANS

I have not been able to identify a function that can help me with this within the program rule configuration e.g.

NumPower( A, B)

which could be used in my case as:

NumPower( #{ Variable - A } , #{ Variable - B } )

Would someone be able to point me in the right direction or share any ideas that I can use to implement this calculation. Any assistance will be appreciated, thanks.

Hi, welcome to the community @Sika_Se :tada::tada::tada:

Would you like to share the use case and how this could be used? Preferably a real example. If condition which uses the exponent is = ANS then action?

I also need to ask if power ^ is supported in Program Rules - it is supported in Program Indicators.

thanks for your feedback @Gassim.

The program indicator functionality will be helpful for the reporting side.
However, in this case, I am trying to display calculated variables within a tracker program during data entry. These can be used as entry variables in other tracker programs within the same instance. E.g. a lab team calculate dosages for a certain combination of drugs in a lab tracker program and the team at the pharmacy can view the forms completed by the lab team and dispense drugs to patients based on the details in the lab forms while they complete forms in a pharmacy tracker which would be capturing different data elements.

Any update on this?

Hey,

I received a response from @superskip and he said that instead of using ^ a double asterisk ** works (even though the parser claims it’s invalid)

Thanks!

I would not recommend using this though. It works front-end but probably not on the server side. Also it is not officially supported, and if it ever will be, I think it’s more likely to be implemented using ^.

thanks for the feedback people. I will try it out and let you know how it goes

Hi all,

Is there any update on this @superskip @Gassim? I have a similar use case for assigning calculated expressions and showing messages.

In addition to exponent, having log and ln functions is also super useful. I mean having frequently-used algebraic functions that are required for many calculations such as WHO CVD risk score or CKD-EPI Creatinine Equation, provides a considerable added value.

Hi,
In DHIS2 version 2.38.0**,** the program rule engine does not support the ^ exponent operator , and there is no built-in NumPower () function available in program rule expressions.

However, you can still implement exponentiation depending on your use case.

Option 1: Use the d2: log () and exp () workaround (Recommended)

The program rule engine supports mathematical functions such as

d2:log(x) → natural logarithm (ln)

exp(x) → exponential function (e^x)

Using the mathematical identity:

AB=exp (B∗ln (A)) A^B = exp (B * ln (A)) AB = exp (B ∗ ln (A))

You can define:

#{Variable - C} = exp( #{Variable - B} * d2:log( #{Variable - A} ) )

Example:

If A = 2, & B = 5

Then: C = exp (5 * d2: log (2))

This correctly returns 32.

Important:

A must be greater than 0 (since the log of zero or negative numbers is undefined).

Works well for positive numeric values.

Option 2: Manual Multiplication (If Exponent is fixed)

If B is always a known small integer (e.g., 2 or 3), you can manually define:

For square:

#{C} = #{A} * #{A}

For cube:

#{C} = #{A} * #{A} * #{A}

But this is not scalable for dynamic exponents.

Option 3: Pre-calculate via Calculated Data Element (Analytics Layer)

If the exponentiation is complex and used for reporting rather than immediate program rule validation:

Create a Program Indicator

Use analytics expression if supported in your version

Or handle calculation at dashboard/visualization level

Why ^ Does Not Work

In DHIS2 (2.38), the program rule engine is based on a simplified expression parser and does not support:

^ operator

Math. Pow()

Custom power functions

Only supported math functions are documented in the Program Rule Expression Guide.

Best Practice Recommendation

For dynamic exponentiation inside Tracker programs:

Use: exp (B * d2: log (A))

Validate that A > 0
Round if needed using d2: round (exp (B * d2: log (A)), 0)

Final Working Example

d2:round(
exp( #{Variable - B} * d2:log( #{Variable - A} ) ),0)

This will return 32 for A=2 and B=5.

Hi Inye,

I assume your proposed options are ChatGPT-generated because as far as I know, there is no d2:log or exp function implemented in DHIS2 for program rule expression, at least until last week (version 2.42.2).

Hi @malekpour

Please know that these calculations (exponents and log) are available when using program indicators, so the real question is whether the objective is actually the program rule itself or the calculation? If the calculation is the objective then a program indicator will work (results for analytics appear only after the analytics tables export is run in the Data Administration app).

If using Program Rules to perform the calculation is necessary then this is a feature request. You’re welcome to create a feature request and link it to this topic (please see ideas!)

Thanks!

Dear @Gassim,

Thanks for your reply. Yes, these functions are needed for PRs to dynamically display dependent data elements. For example, if patient score is above X (which log is required for its calculation), hide DE Y.

Based on your guidance, I’ll request this feature.

Thanks!

Thanks @malekpour !

Just want to add a link to the related topic: Implementation of `log` and `exponent` function in Program Rules - #6 by malekpour