Filtering Columns According to Span Labels

From Q
Jump to navigation Jump to search

When a grid question is crosstabulated with another question, the columns of the grid become spans in the table. In some cases, one may want to filter each span and show the filtered results in the table. One example is when the column spans represent brands, and you wish to filter the table based on the customers of each brand. In the code below, you specify which filters should be associate with each span label, and the filtering is the applied to the table.

This script will cause significance results to be displayed incorrectly. Please read Caveats for Table JavaScript before using this script.

var number_of_spans = 4; // Specify the number of spans to consider
var span_filters = { // Specify which spans correspond to which filter
    'Span1' : 'Filter1',
    'Span2' : 'Filter2',
    'Span3' : 'Filter3',
    'Span4' : 'Filter4'}
// Run through each statistic on the table and obtain filtered statistics
for (var j = 0; j < table.statistics.length; j++) {
    // Get the values of the statistic
    var cur_stat = table.statistics[j]; // Get the name of the current statistic
    var cur_data = table.get(cur_stat); // Get the data from the current statistic
    // Run through the column spans and apply each filter in turn.
    for (var k = 0; k < (number_of_spans); k++) {
        var span_name = table.columnSpans[k].label; // Obtain the name of the current span
        var cols_to_change = table.columnSpans[k].indices;
        var current_filter = span_filters[span_name];
        var filtered_table = calculateTable(table.blue,table.brown,['!UseQFilters',current_filter],'!UseQWeight'); // Calculate the filtered table
        var filtered_data = filtered_table.get(cur_stat); // Get the numbers from the filtered table
        // Run through the columns in the spand and replace the original statistics with the filtered statistics
        for (var m = 0; m < cols_to_change.length; m++) {
            var cur_col = cols_to_change[m];
            for (var row = 0; row < table.numberRows; row++)
                cur_data[row][cur_col] = filtered_data[row][cur_col];
        }
    }
    table.set(cur_stat,cur_data);
}

See also