Having a default fill-in in date type question with year appearance

Hello ODK Forum. I have been familiar with ODK programming for a few years, although this is my first ODK-related post.

I'm experiencing trouble when trying to have a pre-fill date type question with a year appearance (it just requires to register a year, not a YY-MM-DD value). The question which requires this is "When did you start working at [organization's name]?", and I would like to have it filled-in by default with the current year minus the number of years the respondent has within the organization he/she is currently working for, so enumerators would possibly go through this question fastly.

To get this, I programmed the following lines:

i) A line to calculate the current year minus the number of years the respondent in the organization:

type name calculation
calculate year_start int(substr(string(${today}),0,4))-round(${years_working},0)

(Note that "today" is today's date and "years_working" is the number of years in the organization, which is a decimal type)

ii) A line which concatenates this with "-01-01":

type name calculation
calculate date_default concat(string(${year_start}),'-01-01')

So the result of this calculation will be a date string which should be able to be read as the first day of January of the (likely) start year.

iii) Finally, I programmed the question which should be pre-filled as the following:

type name appearance appearance calculation
date start When did you start working at ${organization}? year ${date_default}

I have tried this coding with Kobo Collect (v1.14.0a) and SurveyCTO Collect (v2.20). And something very strange I have noticed is that it actually works with Kobo Collect, but SurveyCTO shows an error message. For example, if the result of date_default is '2015-01-01', it displays: "2015-01-01 cannot be converted to a date value", and the same happens with other year values.

Have you ever ran into a similar issue?

Thanks very much in advance.

Luis Eduardo

Kobo Collect and SurveyCTO Collect are both based on older versions of ODK Collect so if you really need to use one of those, please contact support for those teams.

In ODK Collect, what you have done is close to working but does not allow the user to override the default. That is because calculations are run again on form save. If you want a dynamic default that is user editable, wrap the calculation in once(). The mental model to have with calculations is that the whole form is evaluated at once on load and then dependent calculations are recomputed as values change. Then the whole form is re-evalulated on save.

The first evaluation on form launch would cause problems with once() . To address that, we can make sure to only set the value for the default start year once year_start has been set.

Here's how I would approach this:

type name label appearance calculation
integer years_working How many years worked?
calculate year_start int(format-date(today(), "%Y"))-round(${years_working}, 0)
calculate date_default concat(string(${year_start}),'-01-01')
date start When did you start working at org? year if(${date_default}='-01-01', '', once(${date_default}))
1 Like