Edit

Share via


Extract and calculate Power BI measures from a Jupyter notebook

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

  • 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.

Set up the notebook

In this section, you set up a notebook environment.

  1. Install SemPy from PyPI by using %pip inline in the notebook.

    %pip install semantic-link-sempy
    
  2. Import the modules you'll use later.

    import sempy.fabric as fabric
    
  3. Connect to the Power BI workspace, and list the semantic models in the workspace.

    fabric.list_datasets()
    
  4. 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)

See other semantic link and SemPy tutorials: