Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Paginated Report as a Function #36

Open
jugi92 opened this issue Jul 18, 2024 · 2 comments
Open

Download Paginated Report as a Function #36

jugi92 opened this issue Jul 18, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@jugi92
Copy link

jugi92 commented Jul 18, 2024

Problem
Running and exporting paginated reports into Excel and PDFs via Notebook

Solution
A simple function to run the report and specify the output path.
Example code to be integrated into Sempy:

def download_paginated_report(group_id:str, report_id:str, file_format:str="PDF", target_folder="/lakehouse/default/Files/", filename="report.pdf", report_parameters:dict={}):
    import requests
    import json
    import time
    import os

    token = mssparkutils.credentials.getToken('pbi')
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-type': 'application/json'
    }

    payload = {
        "format": file_format
    }

    url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/ExportTo?"
    for key, value in report_parameters.items():
        url = url + f"rp:{key}={value}&"
    
    print(url)
    export_response = requests.post(url, data=json.dumps(payload), headers=headers)
    export_response.raise_for_status()

    export_id = export_response.json()["id"]

    timeout = 60
    counter = 0
    wait_time = 5
    while True:
        url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/exports/{export_id}"
        status_response = requests.get(url, headers=headers)
        if status_response.json()["status"] == "Succeeded":
            break
        if counter > timeout:
            raise(TimeoutError("Request timed out"))
        counter = counter + wait_time
        time.sleep(wait_time)

    url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/exports/{export_id}/file"

    report_response = requests.get(url, headers=headers)
    report_response.raise_for_status()
    os.chdir(target_folder)
    with open(filename, 'wb') as f:
        f.write(report_response.content)
    print(f"export saved as {filename} in {target_folder}")

Alternatives
Keeping the above mentioned code in a notebook and maintaining it, but I would prefer to have it in semantic-link-labs to scale the usage.

@jugi92 jugi92 added the enhancement New feature or request label Jul 18, 2024
@m-kovalsky
Copy link
Collaborator

m-kovalsky commented Jul 18, 2024 via email

@jugi92
Copy link
Author

jugi92 commented Aug 7, 2024

Yes, I tried it and created two PRs to enhance the functionality:
#65
#67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants