Edit

Quickstart: Run your first DAX query

In this quickstart, you authenticate to Power BI, execute a DAX query against a semantic model, and deserialize the Arrow response into a local data structure.

Prerequisites

  • A Power BI workspace with at least one semantic model.
  • Build and Read permissions on the semantic model.
  • A Microsoft Entra app registration (or use interactive authentication for testing).
  • Python 3.10 or later.

  • Install dependencies:

    pip install msal pyarrow pandas
    

1 - Authenticate

Acquire a bearer token with the https://analysis.windows.net/powerbi/api/.default scope.

from msal import PublicClientApplication

client_id = "YOUR_APP_CLIENT_ID"
authority = "https://login.microsoftonline.com/YOUR_TENANT_ID"
scopes = ["https://analysis.windows.net/powerbi/api/.default"]

app = PublicClientApplication(client_id, authority=authority)
result = app.acquire_token_interactive(scopes=scopes)

access_token = result["access_token"]

2 - Execute a DAX query

Send a POST request to the Execute Queries Arrow endpoint with a simple EVALUATE statement.

import io
import pyarrow as pa
import requests

group_id = "YOUR_WORKSPACE_ID"
dataset_id = "YOUR_DATASET_ID"

url = (f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}"
       f"/datasets/{dataset_id}/executeDaxQueries")

headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json",
}

body = {"query": "EVALUATE TOPN(5, 'DimProduct')"}

response = requests.post(url, headers=headers, json=body)
response.raise_for_status()

reader = pa.ipc.open_stream(io.BytesIO(response.content))
table = reader.read_all()
df = table.to_pandas()

print(df)

3 - Inspect the results

# Print the Arrow schema (column names and types)
print(table.schema)

# Show the first few rows as a pandas DataFrame
print(df.head())

# Access a specific column
print(table.column("ProductName").to_pylist()[:5])

Clean up resources

If you created a Microsoft Entra app registration solely for testing, navigate to the Azure portal and delete it. Access tokens expire automatically and don't need manual revocation.