Back to snippets

sempy_fabric_powerbi_semantic_model_dax_query_to_pandas.py

python

This quickstart demonstrates how to connect to a Power BI semantic model,

15d ago30 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
sempy_fabric_powerbi_semantic_model_dax_query_to_pandas.py
1import sempy.fabric as fabric
2import pandas as pd
3
4# Define the workspace and dataset name
5workspace_name = "Your Workspace Name"
6dataset_name = "Your Dataset Name"
7
8# List all tables in the specified semantic model
9tables = fabric.list_tables(dataset_name, workspace=workspace_name)
10print("Tables in the model:")
11print(tables)
12
13# Execute a DAX query against the semantic model
14dax_query = """
15EVALUATE
16    SUMMARIZECOLUMNS(
17        'Table'[Column1],
18        "Total Value", SUM('Table'[Column2])
19    )
20"""
21
22df = fabric.evaluate_dax(
23    dataset=dataset_name,
24    workspace=workspace_name,
25    dax_string=dax_query
26)
27
28# Display the resulting DataFrame
29print("\nQuery Results:")
30print(df.head())