When creating new items, you can define variables in the expression editor using a conditional definition, i.e., an IF statement, or by using a count function, which allows you to count responses. This can help users dig deeper into the data, bringing a new level of insight to an analysis.
In this article
1. Conditions
IF Statements
IF statements are conditional statements that perform an action when a condition is met. An IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
IF statements are typically used to create new measures or weights with a different value depending on specific conditions.
IF Statement Syntax
| Syntax for IF statement | if (Logic_Test, Value_if_True, Value_if_False) |
| Syntax for nested IF statement | if (Logic_Test, Value_if_True, if (Logic_Test, Value_if_True, Value_if_False)) |
Examples
Here are two examples using IF statements to create a new items. In the first example, a new weight is created and the second shows a new measure.
a. Weight
Let's assume you want to weight Males and Females to a 50/50 distribution. Across all sources, the Male/Female distribution is 45/55. To change this to 50/50, we need to assign all males a weight of 1.111 (50/45), and all females need a weight of 0.909 (50/55). For any missing cases (i.e., No answer for Male or Female/ Value if False), we are assigning a NULL.
- Select a location in the tree
- Click new weight
- Enter the conditions for the IF statement
- Apply expression
b. Measure
IF statements are used to populate a new measure with a different value depending on specific conditions. In this example, we will use the Recommend rating scale to create an Net Promoter Score (NPS). We can run this data from the NPS axis, but it's useful to have the average available separately in a measure so it can be used more widely.
- Select a location in the tree
- Click new measure
- Enter the conditions for the IF statement
- Apply expression
2. Functions
Count Responses
The count function can be used to define a new element or a new measure by counting the number of responses given to the items listed in a comma-separated list. The count function creates a numeric value from an existing axis, element, or measure.
The numeric value generated from a count function can either be used in a new measure or be compared to another value to create a True/False result for use in a new element.
- Axis item(s) in the Count function count the number of elements in those axes that each respondent has a response in.
- Individual Axis.Element item(s) in the Count function will only ever generate a value of 0 or 1, as each respondent can only have a response to a specific element.
- Each Measure item in the count function will only generate a value of 0 or 1, as each respondent can only have one numerical response to a measure.
- To sum multiple items together, list them within the Count function using a comma separator.
- Boolean operators can specify relationships that must exist for a count to be included.
Count Function Syntax
a) Counting all responses in a standard axis.
With this syntax, the values count returns are the number of elements each respondent responded to. Note, that only multiple-response variables will ever give a count greater than 1.
- Element: count (AxisName1) >= 1
- Measure: count (AxisName1)
b) Count function with Boolean logic can be used to count whether a respondent mentioned at least one response to a specific set of items.
Using count ( _ or _ or _ )
With this syntax, the values count returns are 0 and 1. The maximum count the expression returns is 1.
- Element: count (AxisName1.(ElementName1 or ElementName2 or Element 3)) = 1
- Measure: count (AxisName1.(ElementName1 or ElementName2 or Element 3))
Using count ( _ , _ , _ )
With this syntax, you can sum together counts from a specific set of items. It evaluates the number of items a respondent has answered and adds the count for each. The maximum count the expression returns is the max number of possible responses.
- Element: count (AxisName1.ElementName1 , AxisName1.ElementName2 , AxisName1.ElementName3)) = 3
- Measure: count (AxisName1.ElementName1 , AxisName1.ElementName2 , AxisName1.ElementName3)
Examples
Let's look at some examples. Let's assume you want to find out the number of types of technology used during a trip.
a. Element
In this case, you want to know how many respondents used one type, how many used two types, etc.
To avoid including None of these element in the count, the best syntax for this scenario is:
count ( _ , _ , _ ).
- Select a location in the tree
- Click new axis, type the axis name and press enter
- Add the element name
- Enter the count syntax in the expression editor
- Before saving, copy the syntax to use in the next element
- Save and add NEW ELEMENT
- Paste the syntax and adjust the value
- Once all element are added, Apply expression
b. Measure
To exclude the responses for the element "None of these" the best syntax to use is count ( _ , _ , _ ). This will just consider the elements you want to include in the calculation.
Using the syntax count (AxisName1) would consider all elements.
- Select a location in the tree
- Click new measure
- Enter the count syntax into the expression editor
- Apply expression
Where to from here?
Learn more about Constructions: