Power BI REST API - Unable to get visuals and fields within report page

441 Views Asked by At

I am looping across power bi reports>pages>visuals and then fields within them , the code to retrieve pages works fine, however there is an issue with visual url, in Visual response it says - No HTTP request was found that matches the url.

I get an issue in below code

visuals_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/reports/{report_id}/pages/{page_name}/visuals"
visuals_response = requests.get(visuals_url, headers=headers)

Below is the full block of code, is there an issue with visuals_url in below code?

import requests

# Define the necessary variables
workspace_id = "your_workspace_id"
report_id = "your_report_id"
access_token = "your_access_token"

# Define the headers with the access token
headers = {
    "Authorization": f"Bearer {access_token}"
}

# Define the URL for pages
pages_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/reports/{report_id}/pages"

# Send a GET request to retrieve pages
response = requests.get(pages_url, headers=headers)
pages = response.json()["value"]

# Extract page information
for page in pages:
    page_name = page["name"]
    page_display_name = page["displayName"]
    print(f"Page Name: {page_name}, Display Name: {page_display_name}")

    # Define the URL for visuals within the current page
    visuals_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/reports/{report_id}/pages/{page_name}/visuals"

    # Send a GET request to retrieve visuals
    visuals_response = requests.get(visuals_url, headers=headers)
    visuals = visuals_response.json()["value"]

    # Extract visual information
    for visual in visuals:
        visual_name = visual["name"]
        visual_display_name = visual["displayName"]
        print(f"Visual Name: {visual_name}, Display Name: {visual_display_name}")
1

There are 1 best solutions below

0
On

Instead of using Rest API you can use powerbiclient package. Follow the following steps to get visuals and fields within report page:

  1. Install power bi clients

    pip install powerbiclient

  2. Get the active page from the embedded report.

     def get_active_page(self):
         // Get list of pages
         pages = self.get_pages()
         active_page = {}
         for page in pages:
             // Get active page
             if page['isActive'] == True:
                 active_page = page
                 break
             return active_page
    
  3. To get the a specific visual on the page use the visuals_on_page() from powerbiclient.

     active_page = get_active_page(report)
     active_page_name = active_page['name']
     // Get list of visuals on active page
     visuals = report.visuals_on_page(active_page_name)
     // Get visual whose type is 'clusteredColumnChart'
     visual = next(filter(lambda visual: visual['type'] == 'clusteredColumnChart', visuals))
     visual_name = visual['name']
    
  4. Once the visual is selected, use the export_visual_data() from the powerbiclient to get the data fields of the visual.

     summarized_exported_data = report.export_visual_data(active_page_name, 
     visual_name, rows = 20)
     print(summarized_exported_data)
    

Output image:

Output image