-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathGenerate Last Updated table.csx
More file actions
102 lines (85 loc) · 3.08 KB
/
Generate Last Updated table.csx
File metadata and controls
102 lines (85 loc) · 3.08 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
/*
* Title: Generate Last Updated table
*
* Author: Irfan Charania
*
* There are times when you want to know when your model was last processed at the table level.
* SSAS Tabular does not provide an easy built-in way to do this.
*
* This script, when executed, will loop through all the tables in the model,
* and
* 1. create (or update) a hidden "Last Processed" calculated column, and
* 2. create (or update) a "Last Updated Tabular" calculated table containing data
* from the above calculated column
*
* Ref: https://www.sqlbi.com/articles/last-process-date-in-ssas-tabular/
*/
// Edit these values as desired
var columnName = "Last Processed";
var tableName = "Last Updated Tabular";
var dateFormatString = "yyyy-MMM-dd h:mm AM/PM";
// ---------------------------
// 1. create (or update) a hidden "Last Processed" calculated column
// ---------------------------
// Loop through all tables in model
foreach(var table in Model.Tables) {
// for all non-calculated tables
if (table.SourceType.ToString() != "Calculated" ){
CalculatedColumn column;
// if calculated column does not exist, create one
if (!table.Columns.Contains(columnName))
{
column = table.AddCalculatedColumn(
"Last Processed", // Name
"NOW()", // DAX Expression
"_Debug" // Display Folder
);
}
// else retreive the calculated column
else{
column = (CalculatedColumn)table.Columns[columnName];
}
// and update it's properties
column.DataType = DataType.DateTime;
column.IsHidden = true;
column.FormatString = dateFormatString;
column.Description = "Shows when SSAS table was last processed";
}
}
// ---------------------------
// 2. create (or update) a "Last Updated Tabular" calculated table
// containing data from the above calculated column
// ---------------------------
List<string> rowsList = new List<string>();
// Loop through all tables in model
foreach(var table in Model.Tables) {
// if calculated column exists
if (table.Columns.Contains(columnName))
{
// create DAX row expression to create row data
var s = String.Format(@"
ROW (
""Last Processed"", FORMAT ( MAX ( {0} ), ""{1}""),
""Table Name"", ""{2}""
)"
, table.Columns[columnName].DaxObjectFullName
, dateFormatString
, table.Name);
// add row expression to list
rowsList.Add(s);
}
}
// Combine all row statements into a single DAX expression
var rows = String.Join(", ", rowsList);
var expression = String.Format("UNION ( {0} )", rows);
CalculatedTable tbl;
// If calculated table already exists, retreive it
if (Model.Tables.Contains(tableName)){
tbl = (CalculatedTable)Model.Tables[tableName];
}
// else add new calculated table to the model
else{
tbl = Model.AddCalculatedTable(tableName);
}
// update calculated table's expression to earlier-built DAX expression
tbl.Expression = expression;