Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
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:
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!