Advanced Calculated Measure Functions

Last modified by mrochelle on 2020/09/23 11:19

Measure based on filtered dimensions

  • Example:
for (var i = 0; i < this.selection.dimsToFilter.length; i++)
{
      var dimId = this.selection.dimsToFilter[i].dim.id;
      Packages.com.digdash.utils.MessageStack.getInstance().addText("Dimension Name: " + dimId);
}
  • Result:
    Dimension Name: Date
    Dimension Name: Product
    Dimension Name: Regions
  • Usage:

Can be used if we want to change the rendered values based on the filetered dimensions

Measure based on explored dimensions

  • Example:
for (var i = 0; i < this.selection.dimsToExplore.length; i++)
{
      var dimId = this.selection.dimsToExplore[i].dim.id;
      Packages.com.digdash.utils.MessageStack.getInstance().addText("Dimension Name: " + dimId);
}
  • Result:

Dimension Name: Date
Dimension Name: Produit
Dimension Name: Régions

  • Usage:

Can be used if we want to change the rendered values if dimenisions are being explored in the graph

Objet Global

  • Example:
if (!_global.count)
{
 _global.count = 0;
}
else
{
 _global.count = _global.count + 1;
}
return _global.count;
  • Result:

1
2
3
… (one for each line)

  • Usage:

Allows you to save values in variables during the execution of the calculated measure

Double pass

  • Example:
// needSecondPass : DO NOT REMOVE THIS LINE
if (phase == 0)
{
// do something
}
else
{
// do something
}
  • Result:

Forces the script to run twice on the calculated measure

  • Usage:

On the first run, you can save values in global variable and retrieve the them in the second run to compare them/manipulated them (example with rank)

Running on any axis

Example :  Explore week number (NUM_WEEK) and dates (DAY_DT), we wish to do a running sum on the CA for each week.
! Beware ! Date must be in ascending ordered in the source

// needSecondPass : DO NOT REMOVE THIS LINE
var section = NUM_WEEK(dmember) + "";
var key = DAY_DT(dmember) + "";
var val = CA TTC [N](NO_AGG);
if (phase == 0)
{
function add(accumulator, a)
 {
     return accumulator + a;
 }
if (!_global.vals)
 {
  _global.vals = {};
  _global.run = {};
 }
if (!_global.vals[section])
 {
  _global.vals[section] = {};
  _global.run[section] = new Array();
  _global.run[section].push(0);
 }
if (!_global.vals[section][key])
 {
  _global.vals[section][key] = _global.run[section].reduce(add) + val;
  _global.run[section].push(val);
 }
Packages.com.digdash.utils.MessageStack.getInstance().addText("***PHASE0 _global.vals[" + section + "][" + key + "]: " + _global.vals[section][key] + " val: " + val);
}
else
{
Packages.com.digdash.utils.MessageStack.getInstance().addText("***PHASE1 _global.vals[" + section + "][" + key + "]: " + _global.vals[section][key]);
return _global.vals[section][key];
}