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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.