Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
abhiram342
Microsoft Employee
Microsoft Employee

Power BI Dataset Refresh -- Add Partit Partition and Refresh Partition in Python

Hi Team,

 

We are using spark to process our data and was able to refresh Power BI Dataset in Python using PBI Rest API. However We want the ability to add and delete partitions in Power BI Dataset using Python. Can you please tell how to achieve partition management in Python?

 

Thanks,

Abhiram

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@abhiram342 , here powershell script has been used, I think you can use same with python

https://www.linkedin.com/pulse/approach-custom-partition-your-power-bi-dataset-refresh-rui-romano/?t...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

Adamboer
Responsive Resident
Responsive Resident

Yes, it is possible to manage partitions in Power BI datasets using Python. Here are the steps you can follow:

  1. Install the Power BI API client for Python: You can use the "powerbiclient" library to interact with the Power BI REST API from Python. To install this library, run the following command in your Python environment:

    pip install powerbiclient

    1. Authenticate with the Power BI service: Before you can manage partitions, you need to authenticate with the Power BI service. You can do this by creating an Azure Active Directory (AAD) application and obtaining a client ID and client secret. You can then use these credentials to authenticate with the Power BI service and get an access token that can be used to make API calls.

    2. Get the dataset ID: You need to know the ID of the Power BI dataset that you want to manage partitions for. You can get this ID by using the "get_datasets" API call and filtering the results by name.

    3. Add a partition: To add a partition, you can use the "add_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key. You can also specify optional parameters such as the partition query, refresh policy, and retention policy.

    4. Delete a partition: To delete a partition, you can use the "delete_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key.

    Here is an example Python code snippet that shows how to add and delete partitions in a Power BI dataset:

    from powerbiclient import Report, models
    from azure.identity import ClientSecretCredential
    import requests

    # Authenticate with the Power BI service
    tenant_id = '<your-tenant-id>'
    client_id = '<your-client-id>'
    client_secret = '<your-client-secret>'
    credential = ClientSecretCredential(tenant_id, client_id, client_secret)
    access_token = credential.get_token('https://analysis.windows.net/powerbi/api').token
    headers = {'Authorization': f'Bearer {access_token}'}

    # Get the dataset ID
    dataset_name = '<your-dataset-name>'
    response = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers)
    datasets = response.json()['value']
    dataset = next(d for d in datasets if d['name'] == dataset_name)
    dataset_id = dataset['id']

    # Add a partition
    dataflow_name = '<your-dataflow-name>'
    partition_key = '<your-partition-key>'
    partition_query = '<your-partition-query>'
    refresh_policy = models.DataflowRefreshPolicy('Daily', 1)
    retention_policy = models.DataflowRetentionPolicy(365, True)
    partition = models.DataflowPartition(partition_key, partition_query,


    Note that this is just an example and you may need to modify it to fit your specific use case. Also, make sure to handle errors and exceptions appropriately in your code.

     




View solution in original post

2 REPLIES 2
Adamboer
Responsive Resident
Responsive Resident

Yes, it is possible to manage partitions in Power BI datasets using Python. Here are the steps you can follow:

  1. Install the Power BI API client for Python: You can use the "powerbiclient" library to interact with the Power BI REST API from Python. To install this library, run the following command in your Python environment:

    pip install powerbiclient

    1. Authenticate with the Power BI service: Before you can manage partitions, you need to authenticate with the Power BI service. You can do this by creating an Azure Active Directory (AAD) application and obtaining a client ID and client secret. You can then use these credentials to authenticate with the Power BI service and get an access token that can be used to make API calls.

    2. Get the dataset ID: You need to know the ID of the Power BI dataset that you want to manage partitions for. You can get this ID by using the "get_datasets" API call and filtering the results by name.

    3. Add a partition: To add a partition, you can use the "add_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key. You can also specify optional parameters such as the partition query, refresh policy, and retention policy.

    4. Delete a partition: To delete a partition, you can use the "delete_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key.

    Here is an example Python code snippet that shows how to add and delete partitions in a Power BI dataset:

    from powerbiclient import Report, models
    from azure.identity import ClientSecretCredential
    import requests

    # Authenticate with the Power BI service
    tenant_id = '<your-tenant-id>'
    client_id = '<your-client-id>'
    client_secret = '<your-client-secret>'
    credential = ClientSecretCredential(tenant_id, client_id, client_secret)
    access_token = credential.get_token('https://analysis.windows.net/powerbi/api').token
    headers = {'Authorization': f'Bearer {access_token}'}

    # Get the dataset ID
    dataset_name = '<your-dataset-name>'
    response = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers)
    datasets = response.json()['value']
    dataset = next(d for d in datasets if d['name'] == dataset_name)
    dataset_id = dataset['id']

    # Add a partition
    dataflow_name = '<your-dataflow-name>'
    partition_key = '<your-partition-key>'
    partition_query = '<your-partition-query>'
    refresh_policy = models.DataflowRefreshPolicy('Daily', 1)
    retention_policy = models.DataflowRetentionPolicy(365, True)
    partition = models.DataflowPartition(partition_key, partition_query,


    Note that this is just an example and you may need to modify it to fit your specific use case. Also, make sure to handle errors and exceptions appropriately in your code.

     




amitchandak
Super User
Super User

@abhiram342 , here powershell script has been used, I think you can use same with python

https://www.linkedin.com/pulse/approach-custom-partition-your-power-bi-dataset-refresh-rui-romano/?t...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Solution Authors