Skip to main content

pbipy

A Power BI Rest API wrapper for Python.

pbipy aims to simplyify working with the Power BI Rest API and support programatic administration of Power BI in Python.

NOTE: At the moment pbipy is only implementing GET based methods, i.e., getting data from the Rest API. Functionality to update and create new items on the Power BI Instance will be implemented soon.

Installation

pip install pbipy

Authentication

To use pbipy you'll first need to acquire a bearer_token.

How do I get a bearer_token?

To acquire a bearer_token you'll need to authenticate against your Registered Azure Power BI App. Registering is the first step in turning on the Power BI Rest API, so from this point on we're assuming that registration is taken care of.

To authenticate against the Registered App, Microsoft provides the MSAL and azure-identity python libraries. These libraries support many different ways of acquiring a bearer_token.

Because there are multiple ways to acquire the token, pbipy assumes you'll do this in the way that suits, rather than directly handling authentication (of course, this might change in future).

This README doesn't cover Authentication in detail, however, these are some helpful resources that look at acquiring a bearer_token in the context of Power BI:

See below for an example that uses the msal library.

Usage

Start by creating the PowerBI() client. All interactions with the Power BI Rest API go through this object.

The example uses msal authentication methods to get a bearer_token.

import msal

from pbipy import PowerBI


def acquire_bearer_token(username, password, azure_tenant_id, client_id, scopes):
    app = msal.PublicClientApplication(client_id, authority=azure_tenant_id)
    result = app.acquire_token_by_username_password(username, password, scopes)
    return result["access_token"]


bearer_token = acquire_bearer_token(
    username="your-username",
    password="your-password",
    azure_tenant_id="https://login.microsoftonline.com/your-azure-tenant-id",
    client_id="your-pbi-client-id",
    scopes=["https://analysis.windows.net/powerbi/api/.default"],
)

# Create Client
pbi = PowerBI(bearer_token)

Get all groups (workspaces).

groups = pbi.get_groups()
print(groups[0])

# Group(id='3d9b93c6-7b6d-4801-a491-1738910904fd', name='marketing group', type='Workspace', ...)

What about the refresh history of a Dataset? Most methods support passing in an object id...

refresh_history = pbi.get_refresh_history("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(refresh_history[0])

# Refresh(id=None, request_id='9399bb89-25d1-44f8-8576-136d7e9014b1', refresh_type='ViaApi', ...)

... or just pass in the object itself.

sales = pbi.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
refresh_history = pbi.get_refresh_history(sales)
print(refresh_history[0])

# Refresh(id=None, request_id='9399bb89-25d1-44f8-8576-136d7e9014b1', refresh_type='ViaApi', ...)

pbipy converts API responses into Python objects.

sales = pbi.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(type(sales))

# <class 'pbipy.models.Dataset'>

To make life easier, attributes are also translated into sensible types.

sales = pbi.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(isinstance(sales.created_date, datetime))

# True

Objects are standardized to have consistent attributes, even when a key is missing from an API response.

sales = Dataset(id="cfafbeb1-8037-4d0c-896e-a46fb27ff229", name="SalesMarketing")

print(hasattr(sales, "is_refreshable"))
print(sales.is_refreshable)

# True
# None

And if you need to access the raw json representation, this is supported to.

sales = pbi.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(sales.raw)

# {'id': 'cfafbeb1-8037-4d0c-896e-a46fb27ff229', 'name': 'SalesMarketing', 'addRowsAPIEnabled': False, ...},

Power BI Rest API Reference

In general, most of the PowerBI() methods follow the resources laid out in the Power BI Rest API Reference:

Power BI REST APIs for embedded analytics and automation - Power BI REST API

Acknowledgements

The design of this library was inspired by the pycontribs/jira library. A personal Thank You to all the contributors to this library for a fantastic example of what an API Wrapper can be.

Metadata

Release files for pbipy 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pbipy 0.2.3
File Size Uploaded
pbipy-0.2.3.tar.gz 15.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pbipy 0.2.3
File Interpreter ABI Platform
pbipy-0.2.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 34.1 kB

Release files / pbipy-0.2.3.tar.gz

Download URL pbipy-0.2.3.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
87d45d8b910bb31d30a1cf28dc43a79804e66649884efdf038ab058c1d6561a3
BLAKE2b-256 checksum
How to use checksums
9cf0942247a63c70b18e29cdca622908603fcfdb32421bc61c6663b6e12bdd04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / pbipy-0.2.3-py2.py3-none-any.whl

Download URL pbipy-0.2.3-py2.py3-none-any.whl
Size 18.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3b3eff3207b6f96eecdc92591ef56c42d0acb169d71b3288f56b6c083ede9162
BLAKE2b-256 checksum
How to use checksums
9892c4f0a3e90e111b10af1b2ec7601837f9682d90154d5e5e61264703c0b824
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

2.13.0

2 release files

2.12.0

2 release files

2.11.0

2 release files

2.9.1

2 release files

2.9.0

2 release files

2.8.2

2 release files

2.6.0

2 release files

2.5.9

2 release files

2.5.7

2 release files

2.5.6

2 release files

2.4.5

2 release files

2.3.4

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.1.3

2 release files

2.0.3

2 release files

1.1.1

1 release file

1.0.0

3 release files

This release

0.2.3 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page