Skip to main content

pbipy

PyPI GitHub tag (latest SemVer) PyPI - Python Version GitHub Static Badge Static Badge

pbipy is a Python Library for interacting with the Power BI Rest API. It aims to simplyify working with the Power BI Rest API and support programatic administration of Power BI in Python.

* Quick note: pbipy is currently in active development and not all API functionality is supported yet. See development progress below for what's been implemented and what's coming.

Installation

pip install pbipy

Or to install the latest development code:

pip install git+https://github.com/andrewvillazon/pbipy

Getting Started: 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 it's assumed your Power BI Rest API is up and running.

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:

The example below uses the msal library to to get a bearer_token.

Quickstart

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

import msal

from pbipy import PowerBI


#  msal auth setup
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)

Functionality

To interact with the API, simply call the relevant method from the client.

# Grab the datasets from a workspace

pbi.datasets(group="f089354e-8366-4e18-aea3-4cb4a3a50b48")

pbipy converts API responses into regular Python objects, with snake case included! 🐍🐍

sales = pbi.dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")

print(type(sales))
print(hasattr(sales, "configured_by"))

# <class 'pbipy.resources.Dataset'>
# True

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

sales = pbi.dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")

print(sales.raw)

# {
#   "id": "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
#   "name": "SalesMarketing",
#   "addRowsAPIEnabled": False,
#   "configuredBy": "john@contoso.com",
#   ...
# }

Examples

Datasets

Get a dataset

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

# <Dataset id='cfafbeb1-8037-4d0c-896e-a46fb27ff229', name='SalesMarketing', ...>

Get a list of Datasets in a Group (aka Workspace)

datasets = pbi.datasets(group="f089354e-8366-4e18-aea3-4cb4a3a50b48")

for dataset in datasets:
    print(dataset)

# <Dataset id='cfafbeb1-8037-4d0c-896e-a46fb27ff229', ...>
# <Dataset id='f7fc6510-e151-42a3-850b-d0805a391db0', ...>

Refresh History of a Dataset

dataset = pbi.dataset(
    "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
    group="f089354e-8366-4e18-aea3-4cb4a3a50b48",
)

refresh_history = dataset.refresh_history()

for entry in refresh_history:
    print(entry)

# {"refreshType":"ViaApi", "startTime":"2017-06-13T09:25:43.153Z", "status": "Completed" ...}

Add user permissions to a Dataset

sales_ds = pbi.dataset( "cfafbeb1-8037-4d0c-896e-a46fb27ff229")

# Give John 'Read' access on the dataset
sales_ds.add_user("john@contoso.com", "User", "Read")

Execute a DAX Query

dataset = pbi.dataset( "cfafbeb1-8037-4d0c-896e-a46fb27ff229")

dxq_result = dataset.execute_queries("EVALUATE VALUES(MyTable)")
print(dxq_result)

# {
#   "results": [
#     {
#       "tables": [
#         {
#           "rows": [
#             {
#               "MyTable[Year]": 2010,
#               "MyTable[Quarter]": "Q1"
#             },
# ...
# }

Power BI Rest API Operations

pbipy methods wrap around the Operations described in the Power BI Rest API Reference:

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

Development Progress

pbipy is in the early stages of development so expect a few features to be missing. The aim is to cover off most of the core stuff like Datasets, Workspaces (Groups), Reports, Apps, etc., and the rest later on. Check back regularly to see what's been added or still in the pipeline.

PowerBI Component Progress Notes
Datasets Done
Groups (Workspaces) Doing
Reports Next
Apps Todo
Dataflows Todo
Dashboards Todo
Everything else Backlog

Contributing

Contributions such as bug reports, fixes, documentation or docstrings, enhancements, and ideas are welcome. pbipy uses github to host code, track issues, record feature requests, and accept pull requests.

A contributing.md is in the works, but in the meantime below is a general guide.

Making a contribution

Pull requests are the best way to make a contribution to the code:

  1. Fork the repo and create your branch from master.
  2. If you've added code that should be tested, add tests.
  3. Add docstrings.
  4. Ensure the test suite passes.
  5. Format your code (pbipy uses black.
  6. Issue that pull request!

Reporting a bug

Great Bug Reports tend to have:

  • A quick summary and/or background
  • Steps to reproduce. Be specific! Give sample code if you can.
  • What you expected would happen
  • What actually happens
  • Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

Acknowledgements

The design of this library was heavily inspired by (basically copied) the pycontribs/jira library. It also borrows elements of cmberryay's pypowerbi wrapper.

Thank You to all the contributors to these libraries for the great examples of what an API Wrapper can be.

Metadata

Release files for pbipy 2.0.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 2.0.3
File Size Uploaded
pbipy-2.0.3.tar.gz 20.4 kB Details

Built distribution (wheel)

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

Total release size: 51.2 kB

Release files / pbipy-2.0.3.tar.gz

Download URL pbipy-2.0.3.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c81722442eafd618ebc0ddc1efadc1a0ac3dd4e1249a9552d58bef3e5d8036ef
BLAKE2b-256 checksum
How to use checksums
7154869bff667908d6786cb7bd610f1ea0023143fd3811e33cfc18e7bb90ea0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

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

Download URL pbipy-2.0.3-py2.py3-none-any.whl
Size 30.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
21f2670c57dceecba2bffa398baa4c28f5105c7d3a751023d509cd4488d6ee19
BLAKE2b-256 checksum
How to use checksums
fa8f651620119aa8f372db6e86e6d74718521753f8d59d9986e5c91e7c91e0d2
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

This release

2.0.3 This release

2 release files

1.1.1

1 release file

1.0.0

3 release files

0.2.3

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