Hello! I would like to filter the available answer options in a select multiple question by the date. Ideally, it would only show sample IDs that are expected to be collected within 3 weeks of the datetime the user selects.
I tried just filtering with a less than (shown below) as a trial. No options showed up.
int(decimal-date-time(date_filter) - decimal-date-time(${datetime_extraction})) < 21
Where:
date_filter is the filter choice on the list. It represents what date we expect the sample would be processed. I formatted as date-time on the csv hoping that might prevent calculation issues with different formats.
datetime_extraction is what the user selects (in date-time format). It represents the actual date the processing is occurring.
I’m new to Kobo but thought this would be a useful approach for our study. It is also worth noting I’m doing this using an external form (select_multiple_from_file) but am certainly open to putting all on the same XLS form if that helps.
Hints:
If the date_filter is a column of the choices it is a text value there (not a datetime). So, you may need to convert it first https://getodk.github.io/xforms-spec/#fn:date
int(…) will cut off any decimals.
Your hint of converting the date_filter to date was the key!
For simplicity on the trial, I did not use pull_data, and I only used one date filter. That is, the only samples that do not appear are those that are expected to be collected more than 21 days after the date the user selects (as opposed to samples expected to be collected within 21 days of the date the user selects).
On my select_multiple question, I used the following choice filter:
(decimal-date-time(date(date_filter)) - decimal-date-time(${datetime_extraction})) < 21
Where:
date_filter is the expected date a sample ID will be collected, and is the choice filter.
datetime_extraction is what the user selects, which represents when the processing is actually occurring.
Well, apparently this solution works well on preview mode. Only the answers within a certain timeframe of my selection appear. However, when I try to deploy it, I get the following error: >> Something broke the parser. See above for a hint.
*org.javarosa.xpath.XPathTypeMismatchException: XPath evaluation: type mismatch * The value “2022-07-25 00:00:00” can’t be converted to a date.
The following files failed validation: ${sample_collection_826_v2}.xml
Result: Invalid
I’ve tried a couple different date and text formats. They keep working on preview but cannot deploy. Does anyone have any suggestions?
I’m not sure why, but later when I tried doing this where I pulled the data from a csv, it seemed to work. Someone with more expertise may have a better idea of why, but my guess is that it prevented some formatting error. So in summary, to filter by date:
Add a date filter to the choice options.
On the form (survey tab), you need to apply “date()” to the date_filter to get it to properly work.
For example, “(decimal-date-time(date(date_filter)) - decimal-date-time(${datetime_extraction})) < 21” would prevent options that occur 3 weeks after the datetime you selected from appearing.
Not quite sure why, but if you do not pull data from a csv there may be an formatting error. It will run correctly in the preview, but cannot be deployed. If you do pull data from a csv, that issue can be avoided.
Hello @sphilto,
Could you provide the related (anonymised) part of your survey, including some choices, please, so we might test the formatting issue without pulldata?
Hello! I have attached this example of an anonymized version. It works on preview, but cannot deploy (“XPath evaluation: type mismatch The value “2022-08-24 00:00:00” can’t be converted to a date.”).
When I tried pulling data, it was able to deploy. It worked during preview and I could submit it on the computer. However, when trying to submit on an android, I received the following error:
In that specific example, I only assigned two dates (9/26/2022 & 10/3/2022, which represent two weeks of sampling). So ~ half (including case ID 3) have that same value.
I just tried changing the dates (adding one week to each). Once again, it worked perfectly on the computer. However, this time when I tried opening the form on my phone, I received this error.