I have a workflow that would be improved by restricting displayed cases to the first n returned cases on a caselist.

Is this possible?


  • There could be up to 1,500 cases on a mobile device

  • Sorting is randomized (and re-randomized) regularly based on events in a separate module. It's a pretty loose algorithm for assigning the random numbers with possible duplicates. It uses MOD, which I think means they'll always be ints.

  • What are the characteristics of the case property that it is sorted on? Is it a decimal from 1 to 0 or is it a range of numbers where 1 is top and N is the bottom? If it's from 1 to N you could easily restrict the case list by adding a filtering ensure that the sort property is less then say 15 to show the top 15 cases. Commented Feb 17, 2017 at 7:26
  • @BenRudolph I've accepted your edits and (I hope) provided some clarity about the sorting
    – Derek
    Commented Feb 17, 2017 at 8:42

1 Answer 1


This can be accomplished by adding a filter based on the position() of the current element, but the syntax will be a bit of a hack because you need to introduce a second predicate filter after the current one to constrain the scope.

Example: If your current filter is

property < 50

I believe you can replace it with

property < 50][position() < N

where N is the number of results you want.

NOTE: You won't be able to see "more" cases, so the user will have go to back and forth in order to see additional results.

Update: It looks like HQ no longer allows you to use ][ to introduce a second predicate filter expression, so this doesn't work at the moment.

  • Nice I was searching for a function like this here confluence.dimagi.com/display/commcarepublic/CommCare+Functions Commented Feb 17, 2017 at 8:37
  • @ClaytonSims I think maybe position() doesn't return exactly what one would expect. I say this because the original result was 10+ cases and applying position()<5 got me only 1 result.
    – Derek
    Commented Feb 17, 2017 at 9:00
  • Important note: this solution will give the first N-1 returned cases, but unfortunately it identifies those cases BEFORE the sorting happens. So in my situation, 9 cases make it through the filter then it gives me the first 5 and THEN it randomizes them, instead of giving me 9 cases, randomizing the order and giving me back the resulting "first 5."
    – Derek
    Commented Mar 21, 2017 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.