Skip to main content

A Python wrapper for Power BI REST API with Pandas integration

Project description

pbipandas

CI PyPI

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:

  • PowerBIClient inherits from WorkspaceClient, DatasetClient, ReportClient, DataflowClient, and BulkClient
  • BulkClient inherits from BaseClient and creates instances of individual clients for bulk operations
  • All individual clients inherit from BaseClient for 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

MIT 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

  1. Register an app in Azure Active Directory
  2. Grant Power BI Service API permissions
  3. Get your tenant ID, client ID, and client secret
  4. 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

๐Ÿ™Œ Contributing

Pull requests are welcome! Please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

โœจ Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pbipandas-1.0.2.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pbipandas-1.0.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file pbipandas-1.0.2.tar.gz.

File metadata

  • Download URL: pbipandas-1.0.2.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pbipandas-1.0.2.tar.gz
Algorithm Hash digest
SHA256 6eb04a301d91c49dfe9273b2db3ae8b020d6a473523fd630e85037b550f6aaaf
MD5 9a2407df068713e98b9967ff1b2d07f5
BLAKE2b-256 3a33f5a61c842bbe02c0bbfdfdcf84134ac885461ec66e235b84d2933d962d33

See more details on using hashes here.

File details

Details for the file pbipandas-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pbipandas-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pbipandas-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fda85f25713ac4b3124386cb8b0dc7545accbf4195d245db7e062710e7f88af
MD5 f3d9badc0669959d0d400c0fa1694846
BLAKE2b-256 8b7eb917f6cba6db060ece929e4d67ea0f7c11b1663b80bc299b4bc0ba7b6036

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page