How to Filter the Rows of a Table
Filters selected from the drop-down menus below a table apply to the whole table. In some cases you may wish to filter the rows of a table using a different filter for each row. One example may occur when each row of the question represents a brand and you wish to filter each by respondents who use that brand. When the filter condition is satisfied, the new variable will take the same value as the old variable. When the filter condition is not satisfied, the new variable will be assigned a missing value. This removes the respondents who do not satisfy the filter from the base. In this case a new copy of the question must be constructed using JavaScript variables, where each variable has the filter built in. This concept can apply to other question types with multiple variables.
Method 1: Using a Rule
You can filter rows and columns of a table using Filtering - Apply Filters to Columns or Rows. However, this will turn off statistical testing. If you require statistical testing, see Method 2.
Method 2: Creating Filtered Variables
The example below shows you how to construct a new copy of a Pick One - Multi question in this way using the Cola.Q example project from your Examples folder. However, the same approach can be used with other Question Types. The Pick One - Multi question will be Q6. Brand Attitude, and each brand in the question will be filtered by respondents who drink that particular brand weekly, according to Q9. Cola weekly. To test this example you should:
- Open C:\Program Files\Q\Examples\Cola.Q (this may be located on a different place on your computer depending upon how Q was installed).
- Go to the Variables and Questions tab.
- Highlight all of the variables from the Pick One - Multi question Q6. Brand Attitude.
- Right-click and select Copy and Paste Questions: Linked. This will create a new copy of the question below.
- Right-click on the first variable in the new question (Coca Cola) and select Edit Variable. (Make sure that you only have the first variable selected.)
- Change the text in the Expression box to the appropriate expression for Coca Cola that is listed below.
- Click OK.
- Repeat the previous three steps for each variable in the question, substituting the appropriate Expression from below.
- Change the entry in the Question column to Q6. Brand Attitude - Filtered by Weekly Drinkers.
The appropriate JavaScript expressions for each brand are as follows:
- Coca Cola
if (Q9_A_3) Q6_A;
else NaN;
- Diet Coke
if (Q9_B_3) Q6_B;
else NaN;
- Coke Zero
if (Q9_C_3) Q6_C;
else NaN;
- Pepsi
if (Q9_D_3) Q6_D;
else NaN;
- Pepsi Light
if (Q9_E_3) Q6_E;
else NaN;
- Pepsi Max
if (Q9_F_3) Q6_F;
else NaN;
Note that:
- The if statement in each formula checks the relevant variable from Q9. Cola weekly to see if the respondent is counted as a weekly drinker of that brand.
- The second line assigns a missing value, or NaN. This removes the respondent from the base for this variable, effectively filtering them.
- In this example you can apply the new filter for the first variable and then right-click it and choose Insert Ready-Made Formulas: Use as Template for Replication as a shortcut. However this may not apply in all cases as it relies on a common structure for the two questions involved. See Use as Template for Replication for more details on using this feature.
See Also
- JavaScript Variables for detail on how to create new variables in the Variables and Questions tab using JavaScript.
- JavaScript for information about the JavaScript programming language.
- Use as Template for Replication for a way to save time when creating JavaScript variables from questions with common structure.
- Filtering - Filter One Question by Another.
- Filtering - Apply Filters to Columns or Rows.
Pages with syntax highlighting errors
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Creating And Modifying Tables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Filters
Q Technical Reference > Troubleshooting
Q Technical Reference > Updating and Automation > JavaScript
User Interface > JavaScript Variables > JavaScript Variables Examples Library