Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Patriszjo
Frequent Visitor

Extract M Code for each Dataset table usingTMSL Code

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()

 

1 ACCEPTED SOLUTION
Patriszjo
Frequent Visitor

Ok, solved it 🙂

I can run in notebook DAX query with INFO.PARTITIONS() function that reads from DMV


Code:

%%dax "Test Dataset for SemPy"

EVALUATE
INFO.PARTITIONS()

Here is the output:
Zrzut ekranu 2024-01-31 114757.png

View solution in original post

1 REPLY 1
Patriszjo
Frequent Visitor

Ok, solved it 🙂

I can run in notebook DAX query with INFO.PARTITIONS() function that reads from DMV


Code:

%%dax "Test Dataset for SemPy"

EVALUATE
INFO.PARTITIONS()

Here is the output:
Zrzut ekranu 2024-01-31 114757.png

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.