I have a question/field in a survey123 that is supposed to calculate a percentage based on two previous questions. The formula:
round(${TOTAL_CM_TREAT}div((${TOTAL_CM_TREAT}+${TOTAL_CM_UNTREAT}))*100,1)
However it won't calculate if ${TOTAL_CM_UNTREAT} is zero. If TOTAL_CM_UNTREAT is > 0 it works fine. However I want it to be able to calculate even if TOTAL_CM_UNTREAT is zero. Any advice?
Thank you!
Hey @RileyScanlan
I believe this is due to the function attempting to divide by zero, you can use an IIF statement to get around that here:
Round(${TOTAL_CM_TREAT} / (${TOTAL_CM_TREAT} + IIF(${TOTAL_CM_UNTREAT} == 0, 0, ${TOTAL_CM_UNTREAT})) * 100, 1)
Is TOTAL_CM_UNTREAT actually 0 or is it empty? If that field is left blank it will be treated as NaN - which you can't do math on. You need to either set TOTAL_CM_UNTREAT to default to zero, adjust any calculations you have on TOTAL_CM_UNTREAT to ensure it is always populated, or adjust your percentage calculation to account for possible empty fields.
round(${TOTAL_CM_TREAT}div((${TOTAL_CM_TREAT}+ if(string-length(${TOTAL_CM_UNTREAT}) < 1, 0, ${TOTAL_CM_UNTREAT})))*100,1)