3 Replies Latest reply: Oct 2, 2014 3:37 AM by Massimo Grossi RSS

    Calculate average on subset of records.

    Viktor Forsman

      Hi,

       

      I'm new to Qlik and its' scripting language and I'm stuck on a seamingly simple task: To create an average, but only include the lowest 5% of the values.

      The following is a javascript implementation I'm currently using:


      // Sort the values

      data = _.sortBy(data, alertness);

       

      // Pick out the worst 5 percent of alertness values into new array

      var worst5percent = _.first(data, Math.round(data.length * 0.05));

       

      // Reduce the sum and divide by array length.

      var pa5 = _.reduce(_.pluck(worst5percent, alertness), function (result, num) {

              return result + num;

          }, 0) / worst5percent.length;

       

      Basically I need to sort, top and average, but I cannot seem to find a sort method. What can I do to achieve this result?

       

      /Viktor