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