Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreJoin the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now
My goal is to extract a list of tables and M queires for each of them from a Semantic Model/Dataset published on a Fabric Workspace.
I use Fabric Notebooks and Python SemPy library to extract such things as tables, columns, relationships, measures, sources etc. and it works just fine but I have troubles with extracting M Code which is nested in TMSL Code.
Here I found example by Sandeep Pawar where he extracts different atributes from TMSL. I tried to edit his code to extract also M Code but failed as I do not fully understand his Python code.
https://fabric.guru/fabric-semantic-link-and-use-cases
Code:
import json
import pandas as pd
import sempy.fabric as fabric
dataset_name = "Test Dataset for SemPy"
workspace_name = "Fabrics Preview"
def custom_flatten(json_object, dataset_name, table_name=None, table_properties=None, column_name=None, flat_list=None):
if flat_list is None:
flat_list = []
if table_properties is None:
table_properties = {}
if not table_name: # Dataset level
for k, v in json_object.items():
if k == 'model':
table_properties.update({f"dataset_{key}": value for key, value in v.items() if key != 'tables'})
custom_flatten(v, dataset_name, table_properties=table_properties, flat_list=flat_list)
elif k == 'tables': # Table level
for table in v:
custom_flatten(table, dataset_name, table_name=table['name'], table_properties=table_properties, flat_list=flat_list)
elif k != 'name':
table_properties[f"dataset_{k}"] = v
elif not column_name: # Table level
for k, v in json_object.items():
if k == 'columns': # Column level
for column in v:
custom_flatten(column, dataset_name, table_name, table_properties, column_name=column['name'], flat_list=flat_list)
elif k != 'name':
table_properties[f"table_{k}"] = v
else: # Column level
column_properties = {f"column_{k}": v for k, v in json_object.items() if k != 'name'}
row = {'dataset_name': dataset_name, 'table_name': table_name, **table_properties, 'column_name': column_name, **column_properties}
flat_list.append(row)
return flat_list
tmsl_data = json.loads(fabric.get_tmsl(workspace=workspace_name, dataset=dataset_name))
dataset_name = tmsl_data['name']
flat_data = custom_flatten(tmsl_data, dataset_name)
tmsl_df = pd.DataFrame(flat_data)
tmsl_df.head()
Solved! Go to Solution.
Ok, solved it 🙂
I can run in notebook DAX query with INFO.PARTITIONS() function that reads from DMV
Code:
Ok, solved it 🙂
I can run in notebook DAX query with INFO.PARTITIONS() function that reads from DMV
Code:
Check out the April 2026 Power BI update to learn about new features.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
| User | Count |
|---|---|
| 11 | |
| 10 | |
| 9 | |
| 8 | |
| 8 |