Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago

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-Report-in/ba-p/2179741

 

# 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-194d-4007-b504-abef3907d339/ExportTo?filter=FarmData/RegisteredUserId=114


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-4007-b508-abef3907d306/exports/MS9CbG9iSWRWMi1kMGMyODA0My00MmIyLTRkN2QtYThlOC03NmNjMjIwZmM1MGNRSDgxSXpqdi5BZ2FMaFFPSnAu?filter=FarmData/RegisteredUserId=114


Step 3 - Download PPTX

 

https://api.powerbi.com/v1.0/myorg/groups/56811d8e-a609-435e-853e-c813fb83b7b8/reports/f0b99643-194d-4007-b508-abef3907d306/exports/MS9CbG9iSWRWMi1kMGMyODA0My00MmIyLTRkN2QtYThlOC03NmNjMjIwZmM1MGNRSDgxSXpqdi5BZ2FMaFFPSnAu/file?filter=FarmData/RegisteredUserId=114

 

Filter also added in power BI Report and screenshot Attched

 

 


With Regards

Bipin Kumar

 

4 Replies

  • v-chenwuz-msft's avatar
    v-chenwuz-msft
    Community Support

    Hi Anonymous ,

     

    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.

     

    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.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      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

       

      With Regards

      Bipin Kumar

      • NaveenMohan's avatar
        NaveenMohan
        Frequent Visitor

        Hi Bipin Kumar,

         

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

         

        Regards,

        Naveen Mohan

  • 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()