Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This tutorial shows how to use SemPy (preview) to calculate measures in Power BI semantic models.
In this tutorial, you learn how to:
- Evaluate Power BI measures programmatically by using the Python interface of the Semantic Link library (SemPy)
- Learn about SemPy components that help bridge AI and BI:
- FabricDataFrame—pandas-like structure enhanced with semantic information
- Functions that get semantic models, including raw data, configurations, and measures
Prerequisites
Get a Microsoft Fabric subscription. Or, sign up for a free Microsoft Fabric trial.
Sign in to Microsoft Fabric.
Use the experience switcher on the bottom left side of your home page to switch to Fabric.
In the navigation pane, select Workspaces, then select your workspace to set it as the current workspace.
Download the Retail Analysis Sample PBIX.pbix semantic model and upload it to your workspace.
Follow along in the notebook
The powerbi_measures_tutorial.ipynb notebook accompanies this tutorial.
To open the accompanying notebook for this tutorial, follow the instructions in Prepare your system for data science tutorials to import the notebook to your workspace.
If you'd rather copy and paste the code from this page, you can create a new notebook.
Be sure to attach a lakehouse to the notebook before you start running code.
Set up the notebook
In this section, you set up a notebook environment.
Install
SemPy
from PyPI by using%pip
inline in the notebook.%pip install semantic-link-sempy
Import the modules you'll use later.
import sempy.fabric as fabric
Connect to the Power BI workspace, and list the semantic models in the workspace.
fabric.list_datasets()
Load the semantic model. In this tutorial, you use the Retail Analysis Sample semantic model.
dataset = "Retail Analysis Sample"
dataset = "Retail Analysis Sample PBIX"
List workspace measures
Use SemPy's list_measures
to list measures in a semantic model:
fabric.list_measures(dataset)
Evaluate measures
Use SemPy's evaluate_measure
function to evaluate measures in different ways.
Evaluate a raw measure
Use SemPy's evaluate_measure
function to calculate the preconfigured measure named "Average Selling Area Size".
fabric.evaluate_measure(dataset, measure="Average Selling Area Size")
Evaluate a measure with groupby_columns
Group the result by columns by using the groupby_columns
parameter:
fabric.evaluate_measure(dataset, measure="Average Selling Area Size", groupby_columns=["Store[Chain]", "Store[DistrictName]"])
This code groups by Store[Chain]
and Store[DistrictName]
.
Evaluate a measure with filters
Use the filters
parameter to limit results to specific column values:
fabric.evaluate_measure(dataset, \
measure="Total Units Last Year", \
groupby_columns=["Store[Territory]"], \
filters={"Store[Territory]": ["PA", "TN", "VA"], "Store[Chain]": ["Lindseys"]})
In this example, Store
is the table, Territory
is the column, and PA
is an allowed value.
Evaluate a measure across multiple tables
Group by columns across multiple tables in the semantic model.
fabric.evaluate_measure(dataset, measure="Total Units Last Year", groupby_columns=["Store[Territory]", "Sales[ItemID]"])
Evaluate multiple measures
The evaluate_measure
function lets you supply multiple measure identifiers and returns the calculated values in a single DataFrame
:
fabric.evaluate_measure(dataset, measure=["Average Selling Area Size", "Total Stores"], groupby_columns=["Store[Chain]", "Store[DistrictName]"])
Use Power BI XMLA connector
The default semantic model client uses the Power BI REST APIs. If queries fail with this client, switch to the Power BI XMLA endpoint by setting use_xmla=True
. SemPy parameters are the same for measure calculations with XMLA.
fabric.evaluate_measure(dataset, \
measure=["Average Selling Area Size", "Total Stores"], \
groupby_columns=["Store[Chain]", "Store[DistrictName]"], \
filters={"Store[Territory]": ["PA", "TN", "VA"], "Store[Chain]": ["Lindseys"]}, \
use_xmla=True)
Related content
See other semantic link and SemPy tutorials:
- Tutorial: Clean data with functional dependencies
- Tutorial: Analyze functional dependencies in a sample semantic model
- Tutorial: Discover relationships in a semantic model using semantic link
- Tutorial: Discover relationships in the Synthea dataset, using semantic link
- Tutorial: Validate data using SemPy and Great Expectations (GX)