Dynamic data attachments - get most recent submission - issue with max(instance()) on KoboCollect

Hello! I am trying to use dynamic data attachments to auto-populate a survey with data from the most recent submission that has the same project name. To do this, I construct a unique identifier (string) using the project location/name and the date from calculating today(). Then I pull in the most recent date using:
date(max(instance('same_form')/root/data[project_id =current()/../project_id]/todays_date))

It all works beautifully on the Enketo web form, but the most recent date is showing up as blank when I use the KoboCollect app. The issue seems to be with taking the max of a list using instance(). When I try to print out just the list of previous entry dates on KoboCollect, I get the error:
XPath evaluation: type mismatch This field is repeated: instance(same_form)/root/data[10]/todays_date[1];instance(same_form)/root/data[32]/todays_date[1] You may need to use the indexed-repeat() function to specify which value you want.

Is there a way to retrieve the most recent submission with a matching project ID using KoboCollect or will it only work with the web form?


dynamic-data-dates.xlsx (16.0 KB)

@barbaraT, pinging @Xiphware to see if there is any issue with the Collect Android App.

Could you try to better locate the problem, please: Show with a text field (or note) what you get in Enketo and in Collect

  1. for the instance(…) expression alone?
  2. Afterwards add the max and review.
  3. Finally add the date.

I am able to see the instance() expression and the others alone on Enketo, here is what that looks like:

But when I do the same in KoboCollect, I get an error:

Here is what this part of the XLS form looks like:

It seems, the problem has nothing to do with the max (or outside date) part, but the instance part. As you can see, also Enketo is showing one single date value only (which will then be the max, of course). I am afraid this is not what your are looking for. It seems similar to a repeat group: As far as I know, a ${repeatGroup} will only show the first value. (You would need to apply a join(…) to combine all repeat values into a string, separated with blanks.)

What is rather strange in your Enketo screen shot: There are 3 different dates! Why?

Further hint: You use today(), in line 6. Take care this will always be recalculated on load/save/edit of the form. Maybe, you need to use once(today). Or set as default? Or ${today} metadata

1 Like

Dear @barbaraT ,
this error appear because the identifier is repeated,
meaning that, the “project_id” is duplicated in the data , whenever an instance find more than one same value it will throw this error , you have to ensure that the project_id is unique.


Thanks for your suggestions! I ended up using join(…) on the instance(…) and then selecting the last 10 characters using substr(…), which seems to work well enough.

For the Enketo output, I got three different dates because the first one is the first date converted to a string, and the next one is date() applied to that first date string, which converts it to UTC. Then the last one is the max date from whatever Enketo uses on the back end to allow the calculation right on instance().

Hint: If you re-edit (or load/save) the case, your date calculation will always be updated. If you don’t want this, you would need to add once(…).