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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
spc-powerb
Frequent Visitor

How to Pass values to the Power BI Filter via API (while Generating PPTX from Power BI Report)

Hi Team,

I am able to generate PPTX and PDF from the API but I am not able to pass the filter to the report while generating PPTX. I tried and showing all the steps below

 

                                                         I have Generated PPTX from the given link

 

https://community.powerbi.com/t5/Community-Blog/How-to-use-Power-BI-Rest-API-to-export-the-Power-BI-...

 

# Passing Filters while generating PPTX

Step 1 - Get the exportId

 

https://api.powerbi.com/v1.0/myorg/groups/56811d8e-a609-435e-853e-c813fb83b7rQ8/reports/f0b99643-194...


Step 2 - Get the Current Status If the session is successful

 

https://api.powerbi.com/v1.0/myorg/groups/56811d8e-a609-435e-853e-c813fb83b7b8/reports/f0b99643-194d...


Step 3 - Download PPTX

 

https://api.powerbi.com/v1.0/myorg/groups/56811d8e-a609-435e-853e-c813fb83b7b8/reports/f0b99643-194d...

 

Filter also added in power BI Report and screenshot Attched

 

spcpowerb_0-1645535617147.png

 


With Regards

Bipin Kumar

 

4 REPLIES 4
satishkumar123
Regular Visitor

Hi Team, I am using the Power BI trail account and wants to fetch the filtered Report content. But at the end I am getting long HTML reponse. Below is my Python Code. Please let me know where I am wrong here.

 

Below is the code-

import requests

def get_access_token(client_id, client_secret, username, password, scope, token_url):
data = {
'grant_type': 'password',
'client_id': client_id,
'client_secret': client_secret,
'username': username,
'password': password,
'scope': scope
}
response = requests.post(token_url, data=data)
print(response.text)
access_token = response.json().get('access_token')
print(access_token)
return access_token

def get_embed_token(access_token, report_id, filters, dataset_id, group_id):
url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/GenerateToken"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
body = {
'accessLevel': 'View',
'datasetId': dataset_id,
'filters': filters # Corrected to pass as a dictionary
}
response = requests.post(url, headers=headers, json=body)
print(response.text)
embed_token = response.json().get('token')
print(embed_token)
return embed_token

def fetch_report_data(emb_url, embed_token):
headers = {'Authorization': f'Bearer {embed_token}',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.get(emb_url, headers=headers)
print(response.status_code)
print(emb_url)
report_data = response.text
#report_data =response.json()
return report_data

def main():
# Power BI credentials and parameters
client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
username = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
scope = 'https://analysis.windows.net/powerbi/api/.default'
token_url = 'https://login.microsoftonline.com/65df6efa-f78b-4336-9681-654c7fd66c53/oauth2/v2.0/token'
report_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
group_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
emb_url = f'https://app.powerbi.com/reportEmbed?reportId={report_id}&groupId={group_id}'
dataset_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# Filters to apply
filters = {
'name': 'Id',
'operator': 'eq',
'value': '00018bcgypygtvgt15'
}

# Obtain access token
access_token = get_access_token(client_id, client_secret, username, password, scope, token_url)

# Get embed token
embed_token = get_embed_token(access_token, report_id, filters, dataset_id, group_id)

# Fetch report data
report_data = fetch_report_data(emb_url, embed_token)

print(report_data)

if __name__ == "__main__":
main()

v-chenwuz-msft
Community Support
Community Support

Hi @spc-powerb ,

 

You want to add filter on the page then export the report in format of pptx.

Maybe you can try add json in the body of this POST request. You can refer this article about REST API.

vchenwuzmsft_0-1645774084447.png

 

Reports - Export To File In Group - REST API (Power BI Power BI REST APIs) | Microsoft Docs

 

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Hi Team,

              I have added json in body of this post request which has given below but filter is not working

 

{
format: "PPTX",
filter:"FarmData/RegisteredUserId eq 122"
}

 

Filter also added in power BI Report and screenshot Attched

spcpowerb_1-1646239594421.png

 

With Regards

Bipin Kumar

Hi Bipin Kumar,

 

Does the issue got resolved?. If yes can you share the solution please!.

 

Regards,

Naveen Mohan

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors