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

Join 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

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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.