Automatically calculate number from previous questions | XM Community
Skip to main content

Hi, I have a series of questions that are text entry forms (which I have set up to be validated as numbers). After these questions I added a follow-up that I want to be automatically calculated based on those previous responses; however, despite being validated as numbers, I can't seem to create a math operation because those questions are registering as string fields rather than numerical. Is there a way to re-code this in javascript? I'm not very familiar with that coding language.

Has anyone done this before? Any help is appreciated!

@hipsen What is your equation looks like? The syntax might be a bit tricky
Meanwhile, check out this troubleshooting: https://www.qualtrics.com/support/survey-platform/survey-module/editing-questions/piped-text/math-operations/#TroubleshootingMathOperations


Thanks @dxconnamnguyen ! I don’t have a specific equation yet, but survey respondents will be asked how much compost, recycling, and waste they’ve generated, followed by a diversion rate question. Ideally the diversion rate would be automatically calculated for them so they don’t have to do the math (i.e. the form would auto-populate the diversion rate by subtracting the values entered in the compost + recycling from the sum of all three previous questions). I can get it to show the formula via piped text, but I want it to show the numerical result.


 (i.e. the form would auto-populate the diversion rate by subtracting the values entered in the compost + recycling from the sum of all three previous questions). I can get it to show the formula via piped text, but I want it to show the numerical result.

Which mean your formula/equation/math operation might look wrong. Can’t help without the detail


please refer the support page that talks about the math operation here.

https://www.qualtrics.com/support/survey-platform/survey-module/editing-questions/piped-text/math-operations/

hope this will help you.


@dxconnamnguyen here’s what it looks like:

The first 3 questions
The piped text that shows a formula but not a calculation

 

I don’t have anything written in Javascript yet.


@hipsen - The reason your math operation isn’t working if because you are doing it on the same page as the input values it is based off. You would need to add a page break between 3.5 and 3.7 for the math operation in 3.7 to work.

Otherwise, you would need to do the calculation dynamically with JavaScript.


@TomG there is a page break. The above is two separate screen shots from two different pages in the survey. I was hoping someone had scripted this because I’m not familiar with JavaScript.


@TomG there is a page break. The above is two separate screen shots from two different pages in the survey. I was hoping someone had scripted this because I’m not familiar with JavaScript.

Then you don’t need JS.  Your math expression should be:

$e{ q://QID1/ChoiceTextEntryValue - ( q://QID2/ChoiceTextEntryValue + q://QID3/ChoiceTextEntryValue ) }

Where QID1=3.3, QID2=3.4, QID3=3.5.  You would need to change the QIDs to match the actual QIDs of your questions.


@TomG That doesn’t work because you can’t perform math operations on text fields, even though I’ve validated them as numerical answers. Below is what I get when I try to do the math operations:

 


That doesn’t work because you can’t perform math operations on text fields, even though I’ve validated them as numerical answers. Below is what I get when I try to do the math operations:

 

Strange. I’m not sure that has always been the case. Anyway, you can add this JS to one of the questions on the first page, then pipe “calc” on the next page:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var inputs = jQuery(".InputText");
var calc = Number(inputs.eq(0).val()) - (Number(inputs.eq(1).val()) + Number(inputs.eq(2).val()));
Qualtrics.SurveyEngine.setEmbeddedData("calc", calc);
});

 


@TomG That doesn’t work because you can’t perform math operations on text fields, even though I’ve validated them as numerical answers. Below is what I get when I try to do the math operations:

 

This doesn’t look like a right syntax.


@TomG I’m not seeing anything show up when I add it to the first question on the first page. And “calc” isn’t an option for piped text embedded data fields when I add the script.


@TomG I’m not seeing anything show up when I add it to the first question on the first page. And “calc” isn’t an option for piped text embedded data fields when I add the script.

The field ‘calc’ won’t show up as an embedded data field you can choose from the drop down unless you add it to the survey flow.  That is also required if you want it saved in your response data.  Otherwise, it is just a temporary (while the survey is active) embedded data field. Either way, you would pipe it on the second page as ${e://Field/calc}.


I’m still getting nothing showing up. Was I supposed to change the script you sent somehow?


I’m still getting nothing showing up. Was I supposed to change the script you sent somehow?

No, as long as you aren’t using Simple layout, it should work as is. Demo


I’m using the simple layout. If I change that will it fix the issue?


I’m using the simple layout. If I change that will it fix the issue?

Yes


That worked!! Thank you so much! 😁


Leave a Reply