Calculations not working

Hi,
I’m experiencing problems with calculations. My survey contains many of them, but none of them works. The xpath however is correct, as kobo does not give me any errors previewing the form, as it would when noting a wrong variable name. I’ve cross-checked them against various sources, user questions, and manuals, and I don’t see what is wrong with them. I’ve tried very simple ones just summing two numeric responses etc., but these also don’t work. This makes me wonder if it may be something in my settings? I’m building the form in xls format. Is it possible that it may be a setting and would someone be able to help me?

Thanks a lot!

Did you check the form with the Online validator, please?

1 Like

Hi @wroos, thank you for your reply. I didn’t know that existed. I checked the form and got the following error. I’m not sure exactly what it means. The variables mentioned are part of a set of different groups. They have relevance statements, which do seem to work for the same questions on another topic (as these are not included in the error now)(e.g. influence_agri is asked when: selected(${member_agri}, ‘1’). The variables related to time have many calculations indeed. Could you help me with what this error means and what I could pay attention to to fix it?

Thanks so much!

The error:
Error: ODK Validate Errors:

XForm is invalid.
: Cycle detected in form’s relevant and calculation logic!
The following nodes are likely involved in the loop:
${source_loan}
${wake_hour}
${dec_small}
${sleep_numeric}
${influence_ywm}
${duration}
${wake_numeric}
${duration_corrected_decimal}
${w_sexp}
${share_duties}
${source_duties}
${influence_trade}
${dec_meet}
${end_time_numeric}
${activity_duration}
${dec_land}
${member_ywm}
${dec_cell}
${end_time_corrected_numeric}
${dec_wash}
${dec_large}
${duration_corrected_decimal}
${end_time_corrected_numeric}
${duration_corrected}
${duration_corrected_numeric}
${dec_wexp}
${dec_loan}
${member_agri}
${final_repeat}
${dec_wcollect}
${dec_wdrink}
${w_wallocate}
${w_wdrink}
${total_duration_numeric}
${influence_credit}
${w_wasset}
${prev_end_time}
${end_time_minute}
${dec_wasset}
${share_rights}
${source_rights}
${source_complain}
${share_complain}
${w_wcollect}
${dec_sexp}
${member_other}
${w_wexp}
${activity_label}
${end_time_hour}
${dec_wallocate}
${influence_agri}
${w_sanitation}
${duration_corrected_numeric}
${source_sanitation}
${share_sanitation}
${awake_duration_numeric}
${dec_wtreat}
${share_water}
${source_water}
${wake_minute}
${w_meet}
${prev_end_time_numeric}
${dec_sanitation}
${activity_duration_numeric}
${influence_other}
${dec_transport}
${influence_water}
${sleep_minute}
${sleep_hour}
${member_water}
${dec_house}
${w_wtreat}
${member_credit}
${member_trade}

The following files failed validation:
${EWI} KoboToolbox Draft Simple.xml

Result: Invalid

It seems that you have self-references in relevant or calculation expressions. (This might be hierarchically.) Please, correct this, and check again.

1 Like

This basically means you have a cyclic - or recursive - dependency among your various calculations. eg

calculation A = f(B) [that is, A is some calculation that depends on B]
calculation B = f(C) [B is some calculation that depends on C]
calculation C = f(D) [C is some calculation that depends on D]
calculation D = f(A) [and D depends back on A, Doh!]

so the value of A is ultimately - thru this chain of dependencies - dependent on its own value! That is called a cyclic dependency which isnt allow in XForms, but can obviously be a bit tricky to detect, because you have to follow all these breadcrumbs to see if you end up where you started…

If you look thru your various calculations you’ll probably find one where you, inadvertently, end up having a cyclic dependency.

Note, you can end up with these invalid cycles not just with calculation expressions, but also - as indicated - with relevant expressions too.

1 Like

Dear @Xiphware, many thanks for your reply. That makes a lot of sense. I could not find any cyclic dependency in my survey, so I tried adding the different groups one by one. The problem occurs when I add the repeat group in this photo. Is it possible that the problem involves my calculation of the previous position’s end time or could you see any other mistakes? And would you know how to solve it? (It’s weird though that the error says it involves all these other variables that have nothing to do with this section and do not present an error if not for the repeat group).

Thanks so much again!

I suspect it may be picking up on these (apparent…) mutually dependent calculations:

That is,

end_time_corrected_numeric = f(prev_end_time_numeric)

but

prev_end_time_numeric = f(end_time_corrected_numeric)

Yes, I know you are trying to pull up the end_time_corrected_numeric from a previous repeat iteration - by using the indexed-repeat() - but the XPath dependecy-checker isnt going to be smart enough to see that from a purely static analysis of your calculation. It is really only by virtue of the fact that the runtime value of the sub-expression position(..)-1 that ensures that you are getting the value of end_time_corrected_numeric from a different repeat iteration than your current one (eg if you just had position(..) then yes it would be obviously cyclic).

If you want to confirm this, change it to:

end_time_corrected_numeric = 42

which should break the cycle. [yeah, it will give you completely the wrong answers, but it should isolate the cause of the error]

1 Like

Immediately, off the top of my head, I dont know how to work around this… :thinking:

Hi @Xiphware, thanks again for your reply! I’ve tested it and it seems that’s indeed what causes the cycle.
Removing the duration checks and correction then doesn’t return any errors on my form anymore in the online validator. However, the calculations and constraints that I have included still don’t seem to work in the preview. E.g. referring to prev_end_time in the activity label, restricting end_time based on prev_end_time (it is restricted, but not correctly as you can’t fill in anything in the preview, hence, it doesn’t seem to recognise the time), etc.
This also seems to be the case for the wake and sleep calculations before the repeat group, as I tried adding a note there to return the awake_duration_numeric variable, which results in ‘NaN’, even though the online validator does not return any errors.

Is there anything you can think of that might be the problem here?
Sorry for the many questions and thanks for your help!

Can you post the entire XLSForm in order to try to debug.

1 Like

EWI KoboToolbox Draft.xlsx (57.8 KB)
This is the one, thanks @Xiphware !
The problem lies with EWI_Module_9. Just to mention, I’ve also tried to take the smaller groups out of the overall module group as I read problems with calculations might occur if they are part of a group, but this didn’t make a difference for me at least.
Thanks again!