Skip to content

Examples

JChamboredon edited this page Sep 27, 2023 · 13 revisions

Interaction with Power BI service

Through the python-pbi package, you can interact with the Power BI service to download and upload reports, etc.

Configuration

Add all the required configuration to create the Pbi object like so:

config = {
    'DOWNLOAD_FOLDER': './downloads/',
    'UPLOAD_FOLDER': './upload/'
}

Download a report

You can download a report from the Power BI service like so:

from pbi.pbi import PowerBI
from tests.config import config

test_pbi = PowerBI(config)

report = test_pbi.download(
    'test_report',
    test_pbi.download_folder,
    workspace_id=<your_powerbi_workspace_id>
)

NB: Power BI service will ask you to authenticate and download all reports containing the string 'test_report'.

Upload a report

You can upload a report to the Power BI service like so:

from pbi.pbi import PowerBI
from tests.config import config

test_pbi = PowerBI(config)

test_pbi.upload(
    test_pbi.upload_folder,
    workspace_id=<your_workspace_id>
)

NB: Power BI service will ask you to authenticate and upload all reports contained in the given folder to the Power BI workspace.

Change the dataset

You can save a PowerBI report with a different dataset connection. This enables automatic deployment from a development environment to the production environment for example. See an example below:

from pbi.pbi import PowerBI
from pbi.report import PbiReport
from tests.config import config

test_pbi = PowerBI(config)

# Download the report from the development workspace into an arbitrary folder
test_pbi.download(
    'test_report',
    'temp_folder',
    workspace_id=<development_workspace_id>
)

# Brings the report to memory with python-pbi
report = PbiReport(
    'temp_folder',
    'test_report'
)

# Changes the dataset from development to production dataset
report.save(
    dataset_id_from=<development_dataset_id>,
    dataset_id_to=<production_dataset_id>,
)

# Uploads the contents of the folder to the production workspace
test_pbi.upload(
    'temp_folder',
    workspace_id=<production_workspace_id>
)

Handling the report in Python

As already introduced above, the python-pbi package enables to create Python PbiReport objects to manipulate them.

Opening a report

Assume you have a Power BI report named test_report.pbix in the folder test_folder. You can bring this file to memory as a Python PbiReport object using the commands below:

from pbi.report import PbiReport

report = PbiReport(
    'test_folder',
    'test_report'
)

NB: At this stage, the Power BI report is accessible in memory. Please check the PbiReport section for a more detailed description.

Saving a report

Assume you have a PbiReport object in memory named report. After manipulating it, you can easily save it via the command below:

report.save()

NB: The save method also allows to change the dataset connection if needed.

Maintenance support

Tidying page IDs

Tidying bookmarks

Manipulating the report layout

Copying visuals

Copying pages

Copying bookmarks

Replacing visuals by some placeholder message

Formatting

Hiding visuals

Updating font

Manipulating slicers

Manipulating report filters

Updating the 'keep layer order' attribute

Disabling headers

Clone this wiki locally