A Python wrapper for Power BI REST API with Pandas integration
Project description
pbipandas
pbipandas is a powerful, modular Python client for the Power BI REST API that helps you authenticate and retrieve data directly into Pandas DataFrames.
โจ Now with modular architecture for better maintainability and targeted functionality!
๐ Features
- ๐๏ธ Comprehensive Metadata: Get details for all Power BI items (datasets, dataflows, reports, refresh logs, datasources)
- ๐ Easy Authentication: OAuth2 authentication using client credentials
- ๐ Pandas Integration: Seamless integration with Pandas DataFrames
- ๐งฉ Modular Design: Use specific modules or the unified client
- ๐ Built-in Help: Discover all functions with
client.info() - โก Bulk Operations: Retrieve data across all workspaces efficiently
- ๐ Refresh Management: Trigger and monitor dataset/dataflow refreshes
- ๐ DAX Queries: Execute DAX queries against datasets
๐ฆ Installation
pip install pbipandas
Or for development:
git clone https://github.com/hoangdinh2710/pbipandas.git
cd pbipandas
pip install -e .
๐ง Quick Start
๐ Discover All Functions
import pbipandas
# Get comprehensive help about all available functions
pbipandas.info()
๐ฏ Option 1: Unified Client (Recommended for Most Users)
from pbipandas import PowerBIClient
# Initialize client
client = PowerBIClient(tenant_id, client_id, client_secret)
# Get help about all available methods
client.info()
# Get all workspaces and datasets
workspaces = client.get_all_workspaces()
datasets = client.get_all_datasets()
# Execute DAX queries
result = client.execute_query(workspace_id, dataset_id, "EVALUATE VALUES(Table[Column])")
# Refresh operations
client.refresh_dataset(workspace_id, dataset_id)
refresh_history = client.get_dataset_refresh_history_by_id(workspace_id, dataset_id)
# Bulk operations across all workspaces
all_sources = client.get_all_dataset_sources()
all_tables = client.get_all_dataset_tables()
# Get measures for specific datasets across all workspaces
dataset_ids = ["dataset1", "dataset2", "dataset3"]
measures = client.get_measures_for_dataset_ids_across_workspaces(dataset_ids)
๐งฉ Option 2: Modular Approach (For Targeted Functionality)
from pbipandas import WorkspaceClient, DatasetClient, BulkClient
# Use specific clients for targeted functionality
workspace_client = WorkspaceClient(tenant_id, client_id, client_secret)
workspaces = workspace_client.get_all_workspaces()
dataset_client = DatasetClient(tenant_id, client_id, client_secret)
dataset_client.refresh_dataset(workspace_id, dataset_id)
result = dataset_client.execute_query(workspace_id, dataset_id, "EVALUATE INFO.TABLES()")
bulk_client = BulkClient(tenant_id, client_id, client_secret)
all_datasets = bulk_client.get_all_datasets()
๐ Option 3: Individual Operations
from pbipandas import DatasetClient, extract_connection_details
# Just dataset operations
dataset_client = DatasetClient(tenant_id, client_id, client_secret)
# Get dataset metadata
metadata = dataset_client.get_dataset_by_id(workspace_id, dataset_id)
tables = dataset_client.get_dataset_tables_by_id(workspace_id, dataset_id)
columns = dataset_client.get_dataset_columns_by_id(workspace_id, dataset_id)
# Get measures for multiple datasets in workspace
dataset_ids = ["dataset1", "dataset2", "dataset3"]
measures = dataset_client.get_measures_for_datasets(workspace_id, dataset_ids)
# Refresh specific tables
dataset_client.refresh_tables_from_dataset(workspace_id, dataset_id, ["Table1", "Table2"])
๐ Available Modules
| Module | Purpose | Key Functions |
|---|---|---|
PowerBIClient |
All-in-one client | Everything below combined |
WorkspaceClient |
Workspace operations | get_all_workspaces(), get_workspace_users_by_id() |
DatasetClient |
Dataset operations | execute_query(), refresh_dataset(), get_dataset_*(), get_measures_for_datasets() |
ReportClient |
Report operations | get_report_by_id(), get_report_sources_by_id() |
DataflowClient |
Dataflow operations | refresh_dataflow(), get_dataflow_*() |
BulkClient |
Bulk data retrieval | get_all_*() functions |
๐ฏ Common Use Cases
๐ Data Discovery & Analysis
from pbipandas import PowerBIClient
client = PowerBIClient(tenant_id, client_id, client_secret)
# Get overview of all Power BI assets
workspaces = client.get_all_workspaces()
datasets = client.get_all_datasets()
reports = client.get_all_reports()
# Analyze data sources across organization
all_sources = client.get_all_dataset_sources()
print(f"Found {len(all_sources)} data sources across {all_sources['workspaceName'].nunique()} workspaces")
๐ Automated Refresh Management
# Monitor and trigger refreshes
refresh_history = client.get_all_dataset_refresh_history()
failed_refreshes = refresh_history[refresh_history['status'] == 'Failed']
# Refresh specific datasets
for workspace_id, dataset_id in failed_datasets:
client.refresh_dataset(workspace_id, dataset_id)
๐ DAX Query Execution
# Execute custom DAX queries
dax_query = """
EVALUATE
TOPN(10,
ADDCOLUMNS(
VALUES(Product[Category]),
"Sales", [Total Sales]
),
[Sales], DESC
)
"""
result = client.execute_query(workspace_id, dataset_id, dax_query)
๐๏ธ Schema Documentation
# Document all dataset schemas
all_tables = client.get_all_dataset_tables()
all_columns = client.get_all_dataset_columns()
all_measures = client.get_all_dataset_measures()
# Create comprehensive data dictionary
schema_doc = all_columns.merge(all_tables, on=['datasetId', 'tableName'])
๐๏ธ Architecture
pbipandas uses a clean, modular architecture with proper inheritance:
pbipandas/
โโโ auth/ # Authentication (BaseClient)
โโโ utils/ # Utilities (connection helpers, info)
โโโ workspace/ # Workspace operations
โโโ dataset/ # Dataset operations + DAX queries
โโโ report/ # Report operations
โโโ dataflow/ # Dataflow operations
โโโ bulks/ # Bulk retrieval across all workspaces
โโโ client.py # Unified PowerBIClient
Inheritance Structure:
PowerBIClientinherits fromWorkspaceClient,DatasetClient,ReportClient,DataflowClient, andBulkClientBulkClientinherits fromBaseClientand creates instances of individual clients for bulk operations- All individual clients inherit from
BaseClientfor authentication
This design allows you to:
- Import only what you need for smaller footprint
- Maintain code easily with clear separation of concerns
- Extend functionality by adding new modules
- Use familiar patterns with consistent APIs across modules
๐งช Development
Running Tests
pytest
Linting and Formatting
flake8 .
black .
๐ License
๐ง Prerequisites
- Python 3.7+
- Power BI Pro or Premium license
- Azure App Registration with Power BI API permissions
- Required credentials:
tenant_id,client_id,client_secret
๐ก๏ธ Authentication Setup
- Register an app in Azure Active Directory
- Grant Power BI Service API permissions
- Get your tenant ID, client ID, and client secret
- Use with pbipandas:
from pbipandas import PowerBIClient
client = PowerBIClient(
tenant_id="your-tenant-id",
client_id="your-client-id",
client_secret="your-client-secret"
)
๐ Learning Resources
- Built-in Help:
pbipandas.info()orclient.info() - Power BI API: Official REST API Documentation
๐ Contributing
Pull requests are welcome! Please open an issue first to discuss what you would like to change.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
โจ Reference
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pbipandas-1.0.1.tar.gz.
File metadata
- Download URL: pbipandas-1.0.1.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df55edf5160483844468883786cb24ae4e0147ac354316d937883cf2c4d14b4e
|
|
| MD5 |
78537a73852efdf97dabab0b5e6d70f4
|
|
| BLAKE2b-256 |
b66dfeb644480a4380dad17fcf3a6cea62f0b25b2b51ddb37dd5c9d41ef7119b
|
File details
Details for the file pbipandas-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pbipandas-1.0.1-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bbb9b667722fd3c04ebb8c71fba2d3be7fcb04ea56727d423af46dc0a9d4704
|
|
| MD5 |
053c298e38fe0584e9b1efe9a0d332c1
|
|
| BLAKE2b-256 |
c6d2ce7b4a5d9ab79ec5a87433c888458eab3646d85550730042b713cfa8d835
|