Calculate Index for Grid Questions

From Q
Jump to navigation Jump to search

This example may be used to compute an index for grid tables (Pick One - Multi and Pick Any - Grid) in the Blue drop-down menu when comparing to the last column which is generally the NET. As the rule borrows statistics from the table (by default it takes over the Missing n statistic), you should rename this statistic by right-clicking your table, selecting Table Options, and changing the name of Missing n to Index in the Override Text column of the Output Text column.

To use this snippet:

  1. Select your table.
  2. Select Automate > Custom Rule.
  3. Paste in the code from below.
  4. (Optional) change the statistic used in statistic_to_replace.
  5. Click the 'Play' icon and close.
  6. Right-click on your table and select Statistics - Cells > Missing n.
  7. Right-click your table, select Table Options, click Output Text, and change the name of Missing n to Index in the column of the Override Text column.
// Choose statistic to replace with calculated Index
var statistic_to_replace = "Missing n";

var idx = table.get(statistic_to_replace);
var pct = table.get("%");

// Choose last column to compare against, i.e. NET
var idx_col = table.numberColumns - 1

// Loop through each column and row
for (col = 0; col < table.numberColumns; col++)
{
    for (row = 0; row < table.numberRows; row++)
    {
        idx[row][col] = (pct[row][col] / pct[row][idx_col]) * 100
    }    
}

// Replace with Index value
table.set(statistic_to_replace, idx);

form.setSummary("Calculate index against last column");

See also