-
Notifications
You must be signed in to change notification settings - Fork 1
/
Export_PBIModel_Metadata_Multiplefiles.csx
277 lines (222 loc) · 8.12 KB
/
Export_PBIModel_Metadata_Multiplefiles.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Update this location to the destination folder on your computer
string folderName = @"C:\Users\ext.trinh.quang\Desktop\Central Dataset - Metadata";
/****************************************************************************************/
/***********************************Columns and Measures ********************************/
// Construct a list of all visible columns and measures:
var objects = Model.AllMeasures.Where(m => !m.Table.IsHidden || m.Table.IsHidden).Cast<ITabularNamedObject>()
.Concat(Model.AllColumns.Where(c => !c.Table.IsHidden || c.Table.IsHidden));
// Get their properties in TSV format (tabulator-separated):
var tsv = ExportProperties(objects,"ObjectName,ObjectType,TableName,SourceColumn,DataType,Expression,HiddenFlag,FormatString,Description");
// (Optional) Output to screen (can then be copy-pasted into Excel):
// tsv.Output();
// ...or save the TSV to a file:
SaveFile(folderName + @"\Measures&Columns.tsv", tsv);
/**********************************************************************/
/***********************************RLS********************************/
var sb = new System.Text.StringBuilder();
// Headers
sb.Append("RoleName" + '\t' + "TableName" + '\t' + "FilterExpression");
sb.Append(Environment.NewLine);
foreach (var t in Model.Tables)
{
foreach(var r in Model.Roles)
{
string rls = Model.Tables[t.Name].RowLevelSecurity[r.Name];
if (!String.IsNullOrEmpty(rls))
{
sb.Append(r.Name + '\t' + t.Name + '\t' + rls);
sb.Append(Environment.NewLine);
}
}
}
System.IO.File.WriteAllText(folderName + @"\RLS.tsv", sb.ToString());
/**********************************************************************/
/****************************RELATIONSHIPS*****************************/
sb = new System.Text.StringBuilder();
// Header
sb.Append("FromTable" + '\t' + "FromColumn" + '\t' + "ToTable" + '\t' + "ToColumn" + '\t' +
"Active" + '\t' + "CrossFilteringBehavior" + '\t' + "RelationshipType" + '\t' + "SecurityFilteringBehavior" + '\t' + "RelyOnReferentialIntegrity");
sb.Append(Environment.NewLine);
foreach (var r in Model.Relationships)
{
string act = string.Empty;
string relType = string.Empty;
string cfb = string.Empty;
string sfb = string.Empty;
string rori = string.Empty;
if (r.IsActive)
{
act = "Yes";
}
else
{
act = "No";
}
if (r.FromCardinality == RelationshipEndCardinality.Many && r.ToCardinality == RelationshipEndCardinality.One)
{
relType = "Many-to-One";
}
else if (r.FromCardinality == RelationshipEndCardinality.Many && r.ToCardinality == RelationshipEndCardinality.Many)
{
relType = "Many-to-Many";
}
if (r.CrossFilteringBehavior == CrossFilteringBehavior.OneDirection)
{
cfb = "Single";
}
else if (r.CrossFilteringBehavior == CrossFilteringBehavior.BothDirections)
{
cfb = "Bi";
}
if (r.SecurityFilteringBehavior == SecurityFilteringBehavior.OneDirection)
{
sfb = "Single";
}
else if (r.SecurityFilteringBehavior == SecurityFilteringBehavior.BothDirections)
{
sfb = "Bi";
}
if (r.RelyOnReferentialIntegrity)
{
rori = "Yes";
}
else
{
rori = "No";
}
sb.Append(r.FromTable.Name + '\t' + r.FromColumn.Name + '\t' + r.ToTable.Name + '\t' + r.ToColumn.Name + '\t' +
act + '\t' + cfb + '\t' + relType + '\t' + sfb + '\t' + rori);
sb.Append(Environment.NewLine);
}
System.IO.File.WriteAllText(folderName + @"\Relationships.tsv", sb.ToString());
/**********************************************************************/
/******************************HIERARCHIES*****************************/
sb = new System.Text.StringBuilder();
// Headers
sb.Append("HierarchyName" + '\t' + "TableName" + '\t' + "ColumnName");
sb.Append(Environment.NewLine);
// Hierarchies
foreach (var h in Model.AllHierarchies.ToList())
{
foreach (var lev in h.Levels.ToList())
{
sb.Append(h.Name + '\t' + h.Table.Name + '\t' + lev.Name);
sb.Append(Environment.NewLine);
}
}
System.IO.File.WriteAllText(folderName + @"\Hierarchies.tsv", sb.ToString());
/**********************************************************************/
/******************************PERSPECTIVES****************************/
sb = new System.Text.StringBuilder();
// Headers
sb.Append("TableName" + '\t' + "ObjectName" + '\t' + "ObjectType");
// Loop header for each perspective
foreach (var p in Model.Perspectives.ToList())
{
sb.Append('\t' + p.Name);
}
sb.Append(Environment.NewLine);
// Measures
foreach (var o in Model.AllMeasures.ToList())
{
sb.Append(o.Parent.Name + '\t' + o.Name + '\t' + "Measure");
foreach (var p in Model.Perspectives.ToList())
{
string per = string.Empty;
if (o.InPerspective[p])
{
per = "Yes";
}
else
{
per = "No";
}
sb.Append('\t' + per);
}
sb.Append(Environment.NewLine);
}
// Columns
foreach (var o in Model.AllColumns.ToList())
{
sb.Append(o.Table.Name + '\t' + o.Name + '\t' + "Column");
foreach (var p in Model.Perspectives.ToList())
{
string per = string.Empty;
if (o.InPerspective[p])
{
per = "Yes";
}
else
{
per = "No";
}
sb.Append('\t' + per);
}
sb.Append(Environment.NewLine);
}
// Hierarchies
foreach (var o in Model.AllHierarchies.ToList())
{
sb.Append(o.Parent.Name + '\t' + o.Name + '\t' + "Hierarchy");
foreach (var p in Model.Perspectives.ToList())
{
string per = string.Empty;
if (o.InPerspective[p])
{
per = "Yes";
}
else
{
per = "No";
}
sb.Append('\t' + per);
}
sb.Append(Environment.NewLine);
}
System.IO.File.WriteAllText(folderName + @"\Perspectives.tsv", sb.ToString());
/**********************************************************************/
/*************************CALCULATION GROUPS***************************/
sb = new System.Text.StringBuilder();
// Headers
sb.Append("CalculationGroup" + '\t' + "CalculationItem" + '\t' + "Expression" + '\t' + "Ordinal" + '\t' + "FormatString" + '\t' + "Description");
sb.Append(Environment.NewLine);
foreach (var o in Model.CalculationGroups.ToList())
{
foreach (var i in o.CalculationItems.ToList())
{
string cg = o.Name;
string ci = i.Name;
string expr = i.Expression;
// Remove tabs and new lines
expr = expr.Replace("\n"," ");
expr = expr.Replace("\t"," ");
string ord = i.Ordinal.ToString();
string fs = i.FormatStringExpression;
string desc = i.Description;
sb.Append(cg + '\t' + ci + '\t' + expr + '\t' + ord + '\t' + fs + '\t' + desc);
sb.Append(Environment.NewLine);
}
}
System.IO.File.WriteAllText(folderName + @"\CalculationGroups.tsv", sb.ToString());
/**********************************************************************/
/*************************PARTITIONS***************************/
sb = new System.Text.StringBuilder();
sb.Append("tableName" + '\t' + "partitionName" + '\t' + "Expressions" + '\t' + "Mode" + '\t' + "SourceType");
sb.Append(Environment.NewLine);
foreach (var p in Model.AllPartitions.ToList())
{
string tableName = p.Table.Name;
string partitionName = p.Name;
string expr = p.Expression;
var mode = p.Mode;
var SourceType = p.SourceType;
if (!String.IsNullOrEmpty(expr))
{
// Remove tabs and new lines
expr = expr.Replace("\n"," ");
expr = expr.Replace("\t"," ");
}
sb.Append(tableName +'\t' + partitionName + '\t' + expr +'\t' + mode + '\t' + SourceType);
sb.Append(Environment.NewLine);
}
System.IO.File.WriteAllText(folderName + @"\partitions.tsv", sb.ToString());