[Working with scripts]: Count Unique

Q: We would like to know how we can perform unique count operation on a data field.

E.g. A dataset with a list of vehicle numbers. (V1, V2, V3, V1).

Normal count will return “4”.

Unique count will return only “3”.

A: you can use Javascript to perform this task.

We have attached the sample for your reference. In our example includes 3 steps:

1/ Having a global array, named “dataArray” to store all the fruit name. Declare dataArray[] in “Function Definitions”:

     var dataArray = new Array();

1

push data into dataArray:

    dataArray.push(Fruit);

2

2/ Create a function called, “countDis” in Function Definitions, which take an Array as its parameter:

function countDis(array) {
 array.sort(function(a,b){
  if(isNaN(a) && isNaN(b)) { return a > b ? 1 : (a < b ? -1 : 0); }
  if(isNaN(a)) { return 1; }
  if(isNaN(b)) { return -1; }
  return a-b;
 }
};

var newArray = [];
var co = 0;
var len = array.length;
for(var i=0; i<len; i++){
  if(i === 0 || array[i] != array[i-1]){
   newArray.push(array[i]);
   co++;
  }
 }
return co;
}

3

3/ Use countDis(dataArray) at Section Footer:

countDis(dataArray);

*** Another way to do this with less scripting is to use Transforms.

“CountUnique.ds” is the sample that demonstrates.

This doesn’t give one number, it lists the companies and each has a Seq column. The biggest Seq value is the number of unique entries.

This is useful if you want to display the unique list as well as the count.

If you just want to get the count out, then after the transform you would use the Data or DataCache object functionality (Table 7.7, 7.8 of the Elixir Report Designer Manual, page 107) getMax function to get the largest Seq value, which is the unique count. This should just be one line of JavaScript, instead of many.

Please rename the following attachment from .txt to .rml.
Count Unique.txt
Please rename the following attachment from .txt to .ds.
CountUnique(ds).txt
FruitSales(ds).txt