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