Compute the Difference Between a Pair of Rows

From Q
Jump to navigation Jump to search

This script calculates the difference between two specified rows and displays it beneath the second row in the table. Two more straightforward approaches to achieving the same outcome are:

You can modify the top of the script to specify which statistic to use, which rows to use, and the label of the new row in the table.

// Table JavaScript for computing the difference between a pair of rows
// Difference is calculated as <First Row> - <Second Row>

var target_statisic = 'Column %'; // Specify the statistic to use in the calculation

var first_row_index = 0; //Specify the position of the first row (indices begin at 0)
var second_row_index = 1; // Specify the position of the second row (indices begin at 0)
var new_row_label = 'Delta'; // Specify the label of the new row (containing the differences)

var new_row_index = second_row_index+1; // Index of the new row

table.insertRowAfter(second_row_index,new_row_label); // Add a new row to store the differences
if (typeof right_table !== 'undefined') {
    if (right_table.columnLabels == null)
        right_table.insertRowAfter(second_row_index, new_row_label);
    else
        right_table.insertColumnAfter(second_row_index, new_row_label);
}

var stats = table.get(target_statisic); // Obtain the array of statistics

// Calcualte the differences in the new row
for (var j = 0; j < table.numberColumns; j++) {
	stats[new_row_index][j] = stats[first_row_index][j] - stats[second_row_index][j];
}

// Assign the new statistics back to the table
table.set(target_statisic,stats);

Note that the code above also inserts a new row into right_table, which correspond to the table for the Statistics - Right respectively. This table must be addressed separately - it does not automatically update when the main table is changed. The function insertRowAfterComplete() can be used to address the extra tables automatically whenever they are shown on the table.

See also

Further reading: NPS Analysis Tool