Pass parameter to DAX query with Power BI REST API and Python

35 views Asked by At

The following Python script is written to access the data in the Power BI data warehouse externally.

import json
import requests
from cred import username, password, client_id, client_secret, scope, token_endpoint,url
from dax_query import dax_query

payload = {
    "grant_type": "password",
    "client_id": client_id,
    "client_secret": client_secret,
    "resource": scope,
    "username": username,
    "password": password,
}

# Define the getToken function
def getToken():
    response = requests.post(token_endpoint, data=payload)
    response_data = response.json()
    access_token = response_data.get("access_token")

    if access_token:
        return access_token
    else:
        print("Failed to obtain access token.")
        exit(1)

# Call getToken to get the access token
access_token = getToken()

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

payload_data = {
    "queries": [
        {
            "query": dax_query,
        }
    ],
    "serializerSettings": {
        "includeNulls": True,
    },
}
response = requests.post(url, headers=headers, data=json.dumps(payload_data))
print(response.text)
print("Response Status Code:", response.status_code)  # Add this line for debugging

if response.status_code == 200:
    print("Response Text:", response.text)
else:
    print("Error Occurred. Response Text:", response.text)

The relavent DAX query (dax_query) is included in dax_query.py file

dax_query = "Evaluate SUMMARIZE( 'axp_gw_succes_faild', 'axp_gw_succes_faild'[Date], 'axp_gw_succes_faild'[api_publisher], 'axp_gw_succes_faild'[application_name], 'axp_gw_succes_faild'[api_name],\"TotalCount\", CALCULATE (SUM ( axp_gw_succes_faild[Count] ),FILTER('Publisher-UserDetails','Publisher-UserDetails'[BAdashboard_authoritylevel] = \"Publisher\"|| 'Publisher-UserDetails'[BAdashboard_authoritylevel] = \"publisherTechLead\"|| 'Publisher-UserDetails'[BAdashboard_authoritylevel] = \"publisherArchitect\"||'Publisher-UserDetails'[BAdashboard_authoritylevel] = \"ProductOwner\"),'Publisher-UserDetails'[User-Email]=@user, GROUPBY (axp_gw_succes_faild,axp_gw_succes_faild[api_name],axp_gw_succes_faild[Date] )))"

I want to send the user as a parameter value 'Publisher-UserDetails'[User-Email]=@user)

Can You suggest a solution to improve the script?

0

There are 0 answers