Musili-Adebayo's avatar
Musili-Adebayo
Icon for Advocate I rankAdvocate I
1 year ago

How to Use Semantic Link Labs in Microsoft Fabric Notebook (Python Demo Walkthrough)

Working with Power BI reports within Microsoft Fabric Notebook just got a whole lot easier!

If you are ever tired of manually completing tasks within Microsoft Fabric or exploring the UI console seems like a lengthy process, especially when working with Power BI reports. The Semantic Link Labs is a Python library designed specifically for you to perform tasks within Microsoft Fabric Notebooks programmatically.

Image Credit: Microsoft Learn Semantic Link dataflow

 

This Python Library was developed by Michael Kovalsky and it builds on the foundational capabilities of Semantic Link, introducing enhanced features that enable deeper integration and more seamless workflows within the Fabric ecosystem while leveraging the power of Microsoft Fabric Notebook.

Here’s what you need to know about this amazing Python Library.

  • You don’t need to be a Python Expert before you get started.
  • It's an Open-Source library.
  • It allows you to programmatically access all the artefacts in your Workspace(Semantic Models, Lakehouse, Warehouse, Reports, etc).
  • It showcases the power of Microsoft Fabric notebooks.
  • You can perform over 304+ Functions with this Library.

 

So, let’s explore it?

Our Scenario:

Anna just joined AMA Enterprise as their Fabric Analytics Engineer, and she has been tasked to do the following tasks leveraging the power of the Semantic Links Labs in Microsoft Fabric Notebook. Result from Adventureworks Dashboard report pages.Result from Adventureworks Dashboard report pages. Result from Adventureworks Dashboard report pages. Result from Adventureworks Dashboard report pages. Result from Adventureworks Dashboard report pages. Result from Adventureworks Dashboard report pages. Result from Adventureworks Dashboard report pages.

Anna’s task:

 

  1. Understand the existing report metadata (show how many report pages, visuals in a dashboard, different filters used in the report, bookmarks, etc).
  2. Show the frequency of the semantic model object used within reports.
  3. Find broken visuals in a Power BI report.
  4. Get the sizes of all the semantic models in a workspace.
  5. Save a report in Power BI Service as a .pbip file.

 

Credit: The snapshot of the Adventureworks Dashboard used for this demonstration was built by my sister, Rofiat Adebayo.

 

You can download the Microsoft Fabric Notebook used for this Python Demo Walkthrough here.
Adeventureworks Dashboard.gif

 

Let's get started:

Open your Notebook in Microsoft Fabric, and let's start seeing some of the code examples in action. Before we get started, we must make sure we have the Semantic Link Labs library installed as well as the other necessary libraries.

#Install the Semantic link Labs library in your Fabric notebook

%pip install semantic-link-labs

After that, we will also be installing the necessary libraries in your notebook.

### Once installed, run this code to import the library into your 
# notebook

import sempy_labs as labs
from sempy_labs.report import ReportWrapper
import sempy.fabric as fabric

1. Understand the existing report metadata (show how many report pages, visuals in a dashboard, different filters used in the report, bookmarks, etc).

Note: This function require the report to be in the PBIR format.
#View Report Metadata

report = 'Adventureworks Dashboard' # Name or ID of the report

# Name or ID of the workspace in which the report resides
workspace = 'Fabricday' 

rpt = ReportWrapper(report=report, workspace=workspace)

# To view the report pages
rpt.list_pages()

#To list all visual in a dashboard              
#rpt.list_visuals()  
 
#To list all custom visual in a dashboard        
#rpt.list_custom_visuals()  

#To list all report filters in a dashboard
#rpt.list_report_filters() 
   
#To list all page filters in a dashboard
#rpt.list_page_filters()  

#To list all visual filters in a dashboard       
#rpt.list_visual_filters() 
 
#To list all visual objects in a dashboard   
#rpt.list_visual_objects()    

#To list all bookmarks in a dashboard  
#rpt.list_bookmarks()  

#To list all report level measures in a dashboard            
#rpt.list_report_level_measures() 

#To list the semantic model objects in a dashboard
#rpt.list_semantic_model_objects() 

The result shows that we have 11 report pages with other details about the Adventureworks Dashboard.

Result from Adventureworks Dashboard report pages.

 

2. Show the frequency of the semantic model object used within reports.

 

Note: This function require the report to be in the PBIR format.
# Show the frequency of the semantic model object used within reports.

# Enter the name or ID of your semantic model
dataset = 'Adventureworks Dashboard' 

# Enter the name or ID of the workspace in which the semantic model resides
workspace = 'Fabricday' 

df = labs.list_semantic_model_object_report_usage(dataset=dataset, workspace=workspace, include_dependencies=True, extended=True)
display(df)

The result below shows that Order Quantity, Product Price, Total Revenue, Total Orders, OrderNumber are the top 5 most used semantic model objects used in the Adventureworks Dashboard.

Result image shows the frequency of the sematic model object used within reports

3. Find broken visuals in a Power BI report.

One of Anna’s tasks is to make sure all dashboard works fine and that there are no broken visuals in any of the report pages. Manually scrolling through a 12-page report dashboard seems like a Herculean task since Anna manages 10 other dashboards, too. This is why you can automatically detect broken visuals as a result of changes in measures or calculations with the code below.

Note: This function require the report to be in the PBIR format.
# Find broken visuals in a Power BI report.

import sempy_labs as labs
from sempy_labs.report import ReportWrapper

# This is for a single report
report = 'Adventureworks Dashboard' # The name or ID of the report

# The name or ID of the workspace in which the report exists
workspace = 'Fabricday' 

rpt = ReportWrapper(report=report, workspace=workspace)
df = rpt.list_semantic_model_objects(extended=True)
display(df)

The result shows the Valid Semantic Model Object as True because there are no broken visuals in my report, as seen from the GIF I had shared earlier. But if they are broken visuals, Valid Semantic Model Object will be False.

Image shows that they are no broken visuals in my report.

4. Find the sizes of the semantic model.

Knowing the size of a semantic model can help Anna to effectively manage her Team Microsoft Fabric capacity and licenses.

# Get the size of a semantic model

import sempy_labs as labs
import sempy.fabric as fabric

# Enter the name or ID of your semantic model
dataset = 'Adventureworks Dashboard' 

# Enter the name or ID of the workspace in which the semantic model # resides
workspace = 'Fabricday'  

# To check for a single semantic model
model_size = labs.get_semantic_model_size(dataset=dataset, workspace=workspace)
display(model_size)

The result shows that the total size of the Adventureworks

 

>>> semantic model is 7761847.496032715

4b. To check the size of all the semantic models in the Fabricday my workspace.

# Check for all semantic models within a workspace

# importing the necessary library
import sempy_labs as labs
import sempy.fabric as fabric

# Enter the name or ID of the workspace in which the semantic model #resides
workspace = 'Fabricday'

model_sizes = {}
dfD = fabric.list_datasets(workspace=workspace, mode="rest")

for _, r in dfD.iterrows():
   d_name = r["Dataset Name"]
   d_id = r["Dataset Id"]
   if not labs.is_default_semantic_model(dataset=d_id, workspace=workspace):
       model_size = labs.get_semantic_model_size(dataset=d_id, workspace=workspace)
       model_sizes[d_name] = model_size
   
display(model_sizes)

The result shows the sizes of the two semantic models I have in my Fabricday workspace.

Image showing the sizes of two semantic model I have in my workspace.

 

5. Save a report in Power BI Service as a .pbip file.

 

Saving a report as .pbip file encourages team collaboration, source control and CI/CD for a Power BI dashboard. It also allows you to easily do a batch update on the item definition on your report, visuals and semantic models.

# Saving a report as .pbib file

import sempy_labs.report as rep

# Name or ID of the report
report = 'Sales & Returns Sample v201912' 

# Name or ID of the workspace in which the report resides
workspace = 'Fabricday' 

# If set to True, saves the report and underlying semantic model. If # set to False, saves just the report.
thick_report = True 

# If set to True, saves a .pbip live-connected to the workspace in 
# the Power BI / Fabric service. If set to False, saves a .pbip with  # a local model, independent from the Power BI / Fabric service.
live_connect = True 

# Enter the name or ID of the lakehouse where you want to save the #.pbip file
lakehouse = 'new_fabric_day_lakehouse' 

# Enter the name or ID of the workspace in which the lakehouse 
# exists
workspace = 'Fabricday' 

lakehouse_workspace = None

rep.save_report_as_pbip(report=report, workspace=workspace, thick_report=thick_report, live_connect=live_connect, lakehouse=lakehouse, lakehouse_workspace=lakehouse_workspace)

The result shows the sampleSales & Returns Sample v201912 has been saved as .pbip file in the File section of my lakehouse.

Images show report saved as a .pbip file

Conclusion

Working with Power Report and Semantic models in Microsoft fabric just got easier with Semantic link labs. You can download the Microsoft Fabric Notebook used for this Python Demo Walkthrough here.

 

References

  1. Semantic Link Labs Git Hub Repository by Michael Kovalsky: https://github.com/microsoft/semantic-link-labs

You can connect with me via my socials below if you want to discuss further.

LinkedIn: Musili Adebayo

Twitter: Musili_Adebayo

 

No RepliesBe the first to reply