Show a Number of Randomly Selected Rows from a Grid

As part of our core randomization options you have the ability to randomize or shuffle column and row order in a grid question. However, there is not a built-in feature to show a random subset of the rows in your grid question. Fortunately, it's pretty easy to script. We'll cover the steps to use a pretty simple script to accomplish just this. These steps assume a basic familiarity with SurveyGizmo and programming.

There are two approaches you might want to take when randomly showing a subset of rows from the grid.

  1. You might want to simply show a reduced number of grid rows to reduce respondent burden and fatigue and ultimately gather better data. This script is covered below in Option 1.
  2. You can also show a subset of rows based on the selected options from a previous question. This script is covered below in Option 1.

Option 1: Show a random subset of rows

See this script in action in an example survey.

OR

Add a survey with this script and setup to your account.

Features and functions used in this script:

Set up your grid question. Our example grid question looks like the below.

In the interest of reducing respondent burden and fatigue, we only want to burden each respondent with responding to three of the ten rows in this grid question.

To set this up we added a Custom Script Action to the top of the page with the grid question and pasted the below script.

--Row IDs to be randomly hidden go here. Make sure they are comma separated, no spaces, and inside quotes.
rowsHideRandom = {3,4,5,6,7,8,9,10,11,12}

--Number of rows you want shown from the list above. No quotes or commas are needed.
shown = 3

--Do not change after this line.

shuffled=tableshuffle(rowsHideRandom)

for key,question in pairs (shuffled) do
    hidequestion(question,true)
end

for i=1, shown do
  	hidequestion(shuffled[i],false)
end

Required Customizations

In the script above you will need to customize variables highlighted in yellow in order to make the script work in your survey.

rowsHideRandom - Replace this list with the question IDs for your grid rows. 

shown - Change this to the number of rows you wish to show.

Option 2: Show rows that correspond to previously selected options

See this script in action in an example survey.

OR

Add a survey with this script and setup to your account.

Features and functions used in this script:

Set up your source question and follow-up grid question. Our example source and grid questions look like the below.

We only want to burden each respondent with responding to a maximum of three rows that correspond to their selections in the previous Checkbox question. 

To set this up we added a Custom Script Action to the top of the page with the grid question and pasted the below script.

source = 2 -- Multiple choice source question ID
targetgrid = 3 -- Target Grid question ID
shown = 3 --select 3 to show

-- Replace the key text with the text of your row headers and the numbers with the row question IDs
vals = {}
vals["United"] = 6
vals["Delta"] = 7
vals["Southwest"] = 8
vals["Frontier"] = 9
vals["American"] = 10
vals["Alaska"] = 11
vals["JetBlue"] = 12
vals["Air Canada"] = 13
vals["Virgin America"] = 14
vals["Continental"] = 15
vals["Spirit"] = 16

--Get all the IDs for each row in target grid and set each to being hidden

targetskus = gettablequestionskus(targetgrid)

for key,question in pairs (targetskus) do

	hidequestion(question,true)
end

options_selected = getvalue(source) -- get all options selected
rowidselection = {} -- a new array to hold the row ids of the grid to randomize from

for key,value in pairs(options_selected) do
  
   table.insert(rowidselection, vals[value])
  
end

--shuffle the array

shuffled = tableshuffle(rowidselection) 

--set the rows to show

for i=1, shown do

	hidequestion(shuffled[i],false)

end

Required Customizations

In the script above you will need to customize variables highlighted in yellow in order to make the script work in your survey.

source - The question ID of the source question you wish to use to determine which rows will show.

targetgrid - The question ID of the target grid you wish to affect.

shown - Number of random rows to be shown.

vals - Replace the key text with the text of your row headers; make sure to preserve the quotes. The value numbers with the row question IDs.

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your needs. Beyond this, check out our Professional Services; these folks have the scripting chops to help you to achieve what you are looking for!

Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In