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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. 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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

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

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.

Top Kudoed Authors