Microsoft Fabric: Load Power BI Data Model into a PySpark data frame

Overview

Within Microsoft Fabric there can be a requirement to load data from a Power BI (Semantic) Data Model into a Data Lakehouse for reporting on multiple data sources.

Business Scenario

You have access to a Power BI Shared Data Model, but you don’t have access to the data source of the Power BI Shared Data Model.

So you need to load data from this Power BI data model into a Data Lakehouse using PySpark so that you can deliver insights by combining the data within this Power BI data model and other data within this Data Lakehouse.

How to Do This

To load a Power BI semantic model dataset into a Data Lakehouse using PySpark in Microsoft Fabric, you can follow these steps:

Set Up Your Environment

• Ensure you have a Microsoft Fabric subscription or sign up for a free trial.
• Sign in to Microsoft Fabric.

• Select or create a workspace.

Upload the Semantic Model

• Import the semantic model into your workspace (e.g. AdventureWorksDataStory.pbix)

Install Necessary Libraries

• Create a new notebook in the workspace you are using.

• If you’re using Spark 3.4 or above, the Semantic Link library is included by default. For Spark 3.3 or below, install it using:

%pip install -U semantic-link

Access and Query Data

• List tables from the semantic model:

df = spark.sql(“SHOW TABLES FROM pbi”)
display(df)

• Query a particular table within the semantic model

%%sql
SELECT `Product Name`, COUNT(*) FROM pbi.`AdventureWorksDataStory`.Product GROUP BY `Product Name`

• Create a Data Lakehouse if one does not already exist within the workspace:

 

• Load Data into Lakehouse:

%%sql
CREATE TABLE myDataLakehouse.ProductCounts AS SELECT `Product Name`, COUNT(*) AS ProductCount FROM pbi.`AdventureWorksDataStory`.Product GROUP BY `Product Name`;

• Query the newly created table within the Data Lakehouse:

%%sql SELECT * FROM myDataLakehouse.ProductCounts

 

References

Read data from semantic models and write data that semantic models can consume using Spark