Last modified by Aurelie Bertrand on 2020/09/24 14:12

Hide last authors
mrochelle 2.2 1 {{toc/}}
2
3 ----
4
Aurelie Bertrand 10.1 5 You may use javascript functions in calculated measure, such as "if" test, or even "for" loop.
6 You can also use DigDash functions to explore the dimensions
mrochelle 2.2 7
Aurelie Bertrand 10.1 8 = Write in the logs =
mrochelle 4.2 9
Aurelie Bertrand 10.1 10 You may display variables, text, measure, dimension in the ogs using the following code :
mrochelle 2.2 11
Aurelie Bertrand 10.1 12 {{code language="js"}}Packages.com.digdash.utils.MessageStack.getInstance().addText("Je suis dans les logs !!! ");{{/code}}
mrochelle 5.1 13
Aurelie Bertrand 10.1 14 Example :
mrochelle 2.2 15
mrochelle 4.2 16 [[image:1600944409879-673.png]]
mrochelle 2.2 17
mrochelle 4.2 18
Aurelie Bertrand 10.1 19 What appears in the logs :
mrochelle 4.2 20
21 [[image:1600944499398-123.png]]
22
Aurelie Bertrand 10.1 23 WIth this, you can check and vet the different steps when building your calculated measure.
mrochelle 4.2 24
Aurelie Bertrand 10.1 25 = Measure based on filtered dimensions =
mrochelle 1.1 26
Aurelie Bertrand 10.1 27 * Example:
mrochelle 1.1 28
29 {{code language="js"}}
30 for (var i = 0; i < this.selection.dimsToFilter.length; i++)
31 {
32 var dimId = this.selection.dimsToFilter[i].dim.id;
Aurelie Bertrand 10.1 33 Packages.com.digdash.utils.MessageStack.getInstance().addText("Dimension Name: " + dimId);
mrochelle 1.1 34 }
35 {{/code}}
36
Aurelie Bertrand 10.1 37 * Result:
38 Dimension Name: Date
39 Dimension Name: Product
40 Dimension Name: Regions
mrochelle 1.1 41
42 * Usage:
43
Aurelie Bertrand 10.1 44 Can be used if we want to change the rendered values based on the filetered dimensions.
mrochelle 1.1 45
Aurelie Bertrand 10.1 46 = Measure based on explored dimensions =
crandon 7.1 47
Aurelie Bertrand 10.1 48 * Example:
crandon 7.1 49
50 {{code language="js"}}
mrochelle 1.1 51 for (var i = 0; i < this.selection.dimsToExplore.length; i++)
52 {
53 var dimId = this.selection.dimsToExplore[i].dim.id;
Aurelie Bertrand 10.1 54 Packages.com.digdash.utils.MessageStack.getInstance().addText("Dimension Name: " + dimId);
mrochelle 1.1 55 }
56 {{/code}}
57
Aurelie Bertrand 10.1 58 * Result:
mrochelle 1.1 59
Aurelie Bertrand 10.1 60 Dimension Name: Date
61 Dimension Name: Produit
62 Dimension Name: Régions
mrochelle 1.1 63
64 * Usage:
65
Aurelie Bertrand 10.1 66 Can be used if we want to change the rendered values if dimenisions are being explored in the graph
mrochelle 1.1 67
Aurelie Bertrand 10.1 68 = Objet Global =
crandon 7.1 69
Aurelie Bertrand 10.1 70 * Example:
crandon 7.1 71
72 {{code language="js"}}
Aurelie Bertrand 10.1 73 if (!_global.count)
mrochelle 1.1 74 {
Aurelie Bertrand 10.1 75 _global.count = 0;
mrochelle 1.1 76 }
77 else
78 {
Aurelie Bertrand 10.1 79 _global.count = _global.count + 1;
mrochelle 1.1 80 }
Aurelie Bertrand 10.1 81 return _global.count;
mrochelle 1.1 82 {{/code}}
83
Aurelie Bertrand 10.1 84 * Result:
mrochelle 1.1 85
Aurelie Bertrand 10.1 86 1
87 2
88 3
89 … (one for each line)
mrochelle 1.1 90
91 * Usage:
92
Aurelie Bertrand 10.1 93 Allows you to save values in variables during the execution of the calculated measure
mrochelle 1.1 94
95
Aurelie Bertrand 10.1 96 = Double pass =
mrochelle 1.1 97
Aurelie Bertrand 10.1 98 * Example:
99
mrochelle 1.1 100 {{code language="js"}}
Aurelie Bertrand 10.1 101 // needSecondPass : DO NOT REMOVE THIS LINE
102 if (phase == 0)
mrochelle 1.1 103 {
Aurelie Bertrand 10.1 104 // do something
mrochelle 1.1 105 }
106 else
107 {
Aurelie Bertrand 10.1 108 // do something
mrochelle 1.1 109 }
110 {{/code}}
111
Aurelie Bertrand 10.1 112 * Result:
mrochelle 1.1 113
Aurelie Bertrand 10.1 114 Forces the script to run twice on the calculated measure
mrochelle 1.1 115
116 * Usage:
117
Aurelie Bertrand 10.1 118 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>>https://doc.digdash.com/xwiki/wiki/howtos/view/howtos/Studio/Calculate_Rank/]])
mrochelle 1.1 119
Aurelie Bertrand 10.1 120 = Running on any axis =
mrochelle 1.1 121
Aurelie Bertrand 10.1 122 Example :  Explore week number (NUM_WEEK) and dates (DAY_DT), we wish to do a running sum on the CA for each week.
123 ! Beware ! Date must be in ascending ordered in the source
mrochelle 1.1 124
125 {{code language="js"}}
126 // needSecondPass : DO NOT REMOVE THIS LINE
127 var section = NUM_WEEK(dmember) + "";
128 var key = DAY_DT(dmember) + "";
129 var val = CA TTC [N](NO_AGG);
130 if (phase == 0)
131 {
132 function add(accumulator, a)
133 {
134 return accumulator + a;
135 }
136 if (!_global.vals)
137 {
138 _global.vals = {};
139 _global.run = {};
140 }
141 if (!_global.vals[section])
142 {
143 _global.vals[section] = {};
144 _global.run[section] = new Array();
145 _global.run[section].push(0);
146 }
147 if (!_global.vals[section][key])
148 {
149 _global.vals[section][key] = _global.run[section].reduce(add) + val;
150 _global.run[section].push(val);
151 }
152 Packages.com.digdash.utils.MessageStack.getInstance().addText("***PHASE0 _global.vals[" + section + "][" + key + "]: " + _global.vals[section][key] + " val: " + val);
153 }
154 else
155 {
156 Packages.com.digdash.utils.MessageStack.getInstance().addText("***PHASE1 _global.vals[" + section + "][" + key + "]: " + _global.vals[section][key]);
157 return _global.vals[section][key];
158 }
159 {{/code}}