Desktop User Guides > Professional > Interview scripting > Writing interview scripts > Filtering questions and response lists > Filtering loops > Filtering a grid using answers from a previous grid
Filtering a grid using answers from a previous grid
The metadata for the example is as follows:
FoodList define
Fruit, Vegetables, Salads, Fish, Meat, Bread,
Dairy "Dairy produce",
Pulses "Pulses/Nuts/Cereals",
PastaRice "Pasta/Rice"
FoodLoop "Which of the following do you eat as part of
your normal diet?" loop
{use FoodList}
fields (
NormalDiet "" categorical [1..1]
Yes, No
) expand;
LastWeekLoop "And roughly how many portions of these foods
do you eat each week?" loop
{use FoodList}
fields (
Portions "" long [1..100]
) expand;
The routing section contains the following code. Notice how the filtering for the second grid has been placed in a function, making it reusable elsewhere in the survey.
LastWeekLoop.QuestionFilter = GridIterationsContainsAny(FoodLoop, {Yes})

Function GridIterationsContainsAny(Grid, Answers)
Dim Iteration
For Each Iteration In Grid
' Iteration[0] is the first categorical question in the grid
If Iteration[0].Response.ContainsAny(Answers) Then
GridIterationsContainsAny = GridIterationsContainsAny + CCategorical(Iteration.QuestionName)
End If
End Function
The function requires two parameters: a grid question name and a list of answers. The example function tests only the first categorical question in the grid, but could be extended to test other questions too. It checks whether the response given to the question contains any of the answers passed to it: in this example, whether the response contains Yes. If so, that category is added to the list of categories for the second grid. So, if a respondent regularly eats fruit, salads, vegetables and bread, the second grid will ask the number of portions for fruit, salads, vegetables, and bread only.
See also
Filtering loops