Creating a Counter Variable
This article will show you how to create a basic Counter Variable
Sometimes you need a way to count how many items have something in common. You can create a counter variable to store this data and then set it using logic. Then you can use the counter variable within your programming for skip patterns, terminations or for any other logical operation. The instructions below will show you how to set something like this up.
Step 1: Create Your Question
- Add your question type which is needed to have the counter based off, whatever that is. It could be a checkbox, numeric fields, or a grid (radio, checkbox or numeric).
- See article: Add a Question
Step 2: Create Your Counter Variable
- Add a 'Single Custom Variable'
- See article: Add a Variable
- Do not add a response list
Step 3: Add Logic to Set Your Counter
- Add a 'Logic Block Page' after the question which your counter will be based on.
- See article: Add a Logic Block Page
- Add a 'Logic Block' to the page
- See article: Adding a Logic Block
- On this block you will want to always set the counter variable to zero. This will help you when testing, especially if you go back and try to re-answer the question to confirm that its working as intended. This way it will reset itself and not continue to increment. It will look like this in the 'Build & Edit Tool':
- Click on '+ Block'
- Run Actions: Leave as 'Always'
- In the 'Actions' drop-down, select 'Set Variable' and search for the 'Custom Single Variable' you created
- In the 'To' field type in the number zero ("0")
- At the bottom of this same 'Logic Block Page', click on '+ Loop'. This will allow you to loop through the sub-questions (response items) and perform logical actions.
- In the 'Loop' drop-down leave this as 'Always'
- In the 'For each response in' field, select the question you are basing the counter on. In my example I used "Q1"
- Within the 'Loop Logic' click on the '+ Block to Loop'. This is where you will customize the counter using logic based on the answers to the question.
- In the 'Run Actions' field, select 'Only If' and enter a formula which makes sense for your scenario and the question type. In the example below we are saying, only if the corresponding items at "Q1" are greater than or equal to one... then do something
- In the 'Add Action' drop-down select 'Set Variable' and add your counter variable since this is what you are trying to set
- In the 'To' field enter your counter variable name plus one. So when this executes then each time (in this example) an item in "Q1" is greater than or equal to one then set the counter to itself plus one. When this is done, you will have a variable which holds the number of items which fit your criteria.
Step 4: Test
- Click on the 'Preview & Test' link to review and make sure the setup is working as needed
Step 5: Review (optional)
- This step is not required but always highly recommended.
- Click on the 'Review Outline' and check the setup for your counter. It will look something like this: