SurveyLoopr
Tips & Tricks
12 min read

Rank Question in XLSForm: Order Selected Options

Mehrab Ali
Mehrab AliPublished November 20, 2026

To add a rank question in XLSForm, write rank followed by a choice list name in the type column, such as rank need; the respondent drags the choices into order and the answer is saved as the choice codes in ranked order, for example 4 1 3 2 5. To rank only the options picked in an earlier select_multiple, add selected(${earlier_question}, name) in the choice_filter column. Every example below compiles with pyxform and ODK Validate.

Download example form (.xlsx)All six examples in one XLSForm, one group each. Tested with pyxform and ODK Validate; free to reuse under CC BY 4.0.Download

How do I add a rank question?

A community needs assessment asks each respondent to order five needs from most to least urgent:

typenamelabelhintrequired
rank needneed_rankRank these needs from most to least urgent for your community.Put the most urgent need at the top.yes
surveychoicessettings
list_namenamelabel
need1Drinking water
need2Health clinic
need3Primary school
need4Road repair
need5Electricity
surveychoicessettings
  • The choices sheet is the same as for a select_one: a list_name, a code in name and a label.
  • The answer is one value: the codes separated by spaces, first-ranked first. Electricity, water, school, clinic, road is saved as 5 1 3 2 4.
  • Every choice shown is ranked. The widget has no "top three only" mode; for that, see the first, second, third choice pattern below.
  • Mark it required. The answer stays empty until the enumerator opens the list and confirms an order.

How do I get the first, second and last ranked item?

A ranked answer is a space-separated list, the same shape as a select_multiple answer, so selected-at() reads one position from it. Positions start at zero:

typenamelabelcalculationrelevantrequired
rank needpriority_rankRank these needs from most to least urgent.yes
calculatepriority_firstselected-at(${priority_rank}, 0)
calculatepriority_secondselected-at(${priority_rank}, 1)
calculatepriority_lastselected-at(${priority_rank}, count-selected(${priority_rank}) - 1)
textwater_problemWhat is the main problem with drinking water here?${priority_first} = '1'yes
surveychoicessettings
  • selected-at(${priority_rank}, 0) is the top-ranked code, 1 the second, and so on. Asking for a position that does not exist returns an empty value, not an error.
  • count-selected() counts the items in the list, so count-selected(...) - 1 is always the last position, however many choices there are.
  • The follow-up question uses the calculate in its relevant: it is asked only when drinking water (code 1) was ranked first.

The calculates hold codes, not labels. To show "Drinking water" in a later question or note, see show the choice label, not the value.

How do I rank only the options selected in a previous question?

This is the most common ranking question on the ODK and KoboToolbox forums: ask which options apply, then rank only those. Use one choice list for both questions and filter the rank question with selected(). A household shocks module:

typenamelabelchoice_filterrelevantcalculationrequired
select_multiple shockshocksWhich of these shocks affected the household in the last 12 months?yes
rank shockshock_rankRank these shocks from most to least damaging.selected(${shocks}, name)count-selected(${shocks}) > 1yes
calculateworst_shockif(count-selected(${shocks}) > 1, selected-at(${shock_rank}, 0), ${shocks})
calculateworst_shock_labeljr:choice-name(${worst_shock}, '${shocks}')
noteworst_shock_noteMost damaging shock: ${worst_shock_label}
surveychoicessettings
list_namenamelabel
shock1Drought
shock2Flood
shock3Crop pests or disease
shock4Livestock disease
shock5Illness or death of a household member
shock6Loss of a job or income
surveychoicessettings
  • selected(${shocks}, name) keeps a choice only if its code was ticked in shocks. name here is the name column of the choices sheet, typed as is, with no ${}.
  • count-selected(${shocks}) > 1 hides the ranking when one shock was picked: there is nothing to order.
  • worst_shock covers both cases. With two or more shocks it takes the top of the ranking; with one, it takes that shock.

The filter itself is explained in more detail in filter choices by a select_multiple.

Warning:

Test a filtered rank question in the app your team will use, both on a phone and in the web form. Forum users have reported versions where a filtered rank list was displayed but could not be reordered, and where selected-at() on a rank answer showed nothing in a web form until an update.

How do I ask for only the top three?

The rank widget orders every choice it shows. With 12 crops and a respondent who only knows their main three, that produces nine positions of noise. Ask for the first, second and third choice as three select_one questions, each one hiding what was already picked:

typenamelabelchoice_filterrelevantrequired
select_one cropcrop_1Which crop earned the household the most income last season?name != '0'yes
select_one cropcrop_2Which crop earned the second most?name != ${crop_1}yes
select_one cropcrop_3Which crop earned the third most?name != ${crop_1} and name != ${crop_2}${crop_2} != '0'yes
surveychoicessettings
list_namenamelabel
crop1Rice
crop2Maize
crop3Wheat
crop4Potato
crop5Vegetables
crop6Jute
crop0No other crop
surveychoicessettings
  • name != ${crop_1} removes the first answer from the second question, so the same crop cannot be picked twice.
  • "No other crop" (code 0) is hidden from the first question and ends the sequence: crop_3 is asked only when crop_2 is a real crop.
  • Each rank is its own column in the data (crop_1, crop_2, crop_3), which is easier to analyse than one space-separated value.

This pattern also works on platforms that have no rank widget.

How do I get the position of one item?

There is no function that returns where a given choice sits in the ranking. Check each position in turn with nested if(). The position of drinking water (code 1) among the five needs:

typenamelabelcalculationrequired
rank needurgency_rankRank these needs from most to least urgent.yes
calculatewater_positionif(selected-at(${urgency_rank}, 0) = '1', 1, if(selected-at(${urgency_rank}, 1) = '1', 2, if(selected-at(${urgency_rank}, 2) = '1', 3, if(selected-at(${urgency_rank}, 3) = '1', 4, 5))))
surveychoicessettings

water_position is 1 when water is ranked first and 5 when it is last. With more than a handful of items, skip this in the form and split the ranked answer into positions at analysis time instead; one calculate per item gets long quickly. Nested conditions are covered in the XLSForm if statement guide.

How do I stop the list order from deciding the ranking?

A rank question starts in the order of the choices sheet. A tired respondent or a rushed enumerator who accepts the list as shown records that order, and the first choice on your sheet "wins" in the data. Shuffle the starting order per interview:

typenamelabelparametersrequired
rank needneed_rank_shuffledRank these needs from most to least urgent.randomize=trueyes
surveychoicessettings

randomize=true goes in the parameters column, not in appearance. The saved codes are unaffected; only the order on screen changes. See randomize choices and question order for seeds and for keeping one order per respondent.

Note:

If many interviews by the same enumerator come back with the ranking in exactly the choices-sheet order, the list was probably accepted without being asked. Randomizing the start order makes that pattern impossible to produce by accident.

Common rank question mistakes and how to fix them

These are the messages pyxform returns, which is what ODK Central, KoboToolbox or the XLSForm converter shows when it rejects the form:

You wroteWrite insteadError you will see
rank with no list namerank needArguments 'itemset' and 'list_name' must not both be None or empty.
rank needs when the list is called needthe exact list_name from the choices sheet[row : 2] List name not in choices sheet: needs
ranking need or rank_one needrank needUnknown question type 'ranking need'.
selected(${shocks}, name) in choice_filter with no shocks questionadd the select_multiple before the rank question, or fix the nameOn the 'survey' sheet, the 'choice_filter' value is invalid. Reference variables must contain a name from the 'survey' sheet. Could not find the name 'shocks'.
selected-at(${need_rank}, 1) for the top itemselected-at(${need_rank}, 0)No error, but you get the second item: positions start at zero
A filtered rank question with no relevantcount-selected(${shocks}) > 1 in relevantNo error, but a one-item list is shown for ranking
randomize=true in appearanceput it in parametersNo error, but the order is not shuffled

For other messages, see common XLSForm errors or paste the form into the free XLSForm validator.

Does this work the same in ODK, KoboToolbox and SurveyCTO?

No. rank is an ODK XLSForm type, and the three platforms differ more here than on most question types:

Platformrank typeFirst, second, third choice with select_one
ODK Collect and ODK Central web formsYes, as shown aboveYes
KoboToolbox (XLSForm upload)Yes, same syntaxYes
KoboToolbox form builderHas its own "Ranking" question, saved as begin_rank and rank__level rowsYes
SurveyCTONo rank field typeYes, with SurveyCTO's own choice filter syntax and relevance column

KoboToolbox's form builder "Ranking" question is not the rank type. It stores one answer per rank level between begin_rank and end_rank rows, with the list in a kobo--rank-items column. That layout is KoboToolbox's own and is not standard XLSForm, so a form built that way will not load in ODK Central. If the form has to run on both, write rank need in the XLSForm and upload it.

When to use a rank question

Use rank when the full order matters and the list is short: five to seven items a respondent can hold in mind. Use the first, second, third choice pattern when only the top of the list matters, when the list is long, or when the form must also run on SurveyCTO. For long lists, let the respondent pick what applies first and rank only that. For the other select-style types and how they display, see the XLSForm appearance column.

Tip:

You can describe the question in plain language instead: "ask which shocks hit the household, then have them rank only the ones they picked, and store the worst one". LooprAI writes the rank question, the choice filter and the calculates, then compiles the form with pyxform before anything is saved.

Build my first form free

Next: XLSForm appearance column →

Frequently Asked Questions

How do I create a ranking question in XLSForm?

Write rank followed by a choice list name in the type column, for example rank need, and define that list on the choices sheet. The respondent puts the choices in order and the answer is saved as the choice codes in ranked order, separated by spaces.

How do I rank only the options selected in a previous question?

Use the same choice list in a select_multiple and in the rank question, then write selected(${earlier_question}, name) in the choice_filter column of the rank question. Add count-selected(${earlier_question}) > 1 in relevant so it is skipped when only one option was picked.

How is a rank question saved in the data?

As one column holding the choice codes in ranked order, separated by spaces, such as 5 1 3 2 4. The first code is the top-ranked item. Split the value on spaces at analysis time, or add calculate rows with selected-at() to store each position.

How do I get the top-ranked item in a calculation?

Use selected-at(${rank_question}, 0) in a calculate row. Positions start at zero, so 1 is the second item and 2 the third. The result is the choice code; use jr:choice-name() with a select question on the same list to get its label.

Can respondents rank only their top three?

Not with the rank widget, which orders every choice it shows. Ask three select_one questions on the same list instead, with a choice_filter such as name != ${first_choice} on the second and third so an option already picked cannot be chosen again.

Does the rank question work in KoboToolbox and SurveyCTO?

KoboToolbox accepts the rank type in an uploaded XLSForm; its form builder has a separate Ranking question saved as begin_rank rows, which is not standard XLSForm. SurveyCTO has no rank field type, so use first, second and third choice select_one questions there.

No Credit Card Required

Build the form first. Add hosting when you need it.

Start free with LooprAI, then deploy a managed ODK Central server or run DataSnap checks when your project is ready.

Automation handles the mechanical work.

Researchers decide what the evidence means.