Repository navigation
Expand file tree
/
Copy pathtable_column_usage_tmdl.py
More file actions
255 lines (204 loc) · 9.23 KB
/
Copy pathtable_column_usage_tmdl.py
File metadata and controls
255 lines (204 loc) · 9.23 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
#!/usr/bin/env python3
"""
TMDL Semantic Model + PBIR Usage Analyzer
✅ Supports TMDL-based models (no model.json required)
✅ Reads tables/columns from: definition/tables/<table>/columns/*.tmdl
✅ Scans PBIR report JSON to detect:
- Where each table is used
- Where each column is used
Usage:
python table_column_usage_tmdl.py <Report.Report> --model-path <SemanticModelFolder> --table DimCustomer
python table_column_usage_tmdl.py <Report.Report> --model-path <SemanticModelFolder> --column DimCustomer.CustomerName
"""
import json
import sys
import argparse
from pathlib import Path
# =====================================================================
# ✅ Utility JSON Loader
# =====================================================================
def load_json(path: Path):
try:
with open(path, "r", encoding="utf-8-sig") as f:
return json.load(f)
except:
return {}
# =====================================================================
# ✅ TMDL + PBIR Scanner
# =====================================================================
class TMDLUsageScanner:
def __init__(self, report_path: str, model_path: str):
self.report_path = Path(report_path)
self.definition = self.report_path / "definition"
if not self.report_path.exists():
raise FileNotFoundError(f"Report folder not found: {report_path}")
# TMDL model path
self.model_path = Path(model_path)
self.tmdl_tables = self.model_path / "definition" / "tables"
if not self.tmdl_tables.exists():
raise FileNotFoundError(
f"TMDL tables folder not found: {self.tmdl_tables}"
)
# -----------------------------------------------------------------
# ✅ Load PBIR report parts
# -----------------------------------------------------------------
def load_pbir_parts(self):
parts = []
# report.json
report_json = self.definition / "report.json"
if report_json.exists():
parts.append((str(report_json), load_json(report_json)))
# Pages + Visuals
pages = self.definition / "pages"
if pages.exists():
for page_folder in pages.iterdir():
if not page_folder.is_dir():
continue
# Page.json
page_json = page_folder / "page.json"
if page_json.exists():
parts.append((str(page_json), load_json(page_json)))
# Visuals
visuals = page_folder / "visuals"
if visuals.exists():
for visual_folder in visuals.iterdir():
visual_json = visual_folder / "visual.json"
if visual_json.exists():
parts.append((str(visual_json), load_json(visual_json)))
# Bookmarks
bookmarks = self.definition / "bookmarks"
if bookmarks.exists():
for bm in bookmarks.glob("*.bookmark.json"):
parts.append((str(bm), load_json(bm)))
return parts
# -----------------------------------------------------------------
# ✅ Load ALL tables & columns from TMDL
# -----------------------------------------------------------------
def load_tmdl_model(self):
"""
TMDL structure:
definition/tables/<TableName>.tmdl
definition/tables/<TableName>/columns/<Column>.tmdl
"""
tables = []
columns = []
for item in self.tmdl_tables.iterdir():
# TMDL allows tables as .tmdl files (not just folders)
if item.is_file() and item.suffix == ".tmdl":
table_name = item.stem
tables.append(table_name)
continue
# Or full folder structure
if item.is_dir():
table_name = item.name
tables.append(table_name)
cols_folder = item / "columns"
if cols_folder.exists():
for col_file in cols_folder.glob("*.tmdl"):
columns.append({
"table": table_name,
"column": col_file.stem
})
return tables, columns
# -----------------------------------------------------------------
# ✅ Recursive JSON search
# -----------------------------------------------------------------
def _scan_json(self, data, table_name, column_name=None, path=""):
hits = []
if isinstance(data, dict):
# Table / column reference via entity/property
if data.get("entity") == table_name:
if column_name is None or data.get("property") == column_name:
hits.append(path)
# "Column": {} style reference
if "Column" in data:
c = data["Column"]
src = c.get("Expression", {}).get("SourceRef", {})
if src.get("Entity") == table_name:
if column_name is None or c.get("Property") == column_name:
hits.append(path)
# DAX expression search
if isinstance(data.get("Expression"), str):
expr = data["Expression"]
if f"'{table_name}'" in expr or f"[{table_name}]" in expr:
hits.append(path)
if column_name and f"[{column_name}]" in expr:
hits.append(path)
# recursion
for k, v in data.items():
newpath = f"{path}.{k}" if path else k
hits.extend(self._scan_json(v, table_name, column_name, newpath))
elif isinstance(data, list):
for i, item in enumerate(data):
hits.extend(self._scan_json(item, table_name, column_name, f"{path}[{i}]"))
return hits
# -----------------------------------------------------------------
# ✅ Public API: find table usage
# -----------------------------------------------------------------
def find_table_usage(self, table):
parts = self.load_pbir_parts()
matches = []
for (filename, payload) in parts:
found = self._scan_json(payload, table)
for f in found:
matches.append((filename, f))
return matches
# -----------------------------------------------------------------
# ✅ Public API: find column usage
# -----------------------------------------------------------------
def find_column_usage(self, table, column):
parts = self.load_pbir_parts()
matches = []
for (filename, payload) in parts:
found = self._scan_json(payload, table, column)
for f in found:
matches.append((filename, f))
return matches
# =====================================================================
# ✅ CLI
# =====================================================================
def main():
parser = argparse.ArgumentParser(description="Find table/column usage in PBIR using TMDL semantic model.")
parser.add_argument("report_path", help="Path to *.Report folder")
parser.add_argument("--model-path", required=True, help="Path to TMDL model folder (folder containing 'definition/tables')")
parser.add_argument("--table", help="Table name to check")
parser.add_argument("--column", help="Column name in Table.Column format")
args = parser.parse_args()
scanner = TMDLUsageScanner(args.report_path, args.model_path)
# ✅ Print all tables (debugging support)
tables, cols = scanner.load_tmdl_model()
print("\n📋 Tables found in TMDL model:")
for t in tables:
print(" -", t)
# ✅ Table usage
if args.table:
if args.table not in tables:
print(f"\n❌ Table '{args.table}' does NOT exist in the semantic model.")
return
print(f"\n🔍 Checking usage of TABLE: {args.table}")
matches = scanner.find_table_usage(args.table)
if not matches:
print(f"❌ Table '{args.table}' is NOT used anywhere in the report.")
else:
print(f"✅ Table '{args.table}' IS used in:")
for file, loc in matches:
print(f" - {file} → {loc}")
# ✅ Column usage
if args.column:
if "." not in args.column:
print("❌ Column must be in Table.Column format")
return
table, col = args.column.split(".", 1)
if table not in tables:
print(f"\n❌ Table '{table}' does NOT exist in the semantic model.")
return
print(f"\n🔍 Checking usage of COLUMN: {table}.{col}")
matches = scanner.find_column_usage(table, col)
if not matches:
print(f"❌ Column '{table}.{col}' is NOT used anywhere.")
else:
print(f"✅ Column '{table}.{col}' IS used in:")
for file, loc in matches:
print(f" - {file} → {loc}")
if __name__ == "__main__":
sys.exit(main())