Constant sum matrix sum + total sum from both rows and columns | XM Community
Skip to main content
Hi.



I need to validate a constant sum matrix by adding rows AND columns.

!



For example, I would like to ask participants to distribute 8 people in the matrix. It could be all 8 males or 8 female or 8 Whites or 8 Asians.

I've been trying to come up with code for more than 4 hours (based on this answer from @GrayMatterThinking), but still couldn't find the solution.



Thank you in advance.
The easiest way to do this is to create a text entry question immediately after your constant sum and hide it with JS. Have JS sum the inputs of the constant sum and update the value of the hidden text question. Use the hidden text question in your constant sum custom validation.
Hi @ahra1120 ,



What TomG says is correct, but if JS is a little too challenging to incorporate in your survey (as it can be for me), there are two other solutions using Qualtrics built-in validation that you might consider.



I don't know if the 8 that you're using to validate the sum of fields from the matrix is fixed, or if it can vary based on a previous input but you just used it for the purpose of an example. Here are solutions for either scenario.



If it is the former (8 is fixed for every respondent), then you could recreate the question in a single column Constant Sum question (Vertical Text with Total), with categories White Male, White Female, Asian Male, Asian Female, etc., and set the "Must Total" value to equal 8.



If it is the latter (that is, the validation sum does vary based on a previous input from a text entry question), then you could leave the question as is in the Matrix format and use Custom Validation. You would take the initial text entry question and in the highlighted field set it to equal a formula like the one below (replacing "##" with the corresponding QID number from your specific Matrix question).



!



$e{ q://QID##/ChoiceNumericEntryValue/1/1 + q://QID##/ChoiceNumericEntryValue/1/2 + q://QID##/ChoiceNumericEntryValue/2/1 + q://QID##/ChoiceNumericEntryValue/2/2 + q://QID##/ChoiceNumericEntryValue/3/1 + q://QID##/ChoiceNumericEntryValue/3/2 + q://QID##/ChoiceNumericEntryValue/4/1 + q://QID##/ChoiceNumericEntryValue/4/2 }



Hope these help.

Hello Tom, do you have the Java Script for this particular action?


The easiest way to do this is to create a text entry question immediately after your constant sum and hide it with JS. Have JS sum the inputs of the constant sum and update the value of the hidden text question. Use the hidden text question in your constant sum custom validation.

@TomG I’m doing something similar here except that I actually want to display the total and make it update each time the row total changes in my Matrix Question. Right now, the function only works when I leave the page and return to it. 

 

DO you have any suggestion on how I might fix this, so it update while on the page?


The easiest way to do this is to create a text entry question immediately after your constant sum and hide it with JS. Have JS sum the inputs of the constant sum and update the value of the hidden text question. Use the hidden text question in your constant sum custom validation.

@TomG I’m doing something similar here except that I actually want to display the total and make it update each time the row total changes in my Matrix Question. Right now, the function only works when I leave the page and return to it. 

 

DO you have any suggestion on how I might fix this, so it update while on the page?

Use a keyup or input event handler.


Hi @MatthewM  I am trying to sum across all cells (Race x Ethnicity), and have it total a prior question.  In your non-JS solution, you create a custom validation.  I tried to replicate.  I can select in the first box the question of interest, but the second box forces me to select one of the cells (not shown, when I scroll down are the additional race*eth options, never question), not offering the question as a possibility, like you show in your example.   Any guidance?  Thank you!

 


@Joba - @KPKat’s post reminded me that I have a function that displays and calculates both row, column, and grand totals: csmAddTotals.


Hi @KPKat ,

Basically you need to switch the questions around in your custom validation.

The questions are not shown in previous example so it may not have been completely clear, but in my case Q10 is a text entry question and Q11 is a matrix. Although the custom validation that I shared the example of was set up inside Q11, you’ll see I selected Q10 in the menu. The formula I shared represents each cell from Q11 (the matrix question).

In your survey, you’ll need to select the prior question in the Custom Validation menu (not the matrix question), and write in the formula with the QIDs from each applicable cell in your matrix question.


@Joba - @KPKat’s post reminded me that I have a function that displays and calculates both row, column, and grand totals: csmAddTotals.

I added optional validation to csmAddTotals - when it adds to the specified grand total, the next button is enabled, otherwise it is disabled. Demo and details at the link provided.


Leave a Reply