Filter answer choices by date

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.

Welcome @sphilto,
Could you provide the related part/extract of your XLSForm, please, incl. choice_list examples?

I would recommend to first develop the solution without pulldata.

You may also have a look at a previous discussion found with the recommended search function of the forum:
https://community.kobotoolbox.org/t/epidemic-calendar-as-reporting-period/2232

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.

1 Like

Hi @sphilto ,

The video below makes it practically clear for you.

Sorry for the late response!

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.

Some examples of choices are:

After I select a date at the beginning of the form, only those that are expected to have been collected or will be collected in 21 days appear.

Thanks so much for your help!
Stephen

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?

Thanks!

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.

2 Likes

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?

1 Like

Hello @FEMMMY,
Could you add the video, please?

1 Like

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.”).

filtering_samples_bydate_nopull.xlsx (11.5 KB)

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:

Hello @sphilto,
Could you solve the problem meanwhile? How?

Thanks for checking! I have not figured it out. In the meantime I’ve been using other filters but will probably try again in the near future.

It seems there was a problem with case ID 3, see screenshot. Could you check the related data in the csv? And provide a screenshot of this csv part?

1 Like

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.

Here is that file and a screenshot.
image
ghana_sample_ids_community.csv (1.4 KB)

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.

Lastly, if it’s helpful, I have attached the example form.
aHRP3kCYT63rEsUuNjcTBT.xlsx (11.7 KB)

There are similar examples, found with the search function of the forum, see
https://community.kobotoolbox.org/search?context=topic&context_id=31027&q=%3Clabel%3E%20node%20for%20itemset%20doesn%E2%80%99t%20exist&skip_context=true

Esp. the following might help you,

1 Like

Would be great, if someone could test it with the newest ODK version. May the UTF issue is solved now, see "Highlights from ODK v2022.3:

  • Submissions are UTF-8 encoded"

Thanks for the suggestion!

I tried both CSV and CSV UTF-8. I didn’t get the “node for itemset doesn’t exist” error, but I’m still getting the date error (shown below).

@wroos ,

Please video for what?

Hello @FEMMMY,
See your previous post above, please

1 Like

@wroos so sorry for the ommission