@sisense/sdk-data 1.11.0 → 1.13.0

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.
Files changed (99) hide show
  1. package/dist/cjs/dimensional-model/analytics/factory.d.ts +47 -0
  2. package/dist/cjs/dimensional-model/analytics/factory.js +153 -0
  3. package/dist/cjs/dimensional-model/analytics/factory.test.d.ts +1 -0
  4. package/dist/cjs/dimensional-model/analytics/factory.test.js +99 -0
  5. package/dist/cjs/dimensional-model/attributes.d.ts +139 -0
  6. package/dist/cjs/dimensional-model/attributes.js +342 -0
  7. package/dist/cjs/dimensional-model/attributes.test.d.ts +1 -0
  8. package/dist/cjs/dimensional-model/attributes.test.js +154 -0
  9. package/dist/cjs/dimensional-model/base.d.ts +43 -0
  10. package/dist/cjs/dimensional-model/base.js +58 -0
  11. package/dist/cjs/dimensional-model/base.test.d.ts +1 -0
  12. package/dist/cjs/dimensional-model/base.test.js +17 -0
  13. package/dist/cjs/dimensional-model/data-model.d.ts +13 -0
  14. package/dist/cjs/dimensional-model/data-model.js +37 -0
  15. package/dist/cjs/dimensional-model/dimensions.d.ts +167 -0
  16. package/dist/cjs/dimensional-model/dimensions.js +307 -0
  17. package/dist/cjs/dimensional-model/dimensions.test.d.ts +1 -0
  18. package/dist/cjs/dimensional-model/dimensions.test.js +54 -0
  19. package/dist/cjs/dimensional-model/factory.d.ts +17 -0
  20. package/dist/cjs/dimensional-model/factory.js +54 -0
  21. package/dist/cjs/dimensional-model/filters/factory.d.ts +797 -0
  22. package/dist/cjs/dimensional-model/filters/factory.js +962 -0
  23. package/dist/cjs/dimensional-model/filters/factory.test.d.ts +1 -0
  24. package/dist/cjs/dimensional-model/filters/factory.test.js +366 -0
  25. package/dist/cjs/dimensional-model/filters/filters.d.ts +344 -0
  26. package/dist/cjs/dimensional-model/filters/filters.js +663 -0
  27. package/dist/cjs/dimensional-model/filters/filters.test.d.ts +1 -0
  28. package/dist/cjs/dimensional-model/filters/filters.test.js +225 -0
  29. package/dist/cjs/dimensional-model/filters/utils/attribute-measure-util.d.ts +47 -0
  30. package/dist/cjs/dimensional-model/filters/utils/attribute-measure-util.js +111 -0
  31. package/dist/cjs/dimensional-model/filters/utils/condition-filter-util.d.ts +21 -0
  32. package/dist/cjs/dimensional-model/filters/utils/condition-filter-util.js +196 -0
  33. package/dist/cjs/dimensional-model/filters/utils/date-time-filter-util.d.ts +2 -0
  34. package/dist/cjs/dimensional-model/filters/utils/date-time-filter-util.js +12 -0
  35. package/dist/cjs/dimensional-model/filters/utils/filter-code-util.d.ts +13 -0
  36. package/dist/cjs/dimensional-model/filters/utils/filter-code-util.js +58 -0
  37. package/dist/cjs/dimensional-model/filters/utils/filter-code-util.test.d.ts +1 -0
  38. package/dist/cjs/dimensional-model/filters/utils/filter-code-util.test.js +32 -0
  39. package/dist/cjs/dimensional-model/filters/utils/filter-from-jaql-util.d.ts +82 -0
  40. package/dist/cjs/dimensional-model/filters/utils/filter-from-jaql-util.js +215 -0
  41. package/dist/cjs/dimensional-model/filters/utils/filter-from-jaql-util.test.d.ts +1 -0
  42. package/dist/cjs/dimensional-model/filters/utils/filter-from-jaql-util.test.js +623 -0
  43. package/dist/cjs/dimensional-model/filters/utils/filter-matcher-utils.d.ts +5 -0
  44. package/dist/cjs/dimensional-model/filters/utils/filter-matcher-utils.js +174 -0
  45. package/dist/cjs/dimensional-model/filters/utils/filter-matcher-utils.test.d.ts +1 -0
  46. package/dist/cjs/dimensional-model/filters/utils/filter-matcher-utils.test.js +236 -0
  47. package/dist/cjs/dimensional-model/filters/utils/filter-types-util.d.ts +17 -0
  48. package/dist/cjs/dimensional-model/filters/utils/filter-types-util.js +79 -0
  49. package/dist/cjs/dimensional-model/filters/utils/types.d.ts +206 -0
  50. package/dist/cjs/dimensional-model/filters/utils/types.js +99 -0
  51. package/dist/cjs/dimensional-model/interfaces.d.ts +512 -0
  52. package/dist/cjs/dimensional-model/interfaces.js +31 -0
  53. package/dist/cjs/dimensional-model/measures/factory.d.ts +920 -0
  54. package/dist/cjs/dimensional-model/measures/factory.js +1188 -0
  55. package/dist/cjs/dimensional-model/measures/factory.test.d.ts +1 -0
  56. package/dist/cjs/dimensional-model/measures/factory.test.js +481 -0
  57. package/dist/cjs/dimensional-model/measures/measures.d.ts +217 -0
  58. package/dist/cjs/dimensional-model/measures/measures.js +416 -0
  59. package/dist/cjs/dimensional-model/measures/measures.test.d.ts +1 -0
  60. package/dist/cjs/dimensional-model/measures/measures.test.js +79 -0
  61. package/dist/cjs/dimensional-model/simple-column-types.d.ts +39 -0
  62. package/dist/cjs/dimensional-model/simple-column-types.js +134 -0
  63. package/dist/cjs/dimensional-model/simple-column-types.test.d.ts +1 -0
  64. package/dist/cjs/dimensional-model/simple-column-types.test.js +85 -0
  65. package/dist/cjs/dimensional-model/types.d.ts +256 -0
  66. package/dist/cjs/dimensional-model/types.js +298 -0
  67. package/dist/cjs/dimensional-model/types.test.d.ts +1 -0
  68. package/dist/cjs/dimensional-model/types.test.js +33 -0
  69. package/dist/cjs/index.d.ts +93 -0
  70. package/dist/cjs/index.js +123 -0
  71. package/dist/cjs/interfaces.d.ts +367 -0
  72. package/dist/cjs/interfaces.js +21 -0
  73. package/dist/cjs/translation/initialize-i18n.d.ts +2 -0
  74. package/dist/cjs/translation/initialize-i18n.js +14 -0
  75. package/dist/cjs/translation/resources/en.d.ts +28 -0
  76. package/dist/cjs/translation/resources/en.js +30 -0
  77. package/dist/cjs/translation/resources/index.d.ts +53 -0
  78. package/dist/cjs/translation/resources/index.js +10 -0
  79. package/dist/cjs/translation/resources/uk.d.ts +5 -0
  80. package/dist/cjs/translation/resources/uk.js +30 -0
  81. package/dist/cjs/translation/translatable-error.d.ts +5 -0
  82. package/dist/cjs/translation/translatable-error.js +15 -0
  83. package/dist/cjs/utils.d.ts +37 -0
  84. package/dist/cjs/utils.js +105 -0
  85. package/dist/cjs/utils.test.d.ts +1 -0
  86. package/dist/cjs/utils.test.js +158 -0
  87. package/dist/dimensional-model/analytics/factory.js +6 -4
  88. package/dist/dimensional-model/filters/factory.d.ts +81 -42
  89. package/dist/dimensional-model/filters/factory.js +125 -87
  90. package/dist/dimensional-model/filters/filters.d.ts +58 -13
  91. package/dist/dimensional-model/filters/filters.js +117 -27
  92. package/dist/dimensional-model/filters/utils/condition-filter-util.d.ts +4 -2
  93. package/dist/dimensional-model/filters/utils/condition-filter-util.js +30 -26
  94. package/dist/dimensional-model/filters/utils/filter-code-util.js +5 -1
  95. package/dist/dimensional-model/filters/utils/filter-from-jaql-util.d.ts +26 -11
  96. package/dist/dimensional-model/filters/utils/filter-from-jaql-util.js +58 -27
  97. package/dist/dimensional-model/filters/utils/filter-types-util.js +3 -3
  98. package/dist/dimensional-model/filters/utils/types.d.ts +6 -0
  99. package/package.json +18 -9
@@ -0,0 +1,1188 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.rank = exports.forecast = exports.trend = exports.contribution = exports.pastYear = exports.pastQuarter = exports.pastMonth = exports.pastWeek = exports.pastDay = exports.diffPastYear = exports.diffPastQuarter = exports.diffPastMonth = exports.diffPastWeek = exports.difference = exports.growthPastYear = exports.growthPastQuarter = exports.growthPastMonth = exports.growthPastWeek = exports.growthRate = exports.growth = exports.runningSum = exports.weekToDateSum = exports.monthToDateSum = exports.quarterToDateSum = exports.yearToDateSum = exports.divide = exports.multiply = exports.subtract = exports.add = exports.measuredValue = exports.countDistinct = exports.count = exports.median = exports.max = exports.min = exports.avg = exports.average = exports.sum = exports.constant = exports.aggregate = exports.customFormula = exports.RankingSortTypes = exports.RankingTypes = void 0;
7
+ const measures_js_1 = require("./measures.js");
8
+ const types_js_1 = require("../types.js");
9
+ const base_js_1 = require("../base.js");
10
+ const mapValues_js_1 = __importDefault(require("lodash/mapValues.js"));
11
+ const attributes_js_1 = require("../attributes.js");
12
+ const simple_column_types_js_1 = require("./../simple-column-types.js");
13
+ /**
14
+ * Defines the different numeric operators that can be used with numeric filters
15
+ *
16
+ * @internal
17
+ */
18
+ exports.RankingTypes = {
19
+ /**
20
+ * In competition ranking, items that rank equally receive the same ranking number, and then a gap is left after the equally ranked items in the ranking numbers.
21
+ */
22
+ StandardCompetition: '1224',
23
+ /**
24
+ * In modified competition ranking, items that rank equally receive the same ranking number, and a gap is left before the equally ranked items in the ranking numbers.
25
+ */
26
+ ModifiedCompetition: '1334',
27
+ /**
28
+ * In dense ranking, items that rank equally receive the same ranking number, and the next items receive the immediately following ranking number.
29
+ */
30
+ Dense: '1223',
31
+ /**
32
+ * In ordinal ranking, all items receive distinct ordinal numbers, including items that rank equally. The assignment of distinct ordinal numbers for equal-ranking items is arbitrary.
33
+ */
34
+ Ordinal: '1234',
35
+ };
36
+ /**
37
+ * Defines the different ranking sorting types supported by rank measure
38
+ *
39
+ * @internal
40
+ */
41
+ exports.RankingSortTypes = {
42
+ Ascending: 'ASC',
43
+ Descending: 'DESC',
44
+ };
45
+ function addToFormula(builder, context, o) {
46
+ if (typeof o === 'number') {
47
+ builder.push(o.toString());
48
+ }
49
+ else if (o && types_js_1.MetadataTypes.isMeasureTemplate(o.type)) {
50
+ // default to sum
51
+ o = o.sum();
52
+ const name = `[${(0, base_js_1.normalizeName)(o.name)}]`;
53
+ builder.push(name);
54
+ context[name] = o;
55
+ }
56
+ else if (o && types_js_1.MetadataTypes.isMeasure(o.type)) {
57
+ const name = `[${(0, base_js_1.normalizeName)(o.name)}]`;
58
+ builder.push(name);
59
+ context[name] = o;
60
+ }
61
+ else if (o && types_js_1.MetadataTypes.isFilter(o.type)) {
62
+ const name = `[${(0, base_js_1.normalizeName)(o.name)}]`;
63
+ builder.push(name);
64
+ context[name] = o;
65
+ }
66
+ else if (o && types_js_1.MetadataTypes.isAttribute(o.type)) {
67
+ const name = `[${(0, base_js_1.normalizeName)(o.name)}]`;
68
+ builder.push(name);
69
+ context[name] = o;
70
+ }
71
+ }
72
+ function measureFunction(measure, name, func, options) {
73
+ const context = {};
74
+ const builder = [func + '('];
75
+ addToFormula(builder, context, measure);
76
+ if (options) {
77
+ builder.push(`, ${options}`);
78
+ }
79
+ builder.push(')');
80
+ return new measures_js_1.DimensionalCalculatedMeasure(name, builder.join(''), context);
81
+ }
82
+ function transformCustomFormulaJaql(jaql) {
83
+ var _a;
84
+ const isFormulaJaql = 'formula' in jaql;
85
+ let sort;
86
+ if (jaql.sort) {
87
+ sort = jaql.sort === 'asc' ? types_js_1.Sort.Ascending : types_js_1.Sort.Descending;
88
+ }
89
+ if (isFormulaJaql) {
90
+ const context = (0, mapValues_js_1.default)((_a = jaql.context) !== null && _a !== void 0 ? _a : {}, (jaqlContextValue) => jaqlContextValue ? transformCustomFormulaJaql(jaqlContextValue) : {});
91
+ return new measures_js_1.DimensionalCalculatedMeasure(jaql.title, jaql.formula, context, undefined, undefined, sort);
92
+ }
93
+ const hasAggregation = !!jaql.agg;
94
+ const isDatatypeDatetime = (0, simple_column_types_js_1.isDatetime)(jaql.datatype);
95
+ const attributeType = (0, simple_column_types_js_1.isNumber)(jaql.datatype)
96
+ ? types_js_1.MetadataTypes.NumericAttribute
97
+ : types_js_1.MetadataTypes.TextAttribute;
98
+ const attribute = isDatatypeDatetime
99
+ ? new attributes_js_1.DimensionalLevelAttribute(jaql.title, jaql.dim, attributes_js_1.DimensionalLevelAttribute.translateJaqlToGranularity(jaql), undefined, undefined, sort)
100
+ : new attributes_js_1.DimensionalAttribute(jaql.title, jaql.dim, attributeType, undefined, sort);
101
+ if (hasAggregation) {
102
+ return new measures_js_1.DimensionalBaseMeasure(jaql.title, attribute, measures_js_1.DimensionalBaseMeasure.aggregationFromJAQL(jaql.agg || ''), undefined, undefined, sort);
103
+ }
104
+ return attribute;
105
+ }
106
+ /**
107
+ * Creates a calculated measure for a valid custom formula built from [base functions](https://docs.sisense.com/main/SisenseLinux/dashboard-functions-reference.htm).
108
+ *
109
+ * Use square brackets (`[]`) within the `formula` property to include dimensions or measures.
110
+ * Each unique dimension or measure included in the `formula` must be defined using a property:value pair in the `context` parameter.
111
+ *
112
+ * You can nest custom formulas by placing one inside the `formula` parameter of another.
113
+ *
114
+ * Note: To use [shared formulas](https://docs.sisense.com/main/SisenseLinux/shared-formulas.htm)
115
+ * from a Fusion Embed instance, you must fetch them first using {@link @sisense/sdk-ui!useGetSharedFormula | useGetSharedFormula}.
116
+ *
117
+ * @example
118
+ * An example of constructing a custom formulas using dimensions, measures, and nested custom formulas
119
+ * from the Sample Ecommerce data model.
120
+ * ```ts
121
+ * // Custom formula
122
+ * const profitabilityRatio = measureFactory.customFormula(
123
+ * 'Profitability Ratio',
124
+ * '([totalRevenue] - SUM([cost])) / [totalRevenue]',
125
+ * {
126
+ * totalRevenue: measureFactory.sum(DM.Commerce.Revenue),
127
+ * cost: DM.Commerce.Cost,
128
+ * },
129
+ * );
130
+ *
131
+ * // Nested custom formula
132
+ * const profitabilityRatioRank = measureFactory.customFormula(
133
+ * 'Profitability Ratio Rank',
134
+ * 'RANK([profRatio], "ASC", "1224")',
135
+ * {
136
+ * profRatio: profitabilityRatio,
137
+ * },
138
+ * );
139
+ * ```
140
+ * @param title - Title of the measure to be displayed in legend
141
+ * @param formula - Formula to be used for the measure
142
+ * @param context - Formula context as a map of strings to measures or attributes
143
+ * @returns A calculated measure instance
144
+ * @group Advanced Analytics
145
+ */
146
+ function customFormula(title, formula, context) {
147
+ const newContext = Object.entries(context).reduce((acc, [key, val]) => {
148
+ acc[`[${key}]`] = val.jaql().jaql;
149
+ return acc;
150
+ }, {});
151
+ return transformCustomFormulaJaql({ title, formula, context: newContext });
152
+ }
153
+ exports.customFormula = customFormula;
154
+ function arithmetic(operand1, operator, operand2, name, withParentheses) {
155
+ const builder = [];
156
+ const context = {};
157
+ if (withParentheses === true) {
158
+ builder.push('(');
159
+ }
160
+ addToFormula(builder, context, operand1);
161
+ builder.push(operator);
162
+ addToFormula(builder, context, operand2);
163
+ if (withParentheses === true) {
164
+ builder.push(')');
165
+ }
166
+ const exp = builder.join('');
167
+ return new measures_js_1.DimensionalCalculatedMeasure(name !== null && name !== void 0 ? name : exp, exp, context);
168
+ }
169
+ /**
170
+ * Creates an aggregated measure.
171
+ *
172
+ * This is a base function to build other aggregation functions (e.g., `sum`, `average`, etc.)
173
+ * as listed in {@link AggregationTypes}.
174
+ *
175
+ * @example
176
+ * Calculate the total cost across all items in a category from the Sample Ecommerce data model.
177
+ * ```ts
178
+ * measureFactory.aggregate(DM.Commerce.Cost, 'sum'),
179
+ * ```
180
+ * @param attribute - Attribute to aggregate
181
+ * @param aggregationType - Aggregation type. See {@link AggregationTypes}
182
+ * @param name - Optional name for the new measure
183
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
184
+ * @returns A measure instance
185
+ * @group Aggregation
186
+ */
187
+ function aggregate(attribute, aggregationType, name, format) {
188
+ return new measures_js_1.DimensionalBaseMeasure(name !== null && name !== void 0 ? name : `${aggregationType.toString()} ${attribute.name}`, attribute, aggregationType, format);
189
+ }
190
+ exports.aggregate = aggregate;
191
+ /**
192
+ * Creates a calculated measure from a numeric value.
193
+ *
194
+ * @example
195
+ * Creates a calculated measure from a numeric value.
196
+ * ```ts
197
+ * measureFactory.constant(42)
198
+ * ```
199
+ * @param value - Value to be returned as a measure
200
+ * @returns A calculated measure instance
201
+ * @group Arithmetic
202
+ */
203
+ function constant(value) {
204
+ return new measures_js_1.DimensionalCalculatedMeasure(`${value}`, `${value}`, {});
205
+ }
206
+ exports.constant = constant;
207
+ /**
208
+ * Creates a sum aggregation measure over the given attribute.
209
+ *
210
+ * To create a running sum, use {@link runningSum}.
211
+ *
212
+ * @example
213
+ * Calculate the total cost across all items in a category from the Sample Ecommerce data model.
214
+ * ```ts
215
+ * measureFactory.sum(DM.Commerce.Cost)
216
+ * ```
217
+ * @param attribute - Attribute to aggregate
218
+ * @param name - Optional name for the new measure
219
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
220
+ * @returns A measure instance
221
+ * @group Aggregation
222
+ */
223
+ function sum(attribute, name, format) {
224
+ return aggregate(attribute, types_js_1.AggregationTypes.Sum, name, format);
225
+ }
226
+ exports.sum = sum;
227
+ /**
228
+ * Creates an average aggregation measure over the given attribute.
229
+ *
230
+ * Both `average()` and `avg()` can be used interchangeably.
231
+ *
232
+ * @example
233
+ * Calculate the average cost across all items in a category from the Sample Ecommerce data model.
234
+ * ```ts
235
+ * measureFactory.average(DM.Commerce.Cost)
236
+ * ```
237
+ * @param attribute - Attribute to aggregate
238
+ * @param name - Optional name for the new measure
239
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
240
+ * @returns A measure instance
241
+ * @group Aggregation
242
+ */
243
+ function average(attribute, name, format) {
244
+ return aggregate(attribute, types_js_1.AggregationTypes.Average, name, format);
245
+ }
246
+ exports.average = average;
247
+ /**
248
+ * {@inheritDoc average}
249
+ *
250
+ * @example
251
+ * Calculate the average cost across all items in a category from the Sample Ecommerce data model.
252
+ * ```ts
253
+ * measureFactory.avg(DM.Commerce.Cost)
254
+ * ```
255
+ * @group Aggregation
256
+ */
257
+ function avg(attribute, name, format) {
258
+ return average(attribute, name, format);
259
+ }
260
+ exports.avg = avg;
261
+ /**
262
+ * Creates a min aggregation measure over the given attribute.
263
+ *
264
+ * @example
265
+ * Calculate the minimum cost across all items in a category from the Sample Ecommerce data model.
266
+ * ```ts
267
+ * measureFactory.min(DM.Commerce.Cost)
268
+ * ```
269
+ * @param attribute - Attribute to aggregate
270
+ * @param name - Optional name for the new measure
271
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
272
+ * @returns A measure instance
273
+ * @group Aggregation
274
+ */
275
+ function min(attribute, name, format) {
276
+ return aggregate(attribute, types_js_1.AggregationTypes.Min, name, format);
277
+ }
278
+ exports.min = min;
279
+ /**
280
+ * Creates a max aggregation measure over the given attribute.
281
+ *
282
+ * @example
283
+ * Calculate the maximum cost across all items in a category from the Sample Ecommerce data model.
284
+ * ```ts
285
+ * measureFactory.max(DM.Commerce.Cost)
286
+ * ```
287
+ * @param attribute - Attribute to aggregate
288
+ * @param name - Optional name for the new measure
289
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
290
+ * @returns A measure instance
291
+ * @group Aggregation
292
+ */
293
+ function max(attribute, name, format) {
294
+ return aggregate(attribute, types_js_1.AggregationTypes.Max, name, format);
295
+ }
296
+ exports.max = max;
297
+ /**
298
+ * Creates a median aggregation measure over the given attribute.
299
+ *
300
+ * @example
301
+ * Calculate the median cost across all items in a category from the Sample Ecommerce data model.
302
+ * ```ts
303
+ * measureFactory.median(DM.Commerce.Cost)
304
+ * ```
305
+ * @param attribute - Attribute to aggregate
306
+ * @param name - Optional name for the new measure
307
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
308
+ * @returns A measure instance
309
+ * @group Aggregation
310
+ */
311
+ function median(attribute, name, format) {
312
+ return aggregate(attribute, types_js_1.AggregationTypes.Median, name, format);
313
+ }
314
+ exports.median = median;
315
+ /**
316
+ * Creates a count aggregation measure over the given attribute.
317
+ *
318
+ * To count distinct values in the given attribute, use {@link countDistinct}.
319
+ *
320
+ * @example
321
+ * Counts the number of Commerce items from the Sample Ecommerce data model.
322
+ * ```ts
323
+ * measureFactory.count(DM.Commerce.BrandID)
324
+ * ```
325
+ * @param attribute - Attribute to aggregate
326
+ * @param name - Optional name for the new measure
327
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
328
+ * @returns A measure instance
329
+ * @group Aggregation
330
+ */
331
+ function count(attribute, name, format) {
332
+ return aggregate(attribute, types_js_1.AggregationTypes.Count, name, format);
333
+ }
334
+ exports.count = count;
335
+ /**
336
+ * Creates a count distinct aggregation measure over the given attribute.
337
+ *
338
+ * To count all values in the given attribute, use {@link count}.
339
+ *
340
+ * @example
341
+ * Calculate the number of distinct brands from the Sample Ecommerce data model.
342
+ * ```ts
343
+ * measureFactory.countDistinct(DM.Brand.BrandID)
344
+ * ```
345
+ * @param attribute - Attribute to aggregate
346
+ * @param name - Optional name for the new measure
347
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
348
+ * @returns A measure instance
349
+ * @group Aggregation
350
+ */
351
+ function countDistinct(attribute, name, format) {
352
+ return aggregate(attribute, types_js_1.AggregationTypes.CountDistinct, name, format);
353
+ }
354
+ exports.countDistinct = countDistinct;
355
+ /**
356
+ * Creates a measured value with the given measure and set of filters.
357
+ *
358
+ * A measured value only includes values from items that match the provided filters.
359
+ *
360
+ * For example you can use a measured value to get a total cost for all items where the cost is greater than 100.
361
+ *
362
+ * Note that the filters on the measured value override the filters on the query, chart, or table the
363
+ * measured value is used in.
364
+ *
365
+ * @example
366
+ * Calculate the total cost across all items in a category from the Sample Ecommerce data model,
367
+ * where the cost is greater than 100. Additional filtering on the cost will not affect this measure.
368
+ * ```ts
369
+ * measureFactory.measuredValue(
370
+ * measureFactory.sum(DM.Commerce.Cost),
371
+ * [filters.greaterThan(DM.Commerce.Cost, 100)],
372
+ * 'Cost Greater Than 100'
373
+ * ),
374
+ * ```
375
+ * @param measure - Measure to filter
376
+ * @param filters - Filters to apply to the measure
377
+ * @param name - Optional name for the new measure
378
+ * @param format - Optional numeric formatting to apply using a Numeral.js format string. Can only be used for explicit queries. Cannot be used in charts, tables, etc.
379
+ * @returns A calculated measure instance
380
+ * @group Advanced Analytics
381
+ */
382
+ function measuredValue(measure, filters, name, format) {
383
+ const builder = [];
384
+ const context = {};
385
+ builder.push('(');
386
+ addToFormula(builder, context, measure);
387
+ filters.forEach((f) => {
388
+ builder.push(',');
389
+ addToFormula(builder, context, f);
390
+ });
391
+ builder.push(')');
392
+ const exp = builder.join('');
393
+ return new measures_js_1.DimensionalCalculatedMeasure(name !== null && name !== void 0 ? name : exp, exp, context, format);
394
+ }
395
+ exports.measuredValue = measuredValue;
396
+ /**
397
+ * Creates a calculated measure by adding two given numbers or measures.
398
+ *
399
+ * @example
400
+ * ```ts
401
+ * const measure1 = measureFactory.sum(DM.Dimension.Attribute1);
402
+ * const measure2 = measureFactory.sum(DM.Dimension.Attribute2);
403
+ * const measureSum = measureFactory.add(measure1, measure2);
404
+ * ```
405
+ * @param value1 - First value
406
+ * @param value2 - Second value
407
+ * @param name - Optional name for the new measure
408
+ * @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
409
+ * @returns A calculated measure instance
410
+ * @group Arithmetic
411
+ */
412
+ function add(value1, value2, name, withParentheses) {
413
+ return arithmetic(value1, '+', value2, name, withParentheses);
414
+ }
415
+ exports.add = add;
416
+ /**
417
+ * Creates a calculated measure by subtracting two given numbers or measures. Subtracts `value2` from `value1`.
418
+ *
419
+ * @example
420
+ * ```ts
421
+ * const measure1 = measureFactory.sum(DM.Dimension.Attribute1);
422
+ * const measure2 = measureFactory.sum(DM.Dimension.Attribute2);
423
+ * const measureDifference = measureFactory.subtract(measure1, measure2);
424
+ * ```
425
+ * @param value1 - First value
426
+ * @param value2 - Second value
427
+ * @param name - Optional name for the new measure
428
+ * @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
429
+ * @returns A calculated measure instance
430
+ * @group Arithmetic
431
+ */
432
+ function subtract(value1, value2, name, withParentheses) {
433
+ return arithmetic(value1, '-', value2, name, withParentheses);
434
+ }
435
+ exports.subtract = subtract;
436
+ /**
437
+ * Creates a calculated measure by multiplying two given numbers or measures.
438
+ *
439
+ * @example
440
+ * ```ts
441
+ * const measure1 = measureFactory.sum(DM.Dimension.Attribute1);
442
+ * const measure2 = measureFactory.sum(DM.Dimension.Attribute2);
443
+ * const measureProduct = measureFactory.multiply(measure1, measure2);
444
+ * ```
445
+ * @param value1 - First value
446
+ * @param value2 - Second value
447
+ * @param name - Optional name for the new measure
448
+ * @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
449
+ * @returns A calculated measure instance
450
+ * @group Arithmetic
451
+ */
452
+ function multiply(value1, value2, name, withParentheses) {
453
+ return arithmetic(value1, '*', value2, name, withParentheses);
454
+ }
455
+ exports.multiply = multiply;
456
+ /**
457
+ * Creates a calculated measure by dividing two given numbers or measures. Divides `value1` by `value2`.
458
+ *
459
+ * @example
460
+ * ```ts
461
+ * const measure1 = measureFactory.sum(DM.Dimension.Attribute1);
462
+ * const measure2 = measureFactory.sum(DM.Dimension.Attribute2);
463
+ * const measureQuotient = measureFactory.divide(measure1, measure2);
464
+ * ```
465
+ * @param value1 - First value
466
+ * @param value2 - Second value
467
+ * @param name - Optional name for the new measure
468
+ * @param withParentheses - Optional boolean flag whether to wrap the arithmetic operation with parentheses
469
+ * @returns A calculated measure instance
470
+ * @group Arithmetic
471
+ */
472
+ function divide(value1, value2, name, withParentheses) {
473
+ return arithmetic(value1, '/', value2, name, withParentheses);
474
+ }
475
+ exports.divide = divide;
476
+ /**
477
+ * Creates a calculated measure that calculates the running total starting from the beginning
478
+ * of the year up to the current day, week, month, or quarter.
479
+ *
480
+ * The time resolution is determined by the minimum date level of the date dimension in the query
481
+ * that uses the returned measure.
482
+ *
483
+ * @example
484
+ * Calculate the running total of total cost from the Sample Ecommerce data model, starting from the
485
+ * beginning of the year up to the current day, week, month, or quarter.
486
+ * ```ts
487
+ * measureFactory.yearToDateSum(measureFactory.sum(DM.Commerce.Cost))
488
+ * ```
489
+ * @param measure - Measure to apply the YTD Sum to
490
+ * @param name - Name for the new measure
491
+ * @returns A calculated measure instance
492
+ * @group Time-based
493
+ */
494
+ function yearToDateSum(measure, name) {
495
+ return measureFunction(measure, name !== null && name !== void 0 ? name : 'YTD ' + measure.name, 'YTDSum');
496
+ }
497
+ exports.yearToDateSum = yearToDateSum;
498
+ /**
499
+ * Creates a calculated measure that calculates the running total starting from the beginning
500
+ * of the quarter up to the current day, week, or month.
501
+ *
502
+ * The time resolution is determined by the minimum date level of the date dimension in the query
503
+ * that uses the returned measure.
504
+ *
505
+ * @example
506
+ * Calculate the running total of total cost from the Sample Ecommerce data model, starting from the
507
+ * beginning of the quarter up to the current day, week, or month.
508
+ * ```ts
509
+ * measureFactory.quarterToDateSum(measureFactory.sum(DM.Commerce.Cost))
510
+ * ```
511
+ * @param measure - Measure to apply the QTD Sum to
512
+ * @param name - Name for the new measure
513
+ * @returns A calculated measure instance
514
+ * @group Time-based
515
+ */
516
+ function quarterToDateSum(measure, name) {
517
+ return measureFunction(measure, name !== null && name !== void 0 ? name : 'QTD ' + name, 'QTDSum');
518
+ }
519
+ exports.quarterToDateSum = quarterToDateSum;
520
+ /**
521
+ * Creates a calculated measure that calculates the running total starting from the beginning
522
+ * of the month up to the current day or week.
523
+ *
524
+ * The time resolution is determined by the minimum date level of the date dimension in the query
525
+ * that uses the returned measure.
526
+ *
527
+ * @example
528
+ * Calculate the running total of total cost from the Sample Ecommerce data model, starting from the
529
+ * beginning of the month up to the current day or week.
530
+ * ```ts
531
+ * measureFactory.monthToDateSum(measureFactory.sum(DM.Commerce.Cost))
532
+ * ```
533
+ * @param measure - Measure to apply the MTD Sum to
534
+ * @param name - Name for the new measure
535
+ * @returns A calculated measure instance
536
+ * @group Time-based
537
+ */
538
+ function monthToDateSum(measure, name) {
539
+ return measureFunction(measure, name !== null && name !== void 0 ? name : 'MTD ' + measure.name, 'MTDSum');
540
+ }
541
+ exports.monthToDateSum = monthToDateSum;
542
+ /**
543
+ * Creates a calculated measure that calculates the running total starting from the beginning
544
+ * of the week up to the current day.
545
+ *
546
+ * The time resolution is determined by the minimum date level of the date dimension in the query
547
+ * that uses the returned measure.
548
+ *
549
+ * @example
550
+ * Calculate the running total of total cost from the Sample Ecommerce data model, starting from the
551
+ * beginning of the week up to the current day.
552
+ * ```ts
553
+ * measureFactory.weekToDateSum(measureFactory.sum(DM.Commerce.Cost))
554
+ * ```
555
+ * @param measure - Measure to apply the WTD Sum to
556
+ * @param name - Name for the new measure
557
+ * @returns A calculated measure instance
558
+ * @group Time-based
559
+ */
560
+ function weekToDateSum(measure, name) {
561
+ return measureFunction(measure, name !== null && name !== void 0 ? name : 'MTD ' + measure.name, 'WTDSum');
562
+ }
563
+ exports.weekToDateSum = weekToDateSum;
564
+ /**
565
+ * Creates a calculated measure that calculates the running total of a given measure.
566
+ *
567
+ * The running sum is calculated using the current sort order of the query it's used in.
568
+ *
569
+ * @example
570
+ * Calculate the running sum of the total cost from the Sample Ecommerce data model across all categories.
571
+ * ```ts
572
+ * measureFactory.runningSum(measureFactory.sum(DM.Commerce.Cost)),
573
+ * ```
574
+ *
575
+ * Running sum values from the Sample Ecommerce data model when categorizing by age range.
576
+ * | AgeRange | Cost | Running Cost |
577
+ * | --- | --- | --- |
578
+ * | 0-18 | 4.32M | 4.32M |
579
+ * | 19-24 | 8.66M | 12.98M |
580
+ * | 25-34 | 21.19M | 34.16M |
581
+ * | 35-44 | 23.64M | 57.8M |
582
+ * | 45-54 | 20.39M | 78.19M |
583
+ * | 55-64 | 11.82M | 90.01M |
584
+ * | 65+ | 17.26M | 107.27M |
585
+ * @param measure - Measure to apply the running sum to
586
+ * @param _continuous - Boolean flag whether to accumulate the sum continuously
587
+ * when there are two or more dimensions. The default value is false.
588
+ * @param name - Name for the new measure
589
+ * @returns A calculated measure instance
590
+ * @group Statistics
591
+ */
592
+ function runningSum(measure, _continuous, name) {
593
+ return measureFunction(measure, name !== null && name !== void 0 ? name : 'Running Sum ' + measure.name, 'RSum');
594
+ }
595
+ exports.runningSum = runningSum;
596
+ /**
597
+ * Creates a calculated measure that calculates growth over a period of time.
598
+ *
599
+ * The time resolution is determined by the minimum date level of the date dimension in the query
600
+ * that uses the returned measure.
601
+ *
602
+ * Growth is calculated using the following formula: `(currentPeriod – previousPeriod) / previousPeriod`.
603
+ *
604
+ * For example, if this period the value is 12 and the previous period's value was 10, the growth for
605
+ * this period is 20%, returned as '0.2' (calculation: `(12 – 10) / 10 = 0.2`).
606
+ *
607
+ * If the previous period's value is greater than the current period, the growth will be negative.
608
+ *
609
+ * For example, if this period the value is 80, and the previous period's was 100, the growth for
610
+ * this period is -20%, returned as `-0.2` (calculation: `(80 – 100) / 100 = -0.2`).
611
+ *
612
+ * @example
613
+ * Calculate the growth in total cost this period in comparison to the previous period from the
614
+ * Sample Ecommerce data model.
615
+ * ```ts
616
+ * measureFactory.growth(measureFactory.sum(DM.Commerce.Cost))
617
+ * ```
618
+ * @param measure - Measure to apply growth to
619
+ * @param name - Name for the new measure
620
+ * @returns A calculated measure instance
621
+ * @group Statistics
622
+ */
623
+ function growth(measure, name) {
624
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growth');
625
+ }
626
+ exports.growth = growth;
627
+ /**
628
+ * Creates a calculated measure that calculates growth rate over a period of time.
629
+ *
630
+ * The time resolution is determined by the minimum date level of the date dimension in the query
631
+ * that uses the returned measure.
632
+ *
633
+ * Growth rate is calculated using the following formula: `currentPeriod / previousPeriod`.
634
+ *
635
+ * For example, if this period the value is 12 and the previous period's value was 10, the growth rate for
636
+ * this period is 120%, returned as '1.2' (calculation: `12 / 10 = 1.2`).
637
+ *
638
+ * If the previous period's value is greater than the current period, the growth rate will be less than one.
639
+ *
640
+ * For example, if this period the value is 80, and the previous period's was 100, the growth for
641
+ * this period is 80%, returned as `0.8` (calculation: `80 / 100 = .8`).
642
+ *
643
+ * @example
644
+ * Calculate the growth rate in total cost this period in comparison to the previous period from the
645
+ * Sample Ecommerce data model.
646
+ * ```ts
647
+ * measureFactory.growthRate(measureFactory.sum(DM.Commerce.Cost))
648
+ * ```
649
+ * @param measure - Measure to apply the Growth rate
650
+ * @param name - Name for the new measure
651
+ * @returns A calculated measure instance
652
+ * @group Statistics
653
+ */
654
+ function growthRate(measure, name) {
655
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthrate');
656
+ }
657
+ exports.growthRate = growthRate;
658
+ /**
659
+ * Creates a calculated measure that calculates the growth from the previous week to the current week.
660
+ *
661
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
662
+ *
663
+ * Growth is calculated using the following formula: `(currentWeek – previousWeek) / previousWeek`.
664
+ *
665
+ * For example, if this week the value is 12 and the previous week's value was 10, the growth for
666
+ * this week is 20%, returned as '0.2' (calculation: `(12 – 10) / 10 = 0.2`).
667
+ *
668
+ * If the previous week's value is greater than the current week, the growth will be negative.
669
+ *
670
+ * For example, if this week the value is 80, and the previous week's was 100, the growth for
671
+ * this week is -20%, returned as `-0.2` (calculation: `(80 – 100) / 100 = -0.2`).
672
+ *
673
+ * @example
674
+ * Calculate the growth in total cost this week in comparison to the previous week from the Sample
675
+ * Ecommerce data model.
676
+ * ```ts
677
+ * measureFactory.growthPastWeek(measureFactory.sum(DM.Commerce.Cost))
678
+ * ```
679
+ * @param measure - Measure to apply growth to
680
+ * @param name - Name for the new measure
681
+ * @returns A calculated measure instance
682
+ * @group Statistics
683
+ */
684
+ function growthPastWeek(measure, name) {
685
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastweek');
686
+ }
687
+ exports.growthPastWeek = growthPastWeek;
688
+ /**
689
+ * Creates a calculated measure that calculates the growth from the previous month to the current month.
690
+ *
691
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
692
+ *
693
+ * Growth is calculated using the following formula: `(currentMonth – previousMonth) / previousMonth`.
694
+ *
695
+ * For example, if this month the value is 12 and the previous month's value was 10, the growth for
696
+ * this month is 20%, returned as '0.2' (calculation: `(12 – 10) / 10 = 0.2`).
697
+ *
698
+ * If the previous month's value is greater than the current month, the growth will be negative.
699
+ *
700
+ * For example, if this month the value is 80, and the previous month's was 100, the growth for
701
+ * this month is -20%, returned as `-0.2` (calculation: `(80 – 100) / 100 = -0.2`).
702
+ *
703
+ * @example
704
+ * Calculate the growth in total cost this month in comparison to the previous month from the Sample
705
+ * Ecommerce data model.
706
+ * ```ts
707
+ * measureFactory.growthPastMonth(measureFactory.sum(DM.Commerce.Cost))
708
+ * ```
709
+ * @param measure - Measure to apply growth to
710
+ * @param name - Name for the new measure
711
+ * @returns A calculated measure instance
712
+ * @group Statistics
713
+ */
714
+ function growthPastMonth(measure, name) {
715
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastmonth');
716
+ }
717
+ exports.growthPastMonth = growthPastMonth;
718
+ /**
719
+ * Creates a calculated measure that calculates the growth from the previous quarter to the current quarter.
720
+ *
721
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
722
+ *
723
+ * Growth is calculated using the following formula: `(currentQuarter – previousQuarter) / previousQuarter`.
724
+ *
725
+ * For example, if this quarter the value is 12 and the previous quarter's value was 10, the growth for
726
+ * this quarter is 20%, returned as '0.2' (calculation: `(12 – 10) / 10 = 0.2`).
727
+ *
728
+ * If the previous quarter's value is greater than the current quarter, the growth will be negative.
729
+ *
730
+ * For example, if this quarter the value is 80, and the previous quarter's was 100, the growth for
731
+ * this quarter is -20%, returned as `-0.2` (calculation: `(80 – 100) / 100 = -0.2`).
732
+ *
733
+ * @example
734
+ * Calculate the growth in total cost this quarter in comparison to the previous quarter from the
735
+ * Sample Ecommerce data model.
736
+ * ```ts
737
+ * measureFactory.growthPastQuarter(measureFactory.sum(DM.Commerce.Cost))
738
+ * ```
739
+ * @param measure - Measure to apply growth to
740
+ * @param name - Name for the new measure
741
+ * @returns A calculated measure instance
742
+ * @group Statistics
743
+ */
744
+ function growthPastQuarter(measure, name) {
745
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastquarter');
746
+ }
747
+ exports.growthPastQuarter = growthPastQuarter;
748
+ /**
749
+ * Creates a calculated measure that calculates the growth from the previous year to the current year.
750
+ *
751
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
752
+ *
753
+ * Growth is calculated using the following formula: `(currentYear – previousYear) / previousYear`.
754
+ *
755
+ * For example, if this year the value is 12 and the previous year's value was 10, the growth for
756
+ * this year is 20%, returned as '0.2' (calculation: `(12 – 10) / 10 = 0.2`).
757
+ *
758
+ * If the previous year's value is greater than the current year, the growth will be negative.
759
+ *
760
+ * For example, if this year the value is 80, and the previous year's was 100, the growth for
761
+ * this year is -20%, returned as `-0.2` (calculation: `(80 – 100) / 100 = -0.2`).
762
+ *
763
+ * @example
764
+ * Calculate the growth in total cost this year in comparison to the previous year from the Sample
765
+ * Ecommerce data model.
766
+ * ```ts
767
+ * measureFactory.growthPastYear(measureFactory.sum(DM.Commerce.Cost))
768
+ * ```
769
+ * @param measure - Measure to apply growth to
770
+ * @param name - Name for the new measure
771
+ * @returns A calculated measure instance
772
+ * @group Statistics
773
+ */
774
+ function growthPastYear(measure, name) {
775
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Growth', 'growthpastyear');
776
+ }
777
+ exports.growthPastYear = growthPastYear;
778
+ /**
779
+ * Creates a calculated measure that calculates the difference between this period's data
780
+ * and the data from the previous period for the given measure.
781
+ *
782
+ * The time resolution is determined by the minimum date level of the date dimension in the query
783
+ * that uses the returned measure.
784
+ *
785
+ * @example
786
+ * Calculate the difference between this period's total cost and the previous period's total cost
787
+ * from the Sample Ecommerce data model.
788
+ * ```ts
789
+ * measureFactory.difference(measureFactory.sum(DM.Commerce.Cost))
790
+ * ```
791
+ * @param measure - Measure to apply difference to
792
+ * @param name - Name for the new measure
793
+ * @returns A calculated measure instance
794
+ * @group Time-based
795
+ */
796
+ function difference(measure, name) {
797
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastperiod');
798
+ }
799
+ exports.difference = difference;
800
+ /**
801
+ * Creates a calculated measure that calculates the difference between this week's data
802
+ * and the data from the previous week for the given measure.
803
+ *
804
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
805
+ *
806
+ * @example
807
+ * Calculate the difference between this week's total cost and the previous week's total cost from
808
+ * the Sample Ecommerce data model.
809
+ * ```ts
810
+ * measureFactory.diffPastWeek(measureFactory.sum(DM.Commerce.Cost))
811
+ * ```
812
+ * @param measure - Measure to apply difference to
813
+ * @param name - Name for the new measure
814
+ * @returns A calculated measure instance
815
+ * @group Time-based
816
+ */
817
+ function diffPastWeek(measure, name) {
818
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastweek');
819
+ }
820
+ exports.diffPastWeek = diffPastWeek;
821
+ /**
822
+ * Creates a calculated measure that calculates the difference between this month's data
823
+ * and the data from the previous month for the given measure.
824
+ *
825
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
826
+ *
827
+ * @example
828
+ * Calculate the difference between this month's total cost and the previous month's total cost from
829
+ * the Sample Ecommerce data model.
830
+ * ```ts
831
+ * measureFactory.diffPastMonth(measureFactory.sum(DM.Commerce.Cost))
832
+ * ```
833
+ * @param measure - Measure to apply difference to
834
+ * @param name - Name for the new measure
835
+ * @returns A calculated measure instance
836
+ * @group Time-based
837
+ */
838
+ function diffPastMonth(measure, name) {
839
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastmonth');
840
+ }
841
+ exports.diffPastMonth = diffPastMonth;
842
+ /**
843
+ * Creates a calculated measure that calculates the difference between this quarter's data
844
+ * and the data from the previous quarter for the given measure.
845
+ *
846
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
847
+ *
848
+ * @example
849
+ * Calculate the difference between this quarter's total cost and the previous quarter's total cost
850
+ * from the Sample Ecommerce data model.
851
+ * ```ts
852
+ * measureFactory.diffPastQuarter(measureFactory.sum(DM.Commerce.Cost))
853
+ * ```
854
+ * @param measure - Measure to apply difference to
855
+ * @param name - Name for the new measure
856
+ * @returns A calculated measure instance
857
+ * @group Time-based
858
+ */
859
+ function diffPastQuarter(measure, name) {
860
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastquarter');
861
+ }
862
+ exports.diffPastQuarter = diffPastQuarter;
863
+ /**
864
+ * Creates a calculated measure that calculates the difference between this year's data
865
+ * and the data from the previous year for the given measure.
866
+ *
867
+ * The date dimension will be dynamically taken from the query that uses the returned measure.
868
+ *
869
+ * @example
870
+ * Calculate the difference between this year's total cost and the previous year's total cost from
871
+ * the Sample Ecommerce data model.
872
+ * ```ts
873
+ * measureFactory.diffPastYear(measureFactory.sum(DM.Commerce.Cost))
874
+ * ```
875
+ * @param measure - Measure to apply difference to
876
+ * @param name - Name for the new measure
877
+ * @returns A calculated measure instance
878
+ * @group Time-based
879
+ */
880
+ function diffPastYear(measure, name) {
881
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Difference', 'diffpastyear');
882
+ }
883
+ exports.diffPastYear = diffPastYear;
884
+ /**
885
+ * Creates a calculated measure that calculates the value for the previous day.
886
+ *
887
+ * @example
888
+ * Calculate total cost for the previous day from the Sample Ecommerce data model.
889
+ * ```ts
890
+ * measureFactory.pastDay(measureFactory.sum(DM.Commerce.Cost))
891
+ * ```
892
+ * @param measure - Measure to apply past value to
893
+ * @param name - Name for the new measure
894
+ * @returns A calculated measure instance
895
+ * @group Time-based
896
+ */
897
+ function pastDay(measure, name) {
898
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Day', 'pastday');
899
+ }
900
+ exports.pastDay = pastDay;
901
+ /**
902
+ * Creates a calculated measure that calculates the value for the same day in the previous week.
903
+ *
904
+ * The time resolution is determined by the minimum date level of the date dimension in the query
905
+ * that uses the returned measure.
906
+ *
907
+ * @example
908
+ * Calculate total cost for the corresponding day one week ago from the Sample Ecommerce data model.
909
+ * ```ts
910
+ * measureFactory.pastWeek(measureFactory.sum(DM.Commerce.Cost))
911
+ * ```
912
+ * @param measure - Measure to apply past value to
913
+ * @param name - Name for the new measure
914
+ * @returns A calculated measure instance
915
+ * @group Time-based
916
+ */
917
+ function pastWeek(measure, name) {
918
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Week', 'pastweek');
919
+ }
920
+ exports.pastWeek = pastWeek;
921
+ /**
922
+ * Creates a calculated measure that calculates the value for the same day or week in the previous month.
923
+ *
924
+ * The time resolution is determined by the minimum date level of the date dimension in the query
925
+ * that uses the returned measure.
926
+ *
927
+ * @example
928
+ * Calculate total cost for the corresponding day or week one month ago from the Sample Ecommerce
929
+ * data model.
930
+ * ```ts
931
+ * measureFactory.pastMonth(measureFactory.sum(DM.Commerce.Cost))
932
+ * ```
933
+ * @param measure - Measure to apply past value to
934
+ * @param name - Name for the new measure
935
+ * @returns A calculated measure instance
936
+ * @group Time-based
937
+ */
938
+ function pastMonth(measure, name) {
939
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Month', 'pastmonth');
940
+ }
941
+ exports.pastMonth = pastMonth;
942
+ /**
943
+ * Creates a calculated measure that calculates the value for the same day, week, or month in the previous quarter.
944
+ *
945
+ * The time resolution is determined by the minimum date level of the date dimension in the query
946
+ * that uses the returned measure.
947
+ *
948
+ * @example
949
+ * Calculate total cost for the corresponding day, week, or month one quarter ago from the Sample
950
+ * Ecommerce data model.
951
+ * ```ts
952
+ * measureFactory.pastQuarter(measureFactory.sum(DM.Commerce.Cost))
953
+ * ```
954
+ * @param measure - Measure to apply past value to
955
+ * @param name - Name for the new measure
956
+ * @returns A calculated measure instance
957
+ * @group Time-based
958
+ */
959
+ function pastQuarter(measure, name) {
960
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Quarter', 'pastquarter');
961
+ }
962
+ exports.pastQuarter = pastQuarter;
963
+ /**
964
+ * Creates a calculated measure that calculates the value for the same day, week, month, or quarter in the previous year.
965
+ *
966
+ * The time resolution is determined by the minimum date level of the date dimension in the query
967
+ * that uses the returned measure.
968
+ *
969
+ * @example
970
+ * Calculate total cost for the corresponding day, week, month, or quarter one year ago from the
971
+ * Sample Ecommerce data model.
972
+ * ```ts
973
+ * measureFactory.pastYear(measureFactory.sum(DM.Commerce.Cost))
974
+ * ```
975
+ * @param measure - Measure to apply past value to
976
+ * @param name - Name for the new measure
977
+ * @returns A calculated measure instance
978
+ * @group Time-based
979
+ */
980
+ function pastYear(measure, name) {
981
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Past Year', 'pastyear');
982
+ }
983
+ exports.pastYear = pastYear;
984
+ /**
985
+ * Creates a calculated contribution measure.
986
+ *
987
+ * A contribution measure calculates the contribution, in percentage, of a measure towards the total.
988
+ * Percentages are expressed as a number between 0 and 1 (e.g. 42% is `0.42`).
989
+ *
990
+ * For example, using the Sample Ecommerce data model you can retrieve the total cost of products
991
+ * categorized by age range. Using a contribution measure you can calculate how much each age range's
992
+ * total cost contributes to the total cost across all age ranges. So, the total cost for the 35-44
993
+ * age range is 23.64M, which is 22% of the 107.27M of all age ranges together. Therefore, the
994
+ * contribution of the 35-44 age range is `.22`.
995
+ *
996
+ * @example
997
+ * Calculates the percentage of the total cost across all categories for items in a category from the
998
+ * Sample Ecommerce data model.
999
+ * ```ts
1000
+ * measureFactory.contribution(measureFactory.sum(DM.Commerce.Cost))
1001
+ * ```
1002
+ *
1003
+ * Contribution values from the Sample Ecommerce data model when categorizing by age range.
1004
+ * | AgeRange | Cost | Contribution |
1005
+ * | --- | --- | --- |
1006
+ * | 0-18 | 4.32M | 0.04 |
1007
+ * | 19-24 | 8.66M | 0.08 |
1008
+ * | 25-34 | 21.19M | 0.2 |
1009
+ * | 35-44 | 23.64M | 0.22 |
1010
+ * | 45-54 | 20.39M | 0.19 |
1011
+ * | 55-64 | 11.82M | 0.11 |
1012
+ * | 65+ | 17.26M | 0.16 |
1013
+ * @param measure - Measure to apply the Contribution logic to
1014
+ * @param name - Name for the new measure
1015
+ * @returns A calculated measure instance
1016
+ * @group Statistics
1017
+ */
1018
+ function contribution(measure, name) {
1019
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Contribution', 'contribution');
1020
+ }
1021
+ exports.contribution = contribution;
1022
+ /**
1023
+ * Creates a calculated measure that computes a specified trend type for a given measure.
1024
+ *
1025
+ * The trend types include linear (the default), logarithmic, advanced smoothing, and local estimates.
1026
+ * You can also opt to automatically identify and ignore anomalous values in the series.
1027
+ *
1028
+ * Trend requires a Sisense instance version of L2023.6.0 or greater.
1029
+ *
1030
+ * @example
1031
+ * Calculate the trend in total cost from the Sample Ecommerce data model.
1032
+ * ```ts
1033
+ * measureFactory.trend(
1034
+ * measureFactory.sum(DM.Commerce.Cost),
1035
+ * 'Total Cost Trend',
1036
+ * {
1037
+ * modelType: 'advancedSmoothing',
1038
+ * ignoreAnomalies: true,
1039
+ * }
1040
+ * )
1041
+ * ```
1042
+ * @param measure - Measure to apply the trend logic to
1043
+ * @param name - Name for the new measure
1044
+ * @param options - Trend options
1045
+ * @returns A calculated measure instance
1046
+ * @group Advanced Analytics
1047
+ */
1048
+ function trend(measure, name, options) {
1049
+ let params;
1050
+ const adjustValues = (value) => value.replace('advancedSmoothing', 'smooth').replace('localEstimates', 'local');
1051
+ if (options) {
1052
+ // make a comma separated name=value string based on options
1053
+ params = Object.entries(options)
1054
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1055
+ .map((e) => adjustValues(`"${e[0]}=${e[1]}"`))
1056
+ .join(',');
1057
+ }
1058
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Trend', 'trend', params);
1059
+ }
1060
+ exports.trend = trend;
1061
+ /**
1062
+ * Creates a calculated measure that generates a forecast based on a specified measure employing
1063
+ * advanced autoML techniques.
1064
+ *
1065
+ * This function offers flexible options allowing you to:
1066
+ * + Let the function auto-select the best statistical model or explicitly choose a preferred model
1067
+ * + Control the time period used for training the model
1068
+ * + Set additional options to improve forecast accuracy by supplying expected lower and upper limits.
1069
+ *
1070
+ * In addition to the forecast, upper and lower confidence intervals are returned
1071
+ * with the name of the new measure and a suffix of _upper and _lower
1072
+ * respectively.
1073
+ *
1074
+ * Forecast requires a Sisense instance version of L2023.6.0 or greater.
1075
+ *
1076
+ * @example
1077
+ * Calculate a forecast for total cost from the Sample Ecommerce data model from the Sample
1078
+ * Ecommerce data model.
1079
+ * ```ts
1080
+ * measureFactory.forecast(
1081
+ * measureFactory.sum(DM.Commerce.Cost),
1082
+ * 'Total Cost Forecast',
1083
+ * {
1084
+ * modelType: 'prophet',
1085
+ * roundToInt: true,
1086
+ * }
1087
+ * )
1088
+ * ```
1089
+ * @param measure - Measure to apply the forecast logic to
1090
+ * @param name - Name for the new measure
1091
+ * @param options - Forecast options
1092
+ * @returns A calculated measure instance
1093
+ * @group Advanced Analytics
1094
+ */
1095
+ function forecast(measure, name, options) {
1096
+ let params;
1097
+ if (options) {
1098
+ // create ISO string values for any Date objects
1099
+ const adjustedOptions = Object.assign({ forecastHorizon: 3 }, options);
1100
+ if (adjustedOptions.startDate) {
1101
+ const startDate = new Date(adjustedOptions.startDate);
1102
+ adjustedOptions.startDate = startDate.toISOString().replace(/.\d+Z$/g, '');
1103
+ }
1104
+ if (adjustedOptions.endDate) {
1105
+ const endDate = new Date(adjustedOptions.endDate);
1106
+ adjustedOptions.endDate = endDate.toISOString().replace(/.\d+Z$/g, '');
1107
+ }
1108
+ // make a comma separated name=value string based on options
1109
+ params = Object.entries(adjustedOptions)
1110
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1111
+ .map((e) => `"${e[0]}=${e[1]}"`)
1112
+ .join(',');
1113
+ }
1114
+ else {
1115
+ params = '"forecastHorizon=3"';
1116
+ }
1117
+ return measureFunction(measure, name !== null && name !== void 0 ? name : measure.name + ' Forecast', 'forecast', params);
1118
+ }
1119
+ exports.forecast = forecast;
1120
+ /**
1121
+ * Creates a calculated measure that calculates the rank of a value in a list of values.
1122
+ *
1123
+ * This function includes options that allow you do customize the ranking order and how to handle
1124
+ * equally ranked items.
1125
+ *
1126
+ * The order options are:
1127
+ * + `'DESC'` (default): Descending, meaning the largest number is ranked first.
1128
+ * + `'ASC'`: Ascending, meaning the smallest number is ranked first.
1129
+ *
1130
+ * The rank type options are:
1131
+ * + `'1224'`: Standard competition, meaning items that rank equally receive the same ranking number,
1132
+ * and then a gap is left after the equally ranked items in the ranking numbers.
1133
+ * + `'1334'`: Modified competition ranking, meaning items that rank equally receive the same ranking number,
1134
+ * and a gap is left before the equally ranked items in the ranking numbers.
1135
+ * + `'1223'`: Dense ranking, meaning items that rank equally receive the same ranking number,
1136
+ * and the next items receive the immediately following ranking number.
1137
+ * + `'1234'`: Ordinal ranking, meaning all items receive distinct ordinal numbers,
1138
+ * including items that rank equally. The assignment of distinct ordinal numbers for equal-ranking items is arbitrary.
1139
+ *
1140
+ * @example
1141
+ * Calculate the rank of the total cost per category, sorted with the smallest cost ranked first,
1142
+ * and ranking ties are handled using the ordinal ranking type from the Sample Ecommerce data model.
1143
+ * ```ts
1144
+ * measureFactory.rank(
1145
+ * measureFactory.sum(DM.Commerce.Cost),
1146
+ * 'Cost Rank',
1147
+ * measureFactory.RankingSortTypes.Ascending,
1148
+ * measureFactory.RankingTypes.Ordinal
1149
+ * )
1150
+ * ```
1151
+ *
1152
+ * Ranking values from the Sample Ecommerce data model when categorizing by age range using the above ranking.
1153
+ * | AgeRange | Cost | Cost Rank |
1154
+ * | --- | --- | --- |
1155
+ * | 0-18 | 4.32M | 1 |
1156
+ * | 19-24 | 8.66M | 2 |
1157
+ * | 25-34 | 21.19M | 6 |
1158
+ * | 35-44 | 23.64M | 7 |
1159
+ * | 45-54 | 20.39M | 5 |
1160
+ * | 55-64 | 11.82M | 3 |
1161
+ * | 65+ | 17.26M | 4 |
1162
+ * @param measure - Measure to apply the ranking logic to
1163
+ * @param name - Name for the new measure
1164
+ * @param sort - Sorting for ranking. By default sort order is descending, where the largest number is ranked first.
1165
+ * @param rankType - How to handle equally ranked items. By default the type is standard competition ranking.
1166
+ * @param groupBy - Rank partition attributes
1167
+ * @returns A calculated measure instance
1168
+ * @group Statistics
1169
+ */
1170
+ function rank(measure, name, sort = exports.RankingSortTypes.Descending, rankType = exports.RankingTypes.StandardCompetition, groupBy = []) {
1171
+ const builder = [];
1172
+ const context = {};
1173
+ builder.push('rank(');
1174
+ addToFormula(builder, context, measure);
1175
+ builder.push(`,${sort},${rankType}`);
1176
+ groupBy.forEach((groupByAttr) => {
1177
+ builder.push(',');
1178
+ addToFormula(builder, context, groupByAttr);
1179
+ });
1180
+ builder.push(')');
1181
+ const exp = builder.join('');
1182
+ // default name
1183
+ if (!name) {
1184
+ name = `${measure.name} rank`;
1185
+ }
1186
+ return new measures_js_1.DimensionalCalculatedMeasure(name, exp, context);
1187
+ }
1188
+ exports.rank = rank;