Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I am trying to connect to a Power BI Service dataset from my local Python environment, without using Fabric Notebooks or Fabric Workspaces. My goal is to query and pull data from a published Power BI dataset directly into Python for further processing or integration. I have explored libraries like msal for authentication and looked into options like REST APIs and XMLA endpoints, but I’m not sure what the recommended or secure approach is to retrieve data directly from Power BI Service using Python. Has anyone successfully implemented this or can provide guidance on the proper steps, libraries, and authentication flow? Appreciate any help or suggestions from the community. Thank you!
Solved! Go to Solution.
please try the following Power BI API approach
before you try the code you will need to set this up the environment , step 6 and step 7
app will need read permission : https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/execute-queries
once all set try this code :
import requests
from msal import ConfidentialClientApplication
import json
# --- CONFIGURATION ---
TENANT_ID = 'your-tenant-id'
CLIENT_ID = 'your-client-id'
CLIENT_SECRET = 'your-client-secret'
WORKSPACE_ID = 'xxxx'
DATASET_ID = 'xxxx'
# --- MSAL SETUP ---
AUTHORITY = f'https://login.microsoftonline.com/{TENANT_ID}'
SCOPE = ['https://analysis.windows.net/powerbi/api/.default']
app = ConfidentialClientApplication(
CLIENT_ID, authority=AUTHORITY, client_credential=CLIENT_SECRET
)
# --- Acquire Token ---
result = app.acquire_token_for_client(scopes=SCOPE)
if "access_token" not in result:
raise Exception(f"Failed to get token: {result.get('error_description')}")
# --- Request Setup ---
url = f"https://api.powerbi.com/v1.0/myorg/groups/{WORKSPACE_ID}/datasets/{DATASET_ID}/executeQueries"
headers = {
'Authorization': f"Bearer {result['access_token']}",
'Content-Type': 'application/json'
}
payload = {
"queries": [
{
"query": "EVALUATE VALUES(MyTable)"
}
],
"serializerSettings": {
"includeNulls": True
},
}
# --- Send POST Request ---
response = requests.post(url, headers=headers, json=payload)
# --- Handle Response ---
if response.status_code == 200:
data = response.json()
print(json.dumps(data, indent=2))
else:
print(f"Error {response.status_code}: {response.text}")
Works for me
Hi @AkhilGrandhi1 ,
Just wanted to check if you had the opportunity to review the suggestions provided?
If the response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank You
please try the following Power BI API approach
before you try the code you will need to set this up the environment , step 6 and step 7
app will need read permission : https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/execute-queries
once all set try this code :
import requests
from msal import ConfidentialClientApplication
import json
# --- CONFIGURATION ---
TENANT_ID = 'your-tenant-id'
CLIENT_ID = 'your-client-id'
CLIENT_SECRET = 'your-client-secret'
WORKSPACE_ID = 'xxxx'
DATASET_ID = 'xxxx'
# --- MSAL SETUP ---
AUTHORITY = f'https://login.microsoftonline.com/{TENANT_ID}'
SCOPE = ['https://analysis.windows.net/powerbi/api/.default']
app = ConfidentialClientApplication(
CLIENT_ID, authority=AUTHORITY, client_credential=CLIENT_SECRET
)
# --- Acquire Token ---
result = app.acquire_token_for_client(scopes=SCOPE)
if "access_token" not in result:
raise Exception(f"Failed to get token: {result.get('error_description')}")
# --- Request Setup ---
url = f"https://api.powerbi.com/v1.0/myorg/groups/{WORKSPACE_ID}/datasets/{DATASET_ID}/executeQueries"
headers = {
'Authorization': f"Bearer {result['access_token']}",
'Content-Type': 'application/json'
}
payload = {
"queries": [
{
"query": "EVALUATE VALUES(MyTable)"
}
],
"serializerSettings": {
"includeNulls": True
},
}
# --- Send POST Request ---
response = requests.post(url, headers=headers, json=payload)
# --- Handle Response ---
if response.status_code == 200:
data = response.json()
print(json.dumps(data, indent=2))
else:
print(f"Error {response.status_code}: {response.text}")
Works for me
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
24 | |
17 | |
17 | |
13 | |
11 |
User | Count |
---|---|
32 | |
20 | |
19 | |
18 | |
11 |