Hoping someone might be able to help me with some survey flow calculations. I’ve tried numerous iterations and nothing seems to work!
I have a set of 7 statements in a grid with 5 options each. These 5 options are recoded as:
1 = 0
2 = 25
3 = 50
4 = 75
5 = 100
Respondents can elect to answer all or only a portion of the statements.
A total score needs to be calculated which is the sum of all the recoded values, divided by how many statements they answered.
e.g. Respondent answers statements 1, 3, 4, 7 with the recodes
Statement 1 = 25
Statement 3 =100
Statement 4 = 0
Statement 7 = 75
(summing to 200).
The respondent answered 4 out of the possible 7 statements, therefore the calculated score is 200 / 4 = 50.
I’ve followed the advice on arithmetic operations on the Qualtrics website and tried multiple different methods of creating embedded data fields. The Branch IF Field = Empty in the survey flow doesn’t appear to work - if it did it would solve all my problems!
Hoping someone might have a good idea I could try.
Best answer by Tom_1842
To put this in place with JavaScript, first create an Embedded Data variable at the top of the Survey Flow called 'average'. Then, add the below JavaScript to the OnReady section of any question that comes on a page after the Matrix question, updating the QID to match the Matrix question:
var choices = "${q://QID1/ChoiceGroup/SelectedChoices}"; var choicesnumber = choices.split(",").length; console.log(choicesnumber);
var recode1 = "${q://QID1/SelectedAnswerRecode/1}"; var recode2 = "${q://QID1/SelectedAnswerRecode/2}"; var recode3 = "${q://QID1/SelectedAnswerRecode/3}"; var recode4 = "${q://QID1/SelectedAnswerRecode/4}"; var recode5 = "${q://QID1/SelectedAnswerRecode/5}"; var recode6 = "${q://QID1/SelectedAnswerRecode/6}"; var recode7 = "${q://QID1/SelectedAnswerRecode/7}";
Hi Phillip, I have “similar” calculations in my survey. One way to realise those “complex” calculations is to use JavaScript.
The JavaScript has to sit in one of the questions AFTER your last statement block. This makes sure that you can access all statement results (e.g. their recoded values, as in my example) via variables.
Here’s a snippet of how my code looks like. I guess you should be able to adapt it to your needs, even without big coding skills.
Qualtrics.SurveyEngine.addOnReady(function() { /* Defining a function that does all the required calculation */ const score = function(per, pre) { var result = "";
per = Number(per); pre = Number(pre);
if (per != 99 && pre != 99) { result = (25 * (per + pre)) - 100; } else if (per == 99 && pre == 5) { result = (25 * (5 + 5)) - 100; } else if (per == 99 && pre < 5) { result = (25 * ((pre / 0.9) + pre)) - 100; } else if (per == 1 && pre == 99) { result = (25 * (1 + 1)) - 100; } else if (per > 1 && per != 99 && pre == 99) { result = (25 * (per + (per * 0.9))) - 100; }
return result; };
/* Getting the answers of previous questions */ var perf = "${q://QID7/SelectedChoicesRecode}"; var pref = "${q://QID8/SelectedChoicesRecode}";
/* Calling the above specified function with all calculations */ var score_Comp = score(perf, pref);
/* Writing result out to Embedded data */ Qualtrics.SurveyEngine.setEmbeddedData( 'Score_Company', score_Comp );
To put this in place with JavaScript, first create an Embedded Data variable at the top of the Survey Flow called 'average'. Then, add the below JavaScript to the OnReady section of any question that comes on a page after the Matrix question, updating the QID to match the Matrix question:
var choices = "${q://QID1/ChoiceGroup/SelectedChoices}"; var choicesnumber = choices.split(",").length; console.log(choicesnumber);
var recode1 = "${q://QID1/SelectedAnswerRecode/1}"; var recode2 = "${q://QID1/SelectedAnswerRecode/2}"; var recode3 = "${q://QID1/SelectedAnswerRecode/3}"; var recode4 = "${q://QID1/SelectedAnswerRecode/4}"; var recode5 = "${q://QID1/SelectedAnswerRecode/5}"; var recode6 = "${q://QID1/SelectedAnswerRecode/6}"; var recode7 = "${q://QID1/SelectedAnswerRecode/7}";
To put this in place with JavaScript, first create an Embedded Data variable at the top of the Survey Flow called 'average'. Then, add the below JavaScript to the OnReady section of any question that comes on a page after the Matrix question, updating the QID to match the Matrix question:
var choices = "${q://QID1/ChoiceGroup/SelectedChoices}"; var choicesnumber = choices.split(",").length; console.log(choicesnumber);
var recode1 = "${q://QID1/SelectedAnswerRecode/1}"; var recode2 = "${q://QID1/SelectedAnswerRecode/2}"; var recode3 = "${q://QID1/SelectedAnswerRecode/3}"; var recode4 = "${q://QID1/SelectedAnswerRecode/4}"; var recode5 = "${q://QID1/SelectedAnswerRecode/5}"; var recode6 = "${q://QID1/SelectedAnswerRecode/6}"; var recode7 = "${q://QID1/SelectedAnswerRecode/7}";