Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Arya_A_R
Regular Visitor

Trying to query data with python scripts using power bi rest api

i was trying to access one of the dataset pubhlished in workspace through power bi rest api where i have admin privileges.The error recieved is "403".

 

Below mentioned is the code used to excute the query.

def execute_dax_query(workspace_id: str, dataset_id: str, query: str) -> str:
    """
    Execute a DAX query against a Power BI dataset using an embed token generated for the same dataset as get_model_definition.
    Returns query results as JSON data.
    """
    # Step 1: Generate embed token for the specified dataset
    gen_token_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/datasets/{dataset_id}/GenerateToken"
    payload = {
        "datasets": [
            {"id": dataset_id}
        ],
        "accessLevel": "read"
    }
    headers = {
        "Authorization": f"Bearer {TOKEN}",
        "Content-Type": "application/json"
    }
    gen_response = requests.post(gen_token_url, headers=headers, json=payload)
    if gen_response.status_code != 200:
        return f"Error: Failed to get embed token: {gen_response.status_code} {gen_response.text[:200]}"
    embed_token = gen_response.json().get("token")
    if not embed_token:
        return "Error: No embed token returned."

    # Step 2: Use embed token to execute DAX query on the same dataset
    data = {"queries": [{"query": query}]}
    url = f"{POWERBI_API_URL}/groups/{workspace_id}/datasets/{dataset_id}/executeQueries"
    result = make_api_request(url, method="POST", data=data, token=embed_token)

    if "error" in result:
        return f"Error: {result['error']}"

    results = result.get("results", [])
    if results and "tables" in results[0]:
        return json.dumps(results[0]["tables"], indent=2)
    else:
        return

Please help me to fix the issue
1 ACCEPTED SOLUTION
Shahid12523
Community Champion
Community Champion

You’re getting 403 because you’re using an embed token — that only works for embedding, not for REST API queries.

 

Fix: Use an Azure AD access token (via MSAL) with scope
https://analysis.windows.net/powerbi/api/.default

Then call executeQueries directly with that token.

 

 Remove the GenerateToken step.

Shahed Shaikh

View solution in original post

2 REPLIES 2
Shahid12523
Community Champion
Community Champion

You’re getting 403 because you’re using an embed token — that only works for embedding, not for REST API queries.

 

Fix: Use an Azure AD access token (via MSAL) with scope
https://analysis.windows.net/powerbi/api/.default

Then call executeQueries directly with that token.

 

 Remove the GenerateToken step.

Shahed Shaikh
GilbertQ
Super User
Super User

Hi @Arya_A_R 

 

I would highly recommend using Semantic Link Labs which will make it a lot easier to get your daxquery results. Here is a link where you can start. Code Examples · microsoft/semantic-link-labs Wiki 

 

And here is the command to run the DAX query: sempy_labs package — semantic-link-labs 0.11.3 documentation





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.