Semantic Link (SemPy)
Semantic Link, also known by its core Python library SemPy, facilitates seamless collaboration between data scientists and business analysts within Microsoft Fabric. It enables Python and Spark users in Synapse Data Science notebooks to connect directly to Power BI semantic models, access their underlying data, measures, and rich semantic information, eliminating the need to re-implement business logic. The library is actively developed, with version 0.14.0 as of the latest release, and is specifically designed for the Microsoft Fabric environment.
Warnings
- breaking Semantic Link (SemPy) is exclusively supported within Microsoft Fabric notebooks and Spark environments. It will not run in standalone Python environments outside of Fabric. Attempting to use it locally will result in errors related to missing Fabric-specific components.
- gotcha The `semantic-link` package is a meta-package. For core functionality, `semantic-link-sempy` is sufficient. If you encounter dependency conflicts or only need specific features, consider installing `semantic-link-sempy` and other `semantic-link-functions-*` packages individually instead of the meta-package.
- gotcha Semantic Link is pre-installed in Microsoft Fabric Runtimes 1.2 (Spark 3.4) and above. Explicitly installing it with `%pip install -U semantic-link` is only necessary for older runtimes (Spark 3.3 or below) or to ensure you have the very latest version.
- gotcha Using Semantic Link requires a Microsoft Fabric subscription with appropriate capacity (e.g., F64 for LLM samples) and Power BI datasets residing in a Premium or Fabric workspace within the same tenant. Certain advanced features may also require XMLA Read/Write enabled for your capacity.
- gotcha The `semantic-link-labs` library is an official extension offering early access to experimental features. While powerful, features in `Labs` may be less stable and subject to change or removal compared to the core `semantic-link` library.
- gotcha The primary goal of Semantic Link is to leverage the semantic information (measures, relationships, data categories) already defined in Power BI semantic models. A common anti-pattern is to extract raw data and re-implement business logic in Python/Spark, negating a key benefit of the library.
Install
-
%pip install -U semantic-link -
pip install semantic-link
Imports
- fabric
import sempy.fabric as fabric
- functions
import sempy.functions as sf
Quickstart
import sempy.fabric as fabric
# List all semantic models (datasets) in the current workspace
df_datasets = fabric.list_datasets()
print("Available datasets:")
print(df_datasets.head())
# Assuming a dataset named 'Sales & Returns Sample' exists
# Read a specific table from a semantic model
df_customers = fabric.read_table(dataset="Sales & Returns Sample", table="Customer")
print("\nFirst 5 rows of the 'Customer' table:")
print(df_customers.head())