Issue with calculating respondent-level average scores from Matrix Table items. | Experience Community
Skip to main content
Question

Issue with calculating respondent-level average scores from Matrix Table items.

  • June 3, 2026
  • 0 replies
  • 5 views

Hi everyone,

 

I have the following survey design: 

- There are 6 possible scenarios.
- Each respondent sees only 2 out of 6 scenarios.
- For each shown scenario, respondents answer Likert/matrix items:
- Logica: I1
- Empathische kwaliteit: I2, I3, I4
- Intentie/behoefte: I5, I6, I7
- Haalbaarheid: I8
- I want to store the selected 1–7 values in Embedded Data after each scenario block and later calculate average scores only over the shown/answered scenarios.

Embedded Data fields include:
S1_Logica_I1, S2_Logica_I1, S3_Logica_I1, S4_Logica_I1, S5_Logica_I1, S6_Logica_I1
S1_PercEmpathie_I2, S1_PercEmpathie_I3, S1_PercEmpathie_I4, etc. for S1–S6
S1_IntentieTotEngag_I5, S1_IntentieTotEngag_I6, S1_IntentieTotEngag_I7, etc. for S1–S6
S1_Haalbaarheid_I8, etc. for S1–S6. I've checked the piped text, that works fine. 

 

In order to calculate the scores I've made JS code. However, it does not function as expected and my output on the anonymous link doesn't show that placeholder HTML is replaced with the output from InnerHTML section in JS and even when it did, there's still somehow problems with calculating the averages in JS. Hereby the code:                                                                                                                                 

Qualtrics.SurveyEngine.addOnLoad(function () {
    var qid = this.questionId;

    try {
        // Schone functie om de Embedded Data op te halen
        function getED(name) {
            var v = Qualtrics.SurveyEngine.getEmbeddedData(name);
            if (v === null || v === undefined) return "";
            v = String(v).trim();
            if (v === "" || v === "NA" || v === "undefined" || v === "null") return "";
            return v.replace(",", "."); // Vervang eventuele komma's door punten voor de berekening
        }

        // Functie om het gemiddelde te berekenen over de wél ingevulde scenario's
        function avg(fields) {
            var scores = [];
            for (var i = 0; i < fields.length; i++) {
                var v = getED(fields[i]);
                if (v !== "") {
                    var n = Number(v);
                    if (!isNaN(n) && n >= 1 && n <= 7) scores.push(n);
                }
            }
            if (!scores.length) return null;
            var s = 0;
            for (var j = 0; j < scores.length; j++) s += scores[j];
            return s / scores.length;
        }

        // Formatteer naar twee decimalen met een komma (bijv. 4,50)
        function fmt(s) {
            return s === null ? "Niet beschikbaar" : s.toFixed(2).replace(".", ",");
        }

        // Jouw oorspronkelijke variabelenamen
        var logica       = ["S1_Logica_I1","S2_Logica_I1","S3_Logica_I1","S4_Logica_I1","S5_Logica_I1","S6_Logica_I1"];
        var empathie     = ["S1_PercEmpathie_I2","S1_PercEmpathie_I3","S1_PercEmpathie_I4","S2_PercEmpathie_I2","S2_PercEmpathie_I3","S2_PercEmpathie_I4","S3_PercEmpathie_I2","S3_PercEmpathie_I3","S3_PercEmpathie_I4","S4_PercEmpathie_I2","S4_PercEmpathie_I3","S4_PercEmpathie_I4","S5_PercEmpathie_I2","S5_PercEmpathie_I3","S5_PercEmpathie_I4","S6_PercEmpathie_I2","S6_PercEmpathie_I3","S6_PercEmpathie_I4"];
        var intentie     = ["S1_IntentieTotEngag_I5","S1_IntentieTotEngag_I6","S1_IntentieTotEngag_I7","S2_IntentieTotEngag_I5","S2_IntentieTotEngag_I6","S2_IntentieTotEngag_I7","S3_IntentieTotEngag_I5","S3_IntentieTotEngag_I6","S3_IntentieTotEngag_I7","S4_IntentieTotEngag_I5","S4_IntentieTotEngag_I6","S4_IntentieTotEngag_I7","S5_IntentieTotEngag_I5","S5_IntentieTotEngag_I6","S5_IntentieTotEngag_I7","S6_IntentieTotEngag_I5","S6_IntentieTotEngag_I6","S6_IntentieTotEngag_I7"];
        var haalbaarheid = ["S1_Haalbaarheid_I8","S2_Haalbaarheid_I8","S3_Haalbaarheid_I8","S4_Haalbaarheid_I8","S5_Haalbaarheid_I8","S6_Haalbaarheid_I8"];

        // We zoeken de placeholder binnen de container van deze specifieke vraag
        var container = this.getQuestionTextContainer();
        var outputDiv = container ? container.querySelector("#gemiddeldeScoresOutput") : null;

        if (outputDiv) {
            outputDiv.innerHTML =
                "<p style='margin: 8px 0;'>Logica AI-advies: <strong>" + fmt(avg(logica)) + "</strong></p>" +
                "<p style='margin: 8px 0;'>Empathische kwaliteit AI-advies: <strong>" + fmt(avg(empathie)) + "</strong></p>" +
                "<p style='margin: 8px 0;'>Behoefte AI-advies te gebruiken: <strong>" + fmt(avg(intentie)) + "</strong></p>" +
                "<p style='margin: 8px 0;'>Haalbaarheid AI-advies: <strong>" + fmt(avg(haalbaarheid)) + "</strong></p>" +
                "<p style='font-size:0.9em; margin-top:16px; color:#555;'>De scores lopen van 1 tot en met 7.</p>";
        }

    } catch (e) {
        console.error("Fout in script:", e.message);
    }
});

 

Feedback is appreciated. I'm not that experienced with JS. Thank you in advance!