Skip to content

Instantly share code, notes, and snippets.

Created October 5, 2023 16:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import json
import pandas as pd
import sempy.fabric as fabric
dataset_name = "<>"
workspace_name = "<>"
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}
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment