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.