-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathCreate measures with a calculation group.csx
More file actions
255 lines (198 loc) · 9.44 KB
/
Copy pathCreate measures with a calculation group.csx
File metadata and controls
255 lines (198 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#r "Microsoft.VisualBasic"
using Microsoft.VisualBasic;
using System.Windows.Forms;
/* '2022-06-13 / B.Agullo / */
/* '2022-09-17 / B.Agullo / possibility to create a Field Parameter with a column for the base measure & calc Item
/* CREATE MEASURES WITH BASE MEASURES AND A CALCULATION GROUP */
/* See: https://www.esbrina-ba.com/creating-well-formatted-measures-based-on-a-calculation-group/ */
/* See: https://www.esbrina-ba.com/a-dynamic-legend-for-a-dynamic-measure-time-intel-chart/
/* select measures and execute, you will need to run it twice */
/* first time to create aux calc group, second time to actually create measuree*/
/* remove aux calc group before going to production, do the right thing */
string auxCgTag = "@AgulloBernat";
string auxCgTagValue = "CG to extract format strings";
string auxCalcGroupName = "DELETE AUX CALC GROUP";
string auxCalcItemName = "Get Format String";
string baseMeasureAnnotationName = "Base Measure";
string calcItemAnnotationName = "Calc Item";
string calcItemSortOrderName = "Sort Order";
string calcItemSortOrderValue = String.Empty;
string scriptAnnotationName = "Script";
string scriptAnnotationValue = "Create Measures with a Calculation Group";
bool generateFieldParameter;
DialogResult dialogResult = MessageBox.Show("Generate Field Parameter?", "Field Parameter", MessageBoxButtons.YesNo);
generateFieldParameter = (dialogResult == DialogResult.Yes);
/*find any regular CGs (excluding the one we might have created)*/
var regularCGs = Model.Tables.Where(
x => x.ObjectType == ObjectType.CalculationGroupTable
& x.GetAnnotation(auxCgTag) != auxCgTagValue);
if (regularCGs.Count() == 0)
{
Error("No Calculation Groups Found");
return;
};
/*check if we already created the auxiliary calculation group*/
var auxCgs = Model.Tables.Where(x => x.GetAnnotation(auxCgTag) == auxCgTagValue);
CalculationGroupTable auxCg = null as CalculationGroupTable;
/*if there are more than one for some reason we'll just use the first one*/
if(auxCgs.Count() >= 1)
{
auxCg = auxCgs.First() as CalculationGroupTable;
} else
{
/*create the aux calc group and ask for a refresh since it cannot be used in a query before that*/
auxCg = Model.AddCalculationGroup(name: auxCalcGroupName);
auxCg.AddCalculationItem(name: auxCalcItemName, expression: "SELECTEDMEASUREFORMATSTRING()");
auxCg.SetAnnotation(auxCgTag, auxCgTagValue);
/*better hidden in case someone forgets to delete it*/
auxCg.IsHidden = true;
int maxPrecedence = 0;
/*check for the max precedence of other calc groups*/
foreach (CalculationGroupTable cg in regularCGs)
{
if (cg.CalculationGroupPrecedence > maxPrecedence)
{
maxPrecedence = cg.CalculationGroupPrecedence;
};
};
/*assign the highest precedence and some margin*/
auxCg.CalculationGroupPrecedence = maxPrecedence + 10;
Info("Save changes to the model, recalculate the model, and launch the script again.");
return;
};
/*check if any measures are selected*/
if (Selected.Measures.Count == 0)
{
Error("No measures selected");
return;
}
CalculationGroupTable regularCg = null as CalculationGroupTable;
/*allow user to select calculation group if more than one is found*/
if (regularCGs.Count() > 1)
{
regularCg = SelectTable(regularCGs) as CalculationGroupTable;
}
/*otherwise just pick the first (and only)*/
else
{
regularCg = regularCGs.First() as CalculationGroupTable;
}
/*check if no selection was made*/
if(regularCg == null)
{
Error("No Target Calculation Group selected");
return;
};
string name = String.Empty;
if(generateFieldParameter) {
name = Interaction.InputBox("Provide a name for the field parameter", "Field Parameter", regularCg.Name + " Measures", 740, 400);
if(name == "") {Error("Execution Aborted"); return;};
};
MeasureCollection measures;
/*iterates through each selected measure*/
foreach (Measure m in Selected.Measures)
{
/*check that base measure has a proper format string*/
if(m.FormatString == "") {
Error("Define FormatString for " + m.Name + " and try again");
return;
};
/*prepares a displayfolder to store all new measures*/
string displayFolderName = m.Name + " Measures";
/*iterates thorough all calculation items of the selected calc group*/
foreach (CalculationItem calcItem in regularCg.CalculationItems)
{
/*measure name*/
string measureName = m.Name + " " + calcItem.Name;
//only if the measure is not yet there (think of reruns)
if(!Model.AllMeasures.Any(x => x.Name == measureName)){
/*prepares a query to calculate the resulting format when applying the calculation item on the measure*/
string query = string.Format(
"EVALUATE {{CALCULATE({0},{1},{2})}}",
m.DaxObjectFullName,
string.Format(
"{0}=\"{1}\"",
regularCg.Columns[0].DaxObjectFullName,
calcItem.Name),
string.Format(
"{0}=\"{1}\"",
auxCg.Columns[0].DaxObjectFullName,
auxCalcItemName)
);
/*executes the query*/
using (var reader = Model.Database.ExecuteReader(query))
{
// resultset should contain just one row, with the format string
while (reader.Read())
{
/*retrive the formatstring from the query*/
string formatString = reader.GetValue(0).ToString();
/*build the expression of the measure*/
string measureExpression = string.Format(
"CALCULATE({0},{1}=\"{2}\")",
m.DaxObjectName,
regularCg.Columns[0].DaxObjectFullName,
calcItem.Name);
/*actually build the measure*/
Measure newMeasure =
m.Table.AddMeasure(
name: measureName,
expression: measureExpression);
/*the all important format string!*/
newMeasure.FormatString = formatString;
/*final polish*/
newMeasure.DisplayFolder = displayFolderName;
newMeasure.FormatDax();
/*add annotations for the creation of the field parameter*/
newMeasure.SetAnnotation(baseMeasureAnnotationName,m.Name);
newMeasure.SetAnnotation(calcItemAnnotationName,calcItem.Name);
newMeasure.SetAnnotation(scriptAnnotationName,scriptAnnotationValue);
newMeasure.SetAnnotation(calcItemSortOrderName,calcItem.Ordinal.ToString("000"));
}
}
}
}
}
if(!generateFieldParameter) {
//end of execution
return;
};
// Before running the script, select the measures or columns that you
// would like to use as field parameters (hold down CTRL to select multiple
// objects). Also, you may change the name of the field parameter table
// below. NOTE: If used against Power BI Desktop, you must enable unsupported
// features under File > Preferences (TE2) or Tools > Preferences (TE3).
if(Selected.Columns.Count == 0 && Selected.Measures.Count == 0) throw new Exception("No columns or measures selected!");
// Construct the DAX for the calculated table based on the measures created previously by the script
var objects = Model.AllMeasures
.Where(x => x.GetAnnotation(scriptAnnotationName) == scriptAnnotationValue)
.OrderBy(x => x.GetAnnotation(baseMeasureAnnotationName) + x.GetAnnotation(calcItemSortOrderName));
var dax = "{\n " + string.Join(",\n ", objects.Select((c,i) => string.Format("(\"{0}\", NAMEOF('{1}'[{0}]), {2},\"{3}\",\"{4}\")",
c.Name, c.Table.Name, i,
Model.Tables[c.Table.Name].Measures[c.Name].GetAnnotation("Base Measure"),
Model.Tables[c.Table.Name].Measures[c.Name].GetAnnotation("Calc Item")))) + "\n}";
// Add the calculated table to the model:
var table = Model.AddCalculatedTable(name, dax);
// In TE2 columns are not created automatically from a DAX expression, so
// we will have to add them manually:
var te2 = table.Columns.Count == 0;
var nameColumn = te2 ? table.AddCalculatedTableColumn(name, "[Value1]") : table.Columns["Value1"] as CalculatedTableColumn;
var fieldColumn = te2 ? table.AddCalculatedTableColumn(name + " Fields", "[Value2]") : table.Columns["Value2"] as CalculatedTableColumn;
var orderColumn = te2 ? table.AddCalculatedTableColumn(name + " Order", "[Value3]") : table.Columns["Value3"] as CalculatedTableColumn;
if(!te2) {
// Rename the columns that were added automatically in TE3:
nameColumn.IsNameInferred = false;
nameColumn.Name = name;
fieldColumn.IsNameInferred = false;
fieldColumn.Name = name + " Fields";
orderColumn.IsNameInferred = false;
orderColumn.Name = name + " Order";
}
// Set remaining properties for field parameters to work
// See: https://twitter.com/markbdi/status/1526558841172893696
nameColumn.SortByColumn = orderColumn;
nameColumn.GroupByColumns.Add(fieldColumn);
fieldColumn.SortByColumn = orderColumn;
fieldColumn.SetExtendedProperty("ParameterMetadata", "{\"version\":3,\"kind\":2}", ExtendedPropertyType.Json);
fieldColumn.IsHidden = true;
orderColumn.IsHidden = true;