Microsoft Fabric- Embed Interactive Power BI Visualizations in Notebooks

Amit Chandak
6 min readAug 20, 2024

--

This blog post introduces you to a powerful new feature that is in preview today and allows you to easily include Power BI reports directly in your Microsoft Fabric Notebooks.

No More Switching Between Tools!

Imagine you have done some data analysis in a Notebook and want to make a quick but impressive visualization of your results with nice Power BI reports. With a little magic from the Python powerbiclient package, you are now able to do it without having to leave a Notebook environment ever. Here is all you need to know: This feature is in preview, meaning it is still under development and may change before the general availability release.

How to create a notebook?

The best way to open a notebook is from the Lakehouse. However, you can also open it from your workspace and choose the Lakehouse:

  1. Go to the left pane and select “Workspace.”
  2. Choose your workspace.
  3. In the top left, click “New,” then “More.”
  4. Under “Data Engineering,” select “Notebooks.”

From Lakehouse:

  1. Go to the left pane and select “Workspace.”
  2. Choose your workspace.
  3. Select “Lakehouse Explorer.”
  4. In the top menu, click “Open Notebook.”
  5. Open a new or existing notebook.

What you need to know before we start working on the Pyspark code: At the moment, this feature is in preview — meaning it’s still under development and will almost certainly undergo changes before it hits the streets.
You will work with the Fabric notebooks, which run using Spark runtime version 3.4. The beauty is that there is nothing more to set up! Just import powerbiclient; you’re all set.

I have selected a lake in my notebook. I will use a table from this lake at a later point in time.

To work with the first set of instructions to display a Power BI report, I need the workspace ID and report ID.

The workspace ID is visible in the current notebook URL as well, just after “groups/”.

For the report ID, I will open the report and get it from the report URL. It appears after “reports/”.

Now that I have both the workspace ID and report ID, it’s time to open the Power BI report in the Fabric notebook using PySpark.

Let us start with the code

from powerbiclient import Report

report_id="report_id"
report = Report(group_id="workspace_id", report_id=report_id)

report

Step 1: Import the Required Library

First, you’ll need to import the Report class from the powerbiclient library.

Step 2: Define Your Report ID

Next, assign the ID of your Power BI report to the report_id variable. This ID is crucial for embedding the report.

Step 3: Create the Report Object

Using the Report class, create a report object. If your report is not associated with any group, set group_id to None.

Step 4: Render the Report

Finally, simply invoke the report object to render the report directly in your notebook.

This will display a Power BI report in the notebook with all filters and slicers, creating a dynamic report displayed within the notebook.

Now we would like to visualize data from a Lakehouse table using a Spark DataFrame. We will use PySpark code for that

# Create a spark dataframe from a Lakehouse parquet table
sdf = spark.sql("SELECT * FROM Lake02.retaildata_fabric limit 10000")

# Create a Power BI report object from spark data frame
from powerbiclient import QuickVisualize, get_dataset_config
PBI_visualize = QuickVisualize(get_dataset_config(sdf))

# Render the report
PBI_visualize

Step 1: Create a Spark DataFrame from a Lakehouse Parquet Table

First, you’ll need to pull data from your Lakehouse table into a Spark DataFrame. This can be done using the spark.sqlmethod.

The SQL query selects all columns from the retaildata_fabric table within the Lake02Lakehouse and limits the result to 10,000 rows. The resulting data is stored in the sdfSpark DataFrame.

Step 2: Import the Power BI Visualization Library

Next, you need to import the necessary classes from the powerbiclient library to enable Power BI visualizations directly within your notebook.

Step 3: Create the Power BI Report Object

Now, you’ll create a Power BI report object using the Spark DataFrame sdf by configuring it for visualization.

The get_dataset_config(sdf) function creates a dataset configuration for the DataFrame sdf, and QuickVisualizegenerates a Power BI report object using this configuration.

Step 4: Render the Report

Finally, render the report directly in your notebook by invoking the report object.

This command will display the Power BI report in your notebook, allowing you to interact with your data visually.

It’s time to visualize an external file. We will get data from GitHub: https://github.com/amitchandakpbi/powerbi.

The file we would like to use is an Excel file. We will read it into a pandas DataFrame: https://github.com/amitchandakpbi/powerbi/raw/main/Sales%20Data%20for%20Fabric.xlsx.

import pandas as pd

# Create a pandas dataframe from a GitHub URL
df = pd.read_excel("https://github.com/amitchandakpbi/powerbi/raw/main/Sales%20Data%20for%20Fabric.xlsx", sheet_name= "Sales")

# Import the libraries
from powerbiclient import QuickVisualize, get_dataset_config

# Create a Power BI report object from pandas dataframe
PBI_visualize = QuickVisualize(get_dataset_config(df))

# Render the report
PBI_visualize

Step 1: Import the Pandas Library

First, import the pandas library, which is essential for handling data in Python, particularly for working with data in DataFrames.

Step 2: Create a Pandas DataFrame from a GitHub URL

Next, load your dataset directly from a GitHub URL into a Pandas DataFrame. The dataset is an Excel file with a specific sheet named “Sales.”

Step 3: Import the Power BI Visualization Library

Then, import the necessary classes from the powerbiclient library, which will enable you to create Power BI visualizations within your notebook.

Step 4: Create a Power BI Report Object from the Pandas DataFrame

Now, configure your Pandas DataFrame for visualization in Power BI by creating a Power BI report object.

Step 5: Render the Report

Finally, render the Power BI report directly in your notebook.

Use the “Show data table” to show the data.

Microsoft Fabric notebooks provide you with tons of functionality, and the seamless integration with Power BI reports takes it to the next level. This is truly the “ONE” unified platform.

You can watch the same on my YouTube channel.

Also, Refer

Complete Power BI in one Video in 11 hours

Mastering Power BI: 230+ Videos

Expertise Power BI: 150+ Videos

Power BI 50 Interview Questions and 10 Advanced Use Cases

My Medium blogs can be found here if you are interested

Click here to access all my blogs and videos in a jiffy via an exclusive glossary using Power BI. Please like, share, and comment on these blogs. I would appreciate your suggestions for improvement, challenges, and suggestions for topics so that I can explore these in more depth.

In addition, I have over 750 videos on my YouTube channel that are related to Power BI, Tableau, and Incorta. With the help of these videos, you will learn hundreds of DAX and Power Query functions, in addition to hundreds of Power BI, use cases that will assist you in becoming an expert in Power BI. Make sure you subscribe, like, and share it with your friends.

Master Power BI
Expertise in Power BI
Power BI For Tableau User
DAX for SQL Users
Learn SQL

--

--

Amit Chandak
Amit Chandak

Written by Amit Chandak

Amit has 20+ years of experience in Business Intelligence, Analytics, Data Science. He is Chief Analytics Officer at Kanerika & Power BI community Super User

No responses yet