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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.