Constant Sum + Matrix Tables + Data Validation | XM Community
Question

Constant Sum + Matrix Tables + Data Validation

  • 15 April 2024
  • 3 replies
  • 40 views

Badge

Hello! 

 

Question Type: Matrix Table

Matrix Type: Constant Sum

 

I would like to use data validation to ensure that the total sum of column 1 (orange) matches a value submitted earlier in the survey. I also need the total sum of column 2 (green) to match a different value submitted earlier in the survey. The total sum of column 3 (yellow) does not need validation and does not match any information submitted previously. 

 

In all the posts I’ve read, I see people putting the piped text of the previous answer into the ‘must total’ data validation box. However, that doesn’t work in my situation because there are multiple totals. Is there any way to use data validation with multiple totals? 

 

 


3 replies

Userlevel 1
Badge +5

@KCL 

mabey this code will help you
 

Qualtrics.SurveyEngine.addOnload(function() {
    this.questionclick = function(event, element) {
        var orangeTotal = 0;
        var greenTotal = 0;
        
        // Calculate the sum of orange and green columns
        jQuery("#"+this.questionId+" .ConstantSumInput").each(function() {
            var value = parseInt(jQuery(this).val());
            if (jQuery(this).hasClass("Q1_1")) { // Check if it's in orange column
                orangeTotal += value;
            } else if (jQuery(this).hasClass("Q1_2")) { // Check if it's in green column
                greenTotal += value;
            }
        });

        // Retrieve previous totals from embedded data
        var previousTotalOrange = parseInt("${e://Field/previous_total_orange}");
        var previousTotalGreen = parseInt("${e://Field/previous_total_green}");

        // Compare with previous totals
        if (orangeTotal !== previousTotalOrange || greenTotal !== previousTotalGreen) {
            Qualtrics.SurveyEngine.setEmbeddedData('validation_error', 'Total sum does not match the previous value.');
        } else {
            Qualtrics.SurveyEngine.setEmbeddedData('validation_error', '');
        }
    }
});
 

Replace Q1_1 and Q1_2 with the actual column IDs of your Matrix Table question. Also, make sure to adjust the embedded data field names (previous_total_orange and previous_total_green) to match what you've set up in your survey.

This script will compare the sum of each column with the corresponding previous total, and if they don't match, it will set an embedded data field (validation_error) with an error message. You can then use display logic to show an error message to the participant if needed.

Remember to test the survey thoroughly after adding custom JavaScript to ensure it behaves as expected.

Userlevel 4
Badge +7

Hi @KCL ,

You can try with the below approach and code:

 

Create embedded data fields to store the total values for each column. Let's call them TotalColumn1 and TotalColumn2.

When respondents submit the values for each column, calculate and store the total for each column using JavaScript.

///Code////

Qualtrics.SurveyEngine.addOnload(function() {
    // Retrieve stored total values
    var totalColumn1 = "${e://Field/TotalColumn1}";
    var totalColumn2 = "${e://Field/TotalColumn2}";
    
    // Calculate total values entered by the respondent
    var enteredTotal1 = 0;
    var enteredTotal2 = 0;
    
    // Change "Q1", "Q2", and "Q3" to the respective question IDs for each column
    var q1Inputs = document.querySelectorAll('#Q1 input[type="text"]');
    var q2Inputs = document.querySelectorAll('#Q2 input[type="text"]');
    
    for (var i = 0; i < q1Inputs.length; i++) {
        enteredTotal1 += parseFloat(q1Inputs[i].value) || 0;
    }
    
    for (var i = 0; i < q2Inputs.length; i++) {
        enteredTotal2 += parseFloat(q2Inputs[i].value) || 0;
    }
    
    // Validate totals
    if (enteredTotal1 !== parseFloat(totalColumn1) || enteredTotal2 !== parseFloat(totalColumn2)) {
        this.disableNextButton();
        alert("The total sum of Column 1 must match " + totalColumn1 + " and the total sum of Column 2 must match " + totalColumn2 + ".");
    } else {
        this.enableNextButton();
    }
});

 

Thank You!

Userlevel 4
Badge +7

This may help you 

 

Leave a Reply