-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureMagic.csx
163 lines (138 loc) · 5.04 KB
/
MeasureMagic.csx
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
#r "System.Xml.Linq"
using System.Xml;
using System.Xml.Linq;
/*
Point to the ExpressionTemplate.xml file to use.
*/
var ExpressionTemplatesXml = ReadFile(Model.GetAnnotation("MeasureMagic_FileLocation"));
/*
Overwrite existing Measures?
*/
var overwriteExistingMeasures = false;
/*
Copy DAX expression to description, if created measure has no description?
*/
var setEmptyDescriptionToExpression = false;
/*
Set to false if the domain daxformatter.com is not reachable.
Disabling Formatting will also speed up creation of Measures.
*/
var tryFormattingDAXExpressions = true;
/*
Enable logging (will create a lot of Message Boxes if you are not on the command line).
*/
var debugOutput = false;
string[] enabledArray = { "1", "true", "True" };
string[] disabledArray = { "0", "false", "False" };
XDocument MeasureTemplateGroups = XDocument.Parse(ExpressionTemplatesXml);
var measureTemplates =
MeasureTemplateGroups
.Descendants("MeasureTemplateGroup")
.Where(x => enabledArray.Contains((string)x.Attribute("Enabled")))
.Descendants("MeasureCollection")
.Where(x => enabledArray.Contains((string)x.Attribute("Enabled")))
.Descendants("Measure")
.Where(x => enabledArray.Contains((string)x.Attribute("Enabled")))
.Select(template => new
{
NameTemplate = (string)template.Attribute("NameTemplate"),
TranslatedName = (string)template.Attribute("TranslatedName"),
Description = (string)template.Attribute("Description"),
DisplayFolder = (string)template.Attribute("DisplayFolder"),
FormatString = (string)template.Attribute("FormatString"),
IsHidden = (string)template.Attribute("IsHidden"),
ExpressionTemplate = (string)template.Value
});
foreach (var template in measureTemplates)
{
var vNameTemplate = template.NameTemplate;
var vExpressionTemplate = template.ExpressionTemplate;
foreach (var baseMeasure in Selected.Measures)
{
var vDatenstandOrTable =
String.IsNullOrEmpty(baseMeasure.Table.GetExtendedProperty("QuelleDatenstand")) ?
baseMeasure.Table.Name :
baseMeasure.Table.GetExtendedProperty("QuelleDatenstand");
var vMName = String.Format(
vNameTemplate,
baseMeasure.Name,
baseMeasure.Table.Name,
vDatenstandOrTable
);
if (baseMeasure.Table.Measures.Contains(vMName))
{
if (overwriteExistingMeasures)
{
baseMeasure.Table.Measures[vMName].Delete();
}
else
{
if (debugOutput)
{
Info(String.Format(@"Measure [{0}] already exists, ommiting.", vMName));
}
continue;
}
}
var vMExpression = String.Format(
vExpressionTemplate,
baseMeasure.DaxObjectName,
baseMeasure.Name,
baseMeasure.Description,
vDatenstandOrTable
);
var measure = baseMeasure.Table.AddMeasure(
vMName,
vMExpression
);
measure.Description = String.Format(
template.Description ?? "",
baseMeasure.Name
);
if (
(setEmptyDescriptionToExpression && String.IsNullOrEmpty(measure.Description)) ||
measure.GetAnnotation("ExpressionAsDescription") == "1"
)
{
measure.Description = measure.Expression;
measure.SetAnnotation("ExpressionAsDescription", "1");
}
measure.DisplayFolder =
template.DisplayFolder is object ?
String.Format(
template.DisplayFolder,
baseMeasure.Name,
baseMeasure.DisplayFolder
) : baseMeasure.DisplayFolder;
measure.IsHidden = String.IsNullOrEmpty(template.IsHidden) ? false : bool.Parse(template.IsHidden);
measure.FormatString = template.FormatString ?? baseMeasure.FormatString;
measure.InPerspective.CopyFrom(baseMeasure.InPerspective);
foreach (var c in Model.Cultures)
{
var tmpTranslatedName =
template.TranslatedName is object ?
String.Format(
template.TranslatedName,
baseMeasure.Name,
baseMeasure.Table.Name,
vDatenstandOrTable,
baseMeasure.TranslatedNames[c] is string &&
baseMeasure.TranslatedNames[c].Length > 0 ?
baseMeasure.TranslatedNames[c] : baseMeasure.Name
) : null;
if (tmpTranslatedName != vMName)
{
measure.TranslatedNames[c] = tmpTranslatedName;
}
}
if (tryFormattingDAXExpressions)
{
measure.FormatDax();
}
measure.SetAnnotation("AUTOGEN", "1");
if (debugOutput)
{
Info(String.Format(@"Measure {0} created.", measure.DaxObjectName));
}
}
}