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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
yazdanb
New Member

Can I expand my Python script to retrieve information about visuals and datasets on a page?

Hey everyone! I currently have a Python script that makes an API call to my team's workspace to retrieve two things:

 

1. A list of all reports/dashboards that current exist and are being worked on

2. A sublist of every page within each report/dashboard

 

My next requirement is to expand this script to retrieve the visual information off of each page - the number of visuals on the page, which type of visuals/elements are they, what data sources are they connected to, how many slicers and buttons are there, where is everything positioned/placed etc. The goal is to have an executable that retrieve and consolidate the information of our workspace in a simple, text-based format so we have all our "metadata" in one place to analyze and keep track of.


Here is my script so far (assuming imports and constants):

def getReportsInGroup(GROUP_ID):
    API_URL = f"https://api.powerbi.com/v1.0/myorg/groups/{GROUP_ID}/reports"
    headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
    response = requests.get(API_URL, headers=headers)

    if response.status_code == 200:
        print("Retrieved report metadata:")
        reports = response.json()['value']
        
        for report in reports:
            print(f"{report['name']} | Dataset ID = {report['id']}")
            getPagesInReport(report['id'])
    
    else:
        print(f"Failed to retrieve metadata. Status code: {response.status_code}")
        try:
            error_content = response.json()
            print(json.dumps(error_content, indent=4))
        except requests.exceptions.JSONDecodeError:
            print("No JSON response content.")
            print(f"Response text: {response.text}")

def getPagesInReport(DATASET_ID):
    API_URL = f"https://api.powerbi.com/v1.0/myorg/groups/{GROUP_ID}/reports/{DATASET_ID}/pages"
    headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
    response = requests.get(API_URL, headers=headers)

    if response.status_code == 200:
        pages = response.json()['value']
        sortedPages = sorted(pages, key=lambda x: x['order'])
        
        for page in sortedPages:
            print(f"--> {page['order']}) {page['displayName']}")
    else:
        print(f"Failed to retrieve metadata. Status code: {response.status_code}")
        try:
            error_content = response.json()
            print(json.dumps(error_content, indent=4))
        except requests.exceptions.JSONDecodeError:
            print("No JSON response content.")
            print(f"Response text: {response.text}")

 

Is it possible to continue using Python methods to get the information I am looking for? I have looked up some idea and saw that some people had luck with embedding reports and running JS on it to get information on what the user actually gets to see since the API calls themselves would not render the same dashboard. I would like to keep it simple, but understand that that is not always possible! Any guidance, tips, and leads will be appreciated! Thank you!

1 REPLY 1
lbendlin
Super User
Super User

Any particular reason for not using the sempy library?

 

Semantic link propagation with SemPy - Microsoft Fabric | Microsoft Learn

Helpful resources

Announcements