Wiki source code of Fonctions Mesure calculée avancés
Last modified by Aurelie Bertrand on 2020/09/24 14:12
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{toc/}} | ||
2 | |||
3 | ---- | ||
4 | |||
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 | ||
7 | |||
8 | = Write in the logs = | ||
9 | |||
10 | You may display variables, text, measure, dimension in the ogs using the following code : | ||
11 | |||
12 | {{code language="js"}}Packages.com.digdash.utils.MessageStack.getInstance().addText("Je suis dans les logs !!! ");{{/code}} | ||
13 | |||
14 | Example : | ||
15 | |||
16 | [[image:1600944409879-673.png]] | ||
17 | |||
18 | |||
19 | What appears in the logs : | ||
20 | |||
21 | [[image:1600944499398-123.png]] | ||
22 | |||
23 | WIth this, you can check and vet the different steps when building your calculated measure. | ||
24 | |||
25 | = Measure based on filtered dimensions = | ||
26 | |||
27 | * Example: | ||
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; | ||
33 | Packages.com.digdash.utils.MessageStack.getInstance().addText("Dimension Name: " + dimId); | ||
34 | } | ||
35 | {{/code}} | ||
36 | |||
37 | * Result: | ||
38 | Dimension Name: Date | ||
39 | Dimension Name: Product | ||
40 | Dimension Name: Regions | ||
41 | |||
42 | * Usage: | ||
43 | |||
44 | Can be used if we want to change the rendered values based on the filetered dimensions. | ||
45 | |||
46 | = Measure based on explored dimensions = | ||
47 | |||
48 | * Example: | ||
49 | |||
50 | {{code language="js"}} | ||
51 | for (var i = 0; i < this.selection.dimsToExplore.length; i++) | ||
52 | { | ||
53 | var dimId = this.selection.dimsToExplore[i].dim.id; | ||
54 | Packages.com.digdash.utils.MessageStack.getInstance().addText("Dimension Name: " + dimId); | ||
55 | } | ||
56 | {{/code}} | ||
57 | |||
58 | * Result: | ||
59 | |||
60 | Dimension Name: Date | ||
61 | Dimension Name: Produit | ||
62 | Dimension Name: Régions | ||
63 | |||
64 | * Usage: | ||
65 | |||
66 | Can be used if we want to change the rendered values if dimenisions are being explored in the graph | ||
67 | |||
68 | = Objet Global = | ||
69 | |||
70 | * Example: | ||
71 | |||
72 | {{code language="js"}} | ||
73 | if (!_global.count) | ||
74 | { | ||
75 | _global.count = 0; | ||
76 | } | ||
77 | else | ||
78 | { | ||
79 | _global.count = _global.count + 1; | ||
80 | } | ||
81 | return _global.count; | ||
82 | {{/code}} | ||
83 | |||
84 | * Result: | ||
85 | |||
86 | 1 | ||
87 | 2 | ||
88 | 3 | ||
89 | … (one for each line) | ||
90 | |||
91 | * Usage: | ||
92 | |||
93 | Allows you to save values in variables during the execution of the calculated measure | ||
94 | |||
95 | |||
96 | = Double pass = | ||
97 | |||
98 | * Example: | ||
99 | |||
100 | {{code language="js"}} | ||
101 | // needSecondPass : DO NOT REMOVE THIS LINE | ||
102 | if (phase == 0) | ||
103 | { | ||
104 | // do something | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | // do something | ||
109 | } | ||
110 | {{/code}} | ||
111 | |||
112 | * Result: | ||
113 | |||
114 | Forces the script to run twice on the calculated measure | ||
115 | |||
116 | * Usage: | ||
117 | |||
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/]]) | ||
119 | |||
120 | = Running on any axis = | ||
121 | |||
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 | ||
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}} |