@sisense/sdk-data 0.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +35 -0
- package/README.md +3 -0
- package/dist/dimensional-model/attributes.d.ts +124 -0
- package/dist/dimensional-model/attributes.js +280 -0
- package/dist/dimensional-model/base.d.ts +38 -0
- package/dist/dimensional-model/base.js +37 -0
- package/dist/dimensional-model/data-model.d.ts +13 -0
- package/dist/dimensional-model/data-model.js +32 -0
- package/dist/dimensional-model/dimensions.d.ts +165 -0
- package/dist/dimensional-model/dimensions.js +297 -0
- package/dist/dimensional-model/factory.d.ts +17 -0
- package/dist/dimensional-model/factory.js +50 -0
- package/dist/dimensional-model/filters/factory.d.ts +315 -0
- package/dist/dimensional-model/filters/factory.js +404 -0
- package/dist/dimensional-model/filters/filters.d.ts +288 -0
- package/dist/dimensional-model/filters/filters.js +534 -0
- package/dist/dimensional-model/interfaces.d.ts +341 -0
- package/dist/dimensional-model/interfaces.js +1 -0
- package/dist/dimensional-model/measures/factory.d.ts +437 -0
- package/dist/dimensional-model/measures/factory.js +632 -0
- package/dist/dimensional-model/measures/measures.d.ts +217 -0
- package/dist/dimensional-model/measures/measures.js +388 -0
- package/dist/dimensional-model/simple-column-types.d.ts +39 -0
- package/dist/dimensional-model/simple-column-types.js +124 -0
- package/dist/dimensional-model/types.d.ts +152 -0
- package/dist/dimensional-model/types.js +284 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +79 -0
- package/dist/interfaces.d.ts +233 -0
- package/dist/interfaces.js +9 -0
- package/package.json +47 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { DimensionalBaseMeasure, DimensionalCalculatedMeasure } from './measures.js';
|
|
2
|
+
import { AggregationTypes, MetadataTypes } from '../types.js';
|
|
3
|
+
import { normalizeName } from '../base.js';
|
|
4
|
+
/**
|
|
5
|
+
* Defines the different numeric operators that can be used with numeric filters
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export const RankingTypes = {
|
|
10
|
+
/**
|
|
11
|
+
* In competition ranking, items that compare equal receive the same ranking number, and then a gap is left in the ranking numbers.
|
|
12
|
+
*/
|
|
13
|
+
StandardCompetition: '1224',
|
|
14
|
+
/**
|
|
15
|
+
* Sometimes, competition ranking is done by leaving the gaps in the ranking numbers before the sets of equal-ranking items (rather than after them as in standard competition ranking)
|
|
16
|
+
*/
|
|
17
|
+
ModifiedCompetition: '1334',
|
|
18
|
+
/**
|
|
19
|
+
* In dense ranking, items that compare equally receive the same ranking number, and the next items receive the immediately following ranking number.
|
|
20
|
+
*/
|
|
21
|
+
Dense: '1223',
|
|
22
|
+
/**
|
|
23
|
+
* In ordinal ranking, all items receive distinct ordinal numbers, including items that compare equal. The assignment of distinct ordinal numbers is arbitrarily.
|
|
24
|
+
*/
|
|
25
|
+
Ordinal: '1234',
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Defines the different ranking sorting types supported by rank measure
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export const RankingSortTypes = {
|
|
33
|
+
Ascending: 'ASC',
|
|
34
|
+
Descending: 'DESC',
|
|
35
|
+
};
|
|
36
|
+
function addToFormula(builder, context, o) {
|
|
37
|
+
if (typeof o === 'number') {
|
|
38
|
+
builder.push(o.toString());
|
|
39
|
+
}
|
|
40
|
+
else if (o && MetadataTypes.isMeasureTemplate(o.type)) {
|
|
41
|
+
// default to sum
|
|
42
|
+
o = o.sum();
|
|
43
|
+
const name = `[${normalizeName(o.name)}]`;
|
|
44
|
+
builder.push(name);
|
|
45
|
+
context[name] = o;
|
|
46
|
+
}
|
|
47
|
+
else if (o && MetadataTypes.isMeasure(o.type)) {
|
|
48
|
+
const name = `[${normalizeName(o.name)}]`;
|
|
49
|
+
builder.push(name);
|
|
50
|
+
context[name] = o;
|
|
51
|
+
}
|
|
52
|
+
else if (o && MetadataTypes.isFilter(o.type)) {
|
|
53
|
+
const name = `[${normalizeName(o.name)}]`;
|
|
54
|
+
builder.push(name);
|
|
55
|
+
context[name] = o;
|
|
56
|
+
}
|
|
57
|
+
else if (o && MetadataTypes.isAttribute(o.type)) {
|
|
58
|
+
const name = `[${normalizeName(o.name)}]`;
|
|
59
|
+
builder.push(name);
|
|
60
|
+
context[name] = o;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function measureFunction(measure, name, func, options) {
|
|
64
|
+
const context = {};
|
|
65
|
+
const builder = [func + '('];
|
|
66
|
+
addToFormula(builder, context, measure);
|
|
67
|
+
if (options) {
|
|
68
|
+
builder.push(`, ${options}`);
|
|
69
|
+
}
|
|
70
|
+
builder.push(')');
|
|
71
|
+
return new DimensionalCalculatedMeasure(name, builder.join(''), context);
|
|
72
|
+
}
|
|
73
|
+
function arithmetic(operand1, operator, operand2, name, withParentheses) {
|
|
74
|
+
const builder = [];
|
|
75
|
+
const context = {};
|
|
76
|
+
if (withParentheses === true) {
|
|
77
|
+
builder.push('(');
|
|
78
|
+
}
|
|
79
|
+
addToFormula(builder, context, operand1);
|
|
80
|
+
builder.push(operator);
|
|
81
|
+
addToFormula(builder, context, operand2);
|
|
82
|
+
if (withParentheses === true) {
|
|
83
|
+
builder.push(')');
|
|
84
|
+
}
|
|
85
|
+
const exp = builder.join('');
|
|
86
|
+
return new DimensionalCalculatedMeasure(name !== null && name !== void 0 ? name : exp, exp, context);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Creates a basic aggregated measure.
|
|
90
|
+
* This is a base function to build other aggregation functions (e.g., `sum`, `average`, etc)
|
|
91
|
+
* as listed in {@link AggregationTypes}.
|
|
92
|
+
*
|
|
93
|
+
* @param attribute - Attribute to aggregate
|
|
94
|
+
* @param aggregationType - Aggregation type. See {@link AggregationTypes}
|
|
95
|
+
* @param name - Optional name for the new measure
|
|
96
|
+
* @param format - Numeric formatting to apply
|
|
97
|
+
* @returns A Measure instance
|
|
98
|
+
*/
|
|
99
|
+
export function aggregate(attribute, aggregationType, name, format) {
|
|
100
|
+
// if (aggregationType == AggregationTypes.Average || aggregationType == AggregationTypes.Max ||
|
|
101
|
+
// aggregationType == AggregationTypes.Min || aggregationType == AggregationTypes.Median ||
|
|
102
|
+
// aggregationType == AggregationTypes.Sum) {
|
|
103
|
+
// if (!MetadataTypes.isNumericDimension(attribute.type)) {
|
|
104
|
+
// throw `${aggregationType} is supported for numeric attributes only, where ${attribute.name} is ${attribute.type}`;
|
|
105
|
+
// }
|
|
106
|
+
// }
|
|
107
|
+
return new DimensionalBaseMeasure(name !== null && name !== void 0 ? name : `${aggregationType.toString()} ${attribute.name}`, attribute, aggregationType, format);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns a numeric value as a measure.
|
|
111
|
+
*
|
|
112
|
+
* @param value - Value to be returned as a measure
|
|
113
|
+
* @returns A Calculated Measure instance
|
|
114
|
+
*/
|
|
115
|
+
export function constant(value) {
|
|
116
|
+
return new DimensionalCalculatedMeasure(`${value}`, `${value}`, {});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Creates a sum aggregation over the given attribute.
|
|
120
|
+
*
|
|
121
|
+
* @param attribute - Attribute to aggregate
|
|
122
|
+
* @param name - Optional name for the new measure
|
|
123
|
+
* @param format - Optional numeric formatting to apply
|
|
124
|
+
* @returns A Measure instance
|
|
125
|
+
*/
|
|
126
|
+
export function sum(attribute, name, format) {
|
|
127
|
+
return aggregate(attribute, AggregationTypes.Sum, name, format);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Creates an average aggregation over the given attribute.
|
|
131
|
+
*
|
|
132
|
+
* @param attribute - Attribute to aggregate
|
|
133
|
+
* @param name - Optional name for the new measure
|
|
134
|
+
* @param format - Optional numeric formatting to apply
|
|
135
|
+
* @returns A Measure instance
|
|
136
|
+
*/
|
|
137
|
+
export function average(attribute, name, format) {
|
|
138
|
+
return aggregate(attribute, AggregationTypes.Average, name, format);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Creates a min aggregation over the given attribute.
|
|
142
|
+
*
|
|
143
|
+
* @param attribute - Attribute to aggregate
|
|
144
|
+
* @param name - Optional name for the new measure
|
|
145
|
+
* @param format - Optional numeric formatting to apply
|
|
146
|
+
* @returns A Measure instance
|
|
147
|
+
*/
|
|
148
|
+
export function min(attribute, name, format) {
|
|
149
|
+
return aggregate(attribute, AggregationTypes.Min, name, format);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Creates a max aggregation over the given attribute.
|
|
153
|
+
*
|
|
154
|
+
* @param attribute - Attribute to aggregate
|
|
155
|
+
* @param name - Optional name for the new measure
|
|
156
|
+
* @param format - Optional numeric formatting to apply
|
|
157
|
+
* @returns A Measure instance
|
|
158
|
+
*/
|
|
159
|
+
export function max(attribute, name, format) {
|
|
160
|
+
return aggregate(attribute, AggregationTypes.Max, name, format);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Creates a median aggregation over the given attribute.
|
|
164
|
+
*
|
|
165
|
+
* @param attribute - Attribute to aggregate
|
|
166
|
+
* @param name - Optional name for the new measure
|
|
167
|
+
* @param format - Optional numeric formatting to apply
|
|
168
|
+
* @returns A Measure instance
|
|
169
|
+
*/
|
|
170
|
+
export function median(attribute, name, format) {
|
|
171
|
+
return aggregate(attribute, AggregationTypes.Median, name, format);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Creates a count aggregation over the given attribute.
|
|
175
|
+
*
|
|
176
|
+
* @param attribute - Attribute to aggregate
|
|
177
|
+
* @param name - Optional name for the new measure
|
|
178
|
+
* @param format - Optional numeric formatting to apply
|
|
179
|
+
* @returns A Measure instance
|
|
180
|
+
*/
|
|
181
|
+
export function count(attribute, name, format) {
|
|
182
|
+
return aggregate(attribute, AggregationTypes.Count, name, format);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Creates a count distinct aggregation over the given attribute.
|
|
186
|
+
*
|
|
187
|
+
* @param attribute - Attribute to aggregate
|
|
188
|
+
* @param name - Optional name for the new measure
|
|
189
|
+
* @param format - Optional numeric formatting to apply
|
|
190
|
+
* @returns A Measure instance
|
|
191
|
+
*/
|
|
192
|
+
export function countDistinct(attribute, name, format) {
|
|
193
|
+
return aggregate(attribute, AggregationTypes.CountDistinct, name, format);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Creates a measured value with the given measure and set of filters.
|
|
197
|
+
*
|
|
198
|
+
* @param measure - Measure to filter
|
|
199
|
+
* @param filters - Filters to apply to the measure
|
|
200
|
+
* @param name - Optional name for the new measure
|
|
201
|
+
* @param format - Optional numeric formatting to apply
|
|
202
|
+
* @returns A Calculated Measure instance
|
|
203
|
+
*/
|
|
204
|
+
export function measuredValue(measure, filters, name, format) {
|
|
205
|
+
const builder = [];
|
|
206
|
+
const context = {};
|
|
207
|
+
builder.push('(');
|
|
208
|
+
addToFormula(builder, context, measure);
|
|
209
|
+
filters.forEach((f) => {
|
|
210
|
+
builder.push(',');
|
|
211
|
+
addToFormula(builder, context, f);
|
|
212
|
+
});
|
|
213
|
+
builder.push(')');
|
|
214
|
+
const exp = builder.join('');
|
|
215
|
+
return new DimensionalCalculatedMeasure(name !== null && name !== void 0 ? name : exp, exp, context, format);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Adds the two given values.
|
|
219
|
+
*
|
|
220
|
+
* @param value1 - First value
|
|
221
|
+
* @param value2 - Second value
|
|
222
|
+
* @param name - Optional name for the new measure
|
|
223
|
+
* @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
|
|
224
|
+
* @returns A Calculated Measure instance
|
|
225
|
+
*/
|
|
226
|
+
export function add(value1, value2, name, withParentheses) {
|
|
227
|
+
return arithmetic(value1, '+', value2, name, withParentheses);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Subtract value2 from value1.
|
|
231
|
+
*
|
|
232
|
+
* @param value1 - First value
|
|
233
|
+
* @param value2 - Second value
|
|
234
|
+
* @param name - Optional name for the new measure
|
|
235
|
+
* @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
|
|
236
|
+
* @returns A Calculated Measure instance
|
|
237
|
+
*/
|
|
238
|
+
export function subtract(value1, value2, name, withParentheses) {
|
|
239
|
+
return arithmetic(value1, '-', value2, name, withParentheses);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Multiply value1 with value2.
|
|
243
|
+
*
|
|
244
|
+
* @param value1 - First value
|
|
245
|
+
* @param value2 - Second value
|
|
246
|
+
* @param name - Optional name for the new measure
|
|
247
|
+
* @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
|
|
248
|
+
* @returns A Calculated Measure instance
|
|
249
|
+
*/
|
|
250
|
+
export function multiply(value1, value2, name, withParentheses) {
|
|
251
|
+
return arithmetic(value1, '*', value2, name, withParentheses);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Divide value1 by value2.
|
|
255
|
+
*
|
|
256
|
+
* @param value1 - First value
|
|
257
|
+
* @param value2 - Second value
|
|
258
|
+
* @param name - Optional name for the new measure
|
|
259
|
+
* @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
|
|
260
|
+
* @returns A Calculated Measure instance
|
|
261
|
+
*/
|
|
262
|
+
export function divide(value1, value2, name, withParentheses) {
|
|
263
|
+
return arithmetic(value1, '/', value2, name, withParentheses);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Calculates year to date (YTD) sum of the given measure. Date dimension will be dynamically taken from the query.
|
|
267
|
+
*
|
|
268
|
+
* @param measure - Measure to apply the YTD Sum to
|
|
269
|
+
* @param name - Name for the new measure
|
|
270
|
+
* @returns A Calculated Measure instance
|
|
271
|
+
*/
|
|
272
|
+
export function yearToDateSum(measure, name) {
|
|
273
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : 'YTD ' + measure.name, 'YTDSum');
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Calculates quarter to date (QTD) sum of the given measure.
|
|
277
|
+
* Date dimension will be dynamically taken from the query.
|
|
278
|
+
*
|
|
279
|
+
* @param measure - Measure to apply the QTD Sum to
|
|
280
|
+
* @param name - Name for the new measure
|
|
281
|
+
* @returns A Calculated Measure instance
|
|
282
|
+
*/
|
|
283
|
+
export function quarterToDateSum(measure, name) {
|
|
284
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : 'QTD ' + name, 'QTDSum');
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Calculates month to date (MTD) sum of the given measure.
|
|
288
|
+
* Date dimension will be dynamically taken from the query.
|
|
289
|
+
*
|
|
290
|
+
* @param measure - Measure to apply the MTD Sum to
|
|
291
|
+
* @param name - Name for the new measure
|
|
292
|
+
* @returns A Calculated Measure instance
|
|
293
|
+
*/
|
|
294
|
+
export function monthToDateSum(measure, name) {
|
|
295
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : 'MTD ' + measure.name, 'MTDSum');
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Calculates week to date (WTD) sum of the given measure.
|
|
299
|
+
* Date dimension will be dynamically taken from the query.
|
|
300
|
+
*
|
|
301
|
+
* @param measure - Measure to apply the WTD Sum to
|
|
302
|
+
* @param name - Name for the new measure
|
|
303
|
+
* @returns A Calculated Measure instance
|
|
304
|
+
*/
|
|
305
|
+
export function weekToDateSum(measure, name) {
|
|
306
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : 'MTD ' + measure.name, 'WTDSum');
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Returns the running total of the measure by the defined dimension
|
|
310
|
+
* according to the current sorting order in the query.
|
|
311
|
+
*
|
|
312
|
+
* By default, `RSUM` accumulates a measure by the sorting order of the dimension.
|
|
313
|
+
* To accumulate by another order, the relevant measure should be added as an additional column and sorted.
|
|
314
|
+
*
|
|
315
|
+
* Note: Filtering the `RSUM` column by Values,
|
|
316
|
+
* filters the dimensions and recalculates the `RSUM` from the first filtered value.
|
|
317
|
+
*
|
|
318
|
+
* @param measure - Measure to apply the Running Sum to
|
|
319
|
+
* @param _continuous - Boolean flag whether to accumulate the sum continuously
|
|
320
|
+
* when there are two or more dimensions. The default value is False.
|
|
321
|
+
* @param name - Name for the new measure
|
|
322
|
+
* @returns A Calculated Measure instance
|
|
323
|
+
*/
|
|
324
|
+
export function runningSum(measure, _continuous, name) {
|
|
325
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : 'Running Sum ' + measure.name, 'RSum');
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Calculates growth over time. The time dimension to be used is determined by the time resolution in the query.
|
|
329
|
+
*
|
|
330
|
+
* Formula: `(current value – compared value) / compared value`.
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
*
|
|
334
|
+
* If this month your value is 12, and last month it was 10, your Growth for this month is 20% (0.2).
|
|
335
|
+
*
|
|
336
|
+
* Calculation: `(12 – 10) / 10 = 0.2`
|
|
337
|
+
*
|
|
338
|
+
* If this year your value is 80, and last year it was 100, your Growth for this year is -20% ( -0.2).
|
|
339
|
+
*
|
|
340
|
+
* Calculation: `(80 – 100) / 100 = -0.2`
|
|
341
|
+
* @param measure - Measure to apply growth to
|
|
342
|
+
* @param name - Name for the new measure
|
|
343
|
+
* @returns A Calculated Measure instance
|
|
344
|
+
*/
|
|
345
|
+
export function growth(measure, name) {
|
|
346
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growth');
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Calculates growth rate over time. The time dimension to be used is determined by the time resolution in the query.
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* If this month your value is 12, and last month it was 10, your Growth Rate for this month is 12/10 = 120% (1.2).
|
|
353
|
+
*
|
|
354
|
+
* Calculation: `12 / 10 = 1.2`
|
|
355
|
+
*
|
|
356
|
+
* If this year your value is 80, and last year it was 100, your Growth for this year is 80/100 = 80% ( 0.8).
|
|
357
|
+
*
|
|
358
|
+
* Calculation: `80 / 100 = 0.8`
|
|
359
|
+
* @param measure - Measure to apply the Growth rate
|
|
360
|
+
* @param name - Name for the new measure
|
|
361
|
+
* @returns A Calculated Measure instance
|
|
362
|
+
*/
|
|
363
|
+
export function growthRate(measure, name) {
|
|
364
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthrate');
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Calculates the growth from past week of the given measure.
|
|
368
|
+
* Date dimension will be dynamically taken from the query.
|
|
369
|
+
*
|
|
370
|
+
* @param measure - Measure to apply growth to
|
|
371
|
+
* @param name - Name for the new measure
|
|
372
|
+
* @returns A Calculated Measure instance
|
|
373
|
+
*/
|
|
374
|
+
export function growthPastWeek(measure, name) {
|
|
375
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastweek');
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Calculates the growth from past month of the given measure.
|
|
379
|
+
* Date dimension will be dynamically taken from the query
|
|
380
|
+
*
|
|
381
|
+
* @param measure - Measure to apply growth to
|
|
382
|
+
* @param name - Name for the new measure
|
|
383
|
+
* @returns A Calculated Measure instance
|
|
384
|
+
*/
|
|
385
|
+
export function growthPastMonth(measure, name) {
|
|
386
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastmonth');
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Calculates the growth from past quarter of the given measure.
|
|
390
|
+
* Date dimension will be dynamically taken from the query.
|
|
391
|
+
*
|
|
392
|
+
* @param measure - Measure to apply growth to
|
|
393
|
+
* @param name - Name for the new measure
|
|
394
|
+
* @returns A Calculated Measure instance
|
|
395
|
+
*/
|
|
396
|
+
export function growthPastQuarter(measure, name) {
|
|
397
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastquarter');
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Calculates the growth from past year of the given measure.
|
|
401
|
+
* Date dimension will be dynamically taken from the query.
|
|
402
|
+
*
|
|
403
|
+
* @param measure - Measure to apply growth to
|
|
404
|
+
* @param name - Name for the new measure
|
|
405
|
+
* @returns A Calculated Measure instance
|
|
406
|
+
*/
|
|
407
|
+
export function growthPastYear(measure, name) {
|
|
408
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastyear');
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Calculates the difference of the given measure.
|
|
412
|
+
* Date dimension will be dynamically taken from the query.
|
|
413
|
+
*
|
|
414
|
+
* @param measure - measure to apply difference to
|
|
415
|
+
* @param name - Name for the new measure
|
|
416
|
+
* @returns A Calculated Measure instance
|
|
417
|
+
*/
|
|
418
|
+
export function difference(measure, name) {
|
|
419
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastperiod');
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Calculates the difference from past week of the given measure.
|
|
423
|
+
* Ddate dimension will be dynamically taken from the query.
|
|
424
|
+
*
|
|
425
|
+
* @param measure - Measure to apply difference to
|
|
426
|
+
* @param name - Name for the new measure
|
|
427
|
+
* @returns A Calculated Measure instance
|
|
428
|
+
*/
|
|
429
|
+
export function diffPastWeek(measure, name) {
|
|
430
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastweek');
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Calculates the difference from past month of the given measure.
|
|
434
|
+
* Date dimension will be dynamically taken from the query.
|
|
435
|
+
*
|
|
436
|
+
* @param measure - Measure to apply difference to
|
|
437
|
+
* @param name - Name for the new measure
|
|
438
|
+
* @returns A Calculated Measure instance
|
|
439
|
+
*/
|
|
440
|
+
export function diffPastMonth(measure, name) {
|
|
441
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastmonth');
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Calculates the difference from past quarter of the given measure.
|
|
445
|
+
* Date dimension will be dynamically taken from the query.
|
|
446
|
+
*
|
|
447
|
+
* @param measure - Measure to apply difference to
|
|
448
|
+
* @param name - Name for the new measure
|
|
449
|
+
* @returns A Calculated Measure instance
|
|
450
|
+
*/
|
|
451
|
+
export function diffPastQuarter(measure, name) {
|
|
452
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastquarter');
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Calculates the difference from past year of the given measure.
|
|
456
|
+
* Date dimension will be dynamically taken from the query.
|
|
457
|
+
*
|
|
458
|
+
* @param measure - Measure to apply difference to
|
|
459
|
+
* @param name - Name for the new measure
|
|
460
|
+
* @returns A Calculated Measure instance
|
|
461
|
+
*/
|
|
462
|
+
export function diffPastYear(measure, name) {
|
|
463
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastyear');
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Calculates the value of the past day of the given measure.
|
|
467
|
+
* Date dimension will be dynamically taken from the query.
|
|
468
|
+
*
|
|
469
|
+
* @param measure - Measure to apply past value to
|
|
470
|
+
* @param name - Name for the new measure
|
|
471
|
+
* @returns A Calculated Measure instance
|
|
472
|
+
*/
|
|
473
|
+
export function pastDay(measure, name) {
|
|
474
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Day', 'pastday');
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Calculates the value of the past week of the given measure.
|
|
478
|
+
* Date dimension will be dynamically taken from the query.
|
|
479
|
+
*
|
|
480
|
+
* @param measure - Measure to apply past value to
|
|
481
|
+
* @param name - Name for the new measure
|
|
482
|
+
* @returns A Calculated Measure instance
|
|
483
|
+
*/
|
|
484
|
+
export function pastWeek(measure, name) {
|
|
485
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Week', 'pastweek');
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Calculates the value of the path month of the given measure.
|
|
489
|
+
* Date dimension will be dynamically taken from the query.
|
|
490
|
+
*
|
|
491
|
+
* @param measure - Measure to apply past value to
|
|
492
|
+
* @param name - Name for the new measure
|
|
493
|
+
* @returns A Calculated Measure instance
|
|
494
|
+
*/
|
|
495
|
+
export function pastMonth(measure, name) {
|
|
496
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Month', 'pastmonth');
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Calculates the value of the past quarter of the given measure.
|
|
500
|
+
* Date dimension will be dynamically taken from the query.
|
|
501
|
+
*
|
|
502
|
+
* @param measure - Measure to apply past value to
|
|
503
|
+
* @param name - Name for the new measure
|
|
504
|
+
* @returns A Calculated Measure instance
|
|
505
|
+
*/
|
|
506
|
+
export function pastQuarter(measure, name) {
|
|
507
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Quarter', 'pastquarter');
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Calculates the value of the past year of the given measure.
|
|
511
|
+
* Date dimension will be dynamically taken from the query.
|
|
512
|
+
*
|
|
513
|
+
* @param measure - Measure to apply past value to
|
|
514
|
+
* @param name - Name for the new measure
|
|
515
|
+
* @returns A Calculated Measure instance
|
|
516
|
+
*/
|
|
517
|
+
export function pastYear(measure, name) {
|
|
518
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Year', 'pastyear');
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Calculates contribution.
|
|
522
|
+
*
|
|
523
|
+
* @param measure - Measure to apply the Contribution logic to
|
|
524
|
+
* @param name - Name for the new measure
|
|
525
|
+
* @returns A Calculated Measure instance
|
|
526
|
+
*/
|
|
527
|
+
export function contribution(measure, name) {
|
|
528
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Contribution', 'contribution');
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Fits a specified trend type to your measure. The trend types include linear,
|
|
532
|
+
* logarithmic, advanced smoothing, and local estimates. It allows for an optional
|
|
533
|
+
* feature to automatically identify and ignore anomalous values in the series.
|
|
534
|
+
*
|
|
535
|
+
* Trend requires a Sisense instance version of L2023.6.0 or greater.
|
|
536
|
+
*
|
|
537
|
+
* @param measure - Measure to apply the trend logic to
|
|
538
|
+
* @param name - Name for the new measure
|
|
539
|
+
* @param options - Trend options
|
|
540
|
+
* @returns A Calculated Measure instance
|
|
541
|
+
*/
|
|
542
|
+
export function trend(measure, name, options) {
|
|
543
|
+
let params;
|
|
544
|
+
const adjustValues = (value) => value
|
|
545
|
+
.replace('advancedSmoothing', 'Advanced Smoothing')
|
|
546
|
+
.replace('localEstimates', 'Local Estimates');
|
|
547
|
+
if (options) {
|
|
548
|
+
// make a comma separated name=value string based on options
|
|
549
|
+
params = Object.entries(options)
|
|
550
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
551
|
+
.map((e) => adjustValues(`"${e[0]}=${e[1]}"`))
|
|
552
|
+
.join(',');
|
|
553
|
+
}
|
|
554
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Trend', 'trend', params);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Calculates Forecast leveraging advanced autoML techniques to generate
|
|
558
|
+
* a forecast for a given measure.
|
|
559
|
+
*
|
|
560
|
+
* This function offers flexibility with auto-selection of the best
|
|
561
|
+
* statistical model or user-selected models, and it also provides control
|
|
562
|
+
* over the time period used for training the model, as well as options to
|
|
563
|
+
* improve forecast accuracy by supplying expected lower and upper limits.
|
|
564
|
+
*
|
|
565
|
+
* In addition to forecast, upper and lower confidence interval is returned
|
|
566
|
+
* with the name of the new measure and a suffix of _upper and _lower
|
|
567
|
+
* respectively.
|
|
568
|
+
*
|
|
569
|
+
* Forecast requires a Sisense instance version of L2023.6.0 or greater.
|
|
570
|
+
*
|
|
571
|
+
* @param measure - Measure to apply the forecast logic to
|
|
572
|
+
* @param name - Name for the new measure
|
|
573
|
+
* @param options - Forecast options
|
|
574
|
+
* @returns A Calculated Measure instance
|
|
575
|
+
*/
|
|
576
|
+
export function forecast(measure, name, options) {
|
|
577
|
+
let params;
|
|
578
|
+
if (options) {
|
|
579
|
+
// create ISO string values for any Date objects
|
|
580
|
+
const adjustedOptions = Object.assign({}, options);
|
|
581
|
+
if (adjustedOptions.startDate) {
|
|
582
|
+
const startDate = new Date(adjustedOptions.startDate);
|
|
583
|
+
adjustedOptions.startDate = startDate.toISOString().replace(/.\d+Z$/g, '');
|
|
584
|
+
}
|
|
585
|
+
if (adjustedOptions.endDate) {
|
|
586
|
+
const endDate = new Date(adjustedOptions.endDate);
|
|
587
|
+
adjustedOptions.endDate = endDate.toISOString().replace(/.\d+Z$/g, '');
|
|
588
|
+
}
|
|
589
|
+
// make a comma separated name=value string based on options
|
|
590
|
+
params = Object.entries(adjustedOptions)
|
|
591
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
592
|
+
.map((e) => `"${e[0]}=${e[1]}"`)
|
|
593
|
+
.join(',');
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
params = '"forecastHorizon=3"';
|
|
597
|
+
}
|
|
598
|
+
return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Forecast', 'forecast', params);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Calculates the rank of a value in a list of values.
|
|
602
|
+
*
|
|
603
|
+
* @example
|
|
604
|
+
* `RANK(Total Cost, “ASC”, “1224”, Product, Years)`
|
|
605
|
+
* will return the rank of the total annual cost per each product were sorted in ascending order.
|
|
606
|
+
* @param measure - Measure to apply the Contribution logic to
|
|
607
|
+
* @param name - Name for the new measure
|
|
608
|
+
* @param sort - By default sort order is descending.
|
|
609
|
+
* @param rankType - By default the type is standard competition ranking `(“1224” ranking)`.
|
|
610
|
+
* Supports also modified competition ranking `(“1334” ranking)`, dense ranking `(“1223” ranking)`,
|
|
611
|
+
* and ordinal ranking `(“1234” ranking)`.
|
|
612
|
+
* @param groupBy - Rank partitions attributes
|
|
613
|
+
* @returns A rank measure
|
|
614
|
+
*/
|
|
615
|
+
export function rank(measure, name, sort = RankingSortTypes.Descending, rankType = RankingTypes.StandardCompetition, groupBy = []) {
|
|
616
|
+
const builder = [];
|
|
617
|
+
const context = {};
|
|
618
|
+
builder.push('rank(');
|
|
619
|
+
addToFormula(builder, context, measure);
|
|
620
|
+
builder.push(`,${sort},${rankType}`);
|
|
621
|
+
groupBy.forEach((groupByAttr) => {
|
|
622
|
+
builder.push(',');
|
|
623
|
+
addToFormula(builder, context, groupByAttr);
|
|
624
|
+
});
|
|
625
|
+
builder.push(')');
|
|
626
|
+
const exp = builder.join('');
|
|
627
|
+
// default name
|
|
628
|
+
if (!name) {
|
|
629
|
+
name = `${measure.name} rank`;
|
|
630
|
+
}
|
|
631
|
+
return new DimensionalCalculatedMeasure(name, exp, context);
|
|
632
|
+
}
|