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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
nphadro07
Frequent Visitor

Back up my semantic model every day

Guy in a cube shows how to back up datasets as a .abf file to adls (Azure Data Lake Storage) using powershell. The short youtube video is here: https://www.youtube.com/watch?v=RbMFQNthlGM&ab_channel=GuyinaCube

 

I've done this but I am working to migrate everything to fabric and using Fabric notebooks. I have spent several days trying to translate this into Python but I can't figure it out. Can someone help me? I got it working with powershell but not Python. Thanks for the help.


Here is the powershell script 

https://github.com/guyinacube/demo-files/blob/master/video%20demos/PowerShell/20220413%20-%20PS%20Ba...

 

1 ACCEPTED SOLUTION

Hi @nphadro07 ,

 Thank you for sharing additional details.

  • In your current setup, you're trying to use the Power BI REST API to back up datasets, which isn't the right tool for this job. To back up datasets, you should use the XMLA endpoint in Power BI, which supports the backup operations you need.
  • The PowerShell script uses the XMLA endpoint by calling the correct URL with PowerShell commands, which are built to support this interaction. However, in Python, you must manually manage the endpoint, headers, and body to achieve the same result.
  • The 404 error is probably happening because the endpoint for the backup operation is incorrect
  • Double-check that the WORKSPACE_ID variable is the same as your actual workspace ID in Power BI. Make sure it's in the correct format (GUID).
  • Make sure that the database name variable exactly matches the name of the dataset you want to back up in your workspace.

Please follow the steps outlined below to resolve your issue.

  • Go to the Power BI Service and open your workspace.
  • Copy the Workspace Connection URL from the Workspace Settings under Premium.
  • Make sure your Python script appends /XMLA to the endpoint URL.
  • The request body now needs to be in XML format, as required by the XMLA endpoint. So, replace the JSON payload in your script with an XMLA query
  • Update your Python script to include the XML query in the request.
  • Decode your access token using jwt.io to verify it has the necessary permissions for XMLA operations.

For more detailed information, please refer to the following links:

Semantic model connectivity and management with the XMLA endpoint in Power BI - Power BI | Microsoft....

Use Python experience on Notebook - Microsoft Fabric | Microsoft Learn.

 

Key deference:

Vyubandimsft_0-1736224606257.png

NOTE:  Make sure your workspace is in Premium or Premium Per User (PPU) capacity to support the XMLA endpoint. This is necessary to access the XMLA API.

 

Please let us know if you need further assistance!

If my response solved your query, please mark it as the Accepted solution to help others find it easily!

And if my answer was helpful, I'd really appreciate a 'Kudos'.

View solution in original post

5 REPLIES 5
V-yubandi-msft
Community Support
Community Support

Hi @nphadro07,

Thanks for reaching out to the Microsoft Fabric Community Forum.

We’re having a bit of trouble pinpointing the exact issue you’re facing while backing up your semantic model using Python. Could you please provide more details about where you’re getting stuck in python ? Any specific error messages or relevant information would significantly help us understand the problem better.

If you can, please share screenshots or code snippets showing the issue. This will help us assist you more effectively and offer precise solutions.

We’re here to help, so please don't hesitate to share further details for a better understanding of your query.

 

Thank You.

Thanks for the response @V-yubandi-msft  ,

Here is my Python code that I keep getting an error "Backup failed: 404 -". I'm relatively new to python so I appreciate thes upport. Also below the Python put in my powershell code that works. Any help is grately appreciated.

import requests
from datetime import datetime
import msal

# Configuration
APP_ID = ""  # HCH_Compass_PBI_Automation app registration service principal account
APP_SECRET = ""
TENANT_ID = ""
POWERBI_BASE_URL = "https://api.powerbi.com/v1.0/myorg/"
WORKSPACE_ID = "HCH_Dataset COMPASS"  # Replace with your workspace ID

# Authentication
def get_access_token(app_id, app_secret, tenant_id):
    authority_url = f"https://login.microsoftonline.com/{tenant_id}"
    client = msal.ConfidentialClientApplication(
        app_id, authority=authority_url, client_credential=app_secret
    )
    token_response = client.acquire_token_for_client(scopes=["https://management.core.windows.net/.default"])
    if "access_token" in token_response:
        return token_response["access_token"]
    else:
        raise Exception("Failed to get access token")

# Backup Dataset
def backup_dataset(access_token, workspace_id, database_name, backup_filename):
    url = f"{POWERBI_BASE_URL}{workspace_id}"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    body = {
        "backup": {
            "database": database_name,
            "file": backup_filename,
            "allowOverwrite": True,
            "applyCompression": True
        }
    }
    response = requests.post(url, headers=headers, json=body)
    if response.status_code == 200:
        print(f"Backup successful: {backup_filename}")
    else:
        print(f"Backup failed: {response.status_code} - {response.text}")

# Main Code
access_token = get_access_token(APP_ID, APP_SECRET, TENANT_ID)

# Define Parameters
database_name = "Invoice and Order HCH COMPASS"
workspace_id = "HCH_Dataset COMPASS"  # Replace with actual Workspace ID
datestring = datetime.utcnow().strftime("%b %d, %Y")
backup_filename = f"{datestring}_{database_name}.abf"

# Execute the Backup
backup_dataset(access_token, workspace_id, database_name, backup_filename)


Powershell that does work but I really don't want to use powershell anymore:

 

#PARAMETERS NOT POPULATED
param
(
    [Parameter()]
    [String]$PowerBI = "powerbi://api.powerbi.com/v1.0/myorg/",

    [Parameter()]
    [String]$Workspace = "Videos",

    [Parameter()]
    [String]$DatabaseName = "TheDataModel",

    [Parameter()]
    [String]$AppId = "",

    [Parameter()]
    [String]$AppSecret = "",

    [Parameter()]
    [String]$TenantId = ""
)

$XMLAEndPoint = $PowerBI+$Workspace

$PWord = ConvertTo-SecureString -String $AppSecret -AsPlainText -Force

$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $AppId, $PWord

$datestring = (Get-Date).ToString("s").Replace(":","-")

$Backupfilename = $DatabaseName+"_"+$datestring+".abf"

$Query = 
@"
{
  "backup": {
    "database": "$DatabaseName",
    "file": "$Backupfilename",
    "allowOverwrite": true,
    "applyCompression": true
  }
}
"@


Invoke-ASCmd  -Server $XMLAEndpoint -Query $Query  -ApplicationId $AppId -TenantId $TenantId -Credential $Credential -ServicePrincipal

Hi @nphadro07 The scope used in the get_access_token function should be https://analysis.windows.net/powerbi/api/.default instead of https://management.core.windows.net/.default

Hi @nphadro07 ,

 Thank you for sharing additional details.

  • In your current setup, you're trying to use the Power BI REST API to back up datasets, which isn't the right tool for this job. To back up datasets, you should use the XMLA endpoint in Power BI, which supports the backup operations you need.
  • The PowerShell script uses the XMLA endpoint by calling the correct URL with PowerShell commands, which are built to support this interaction. However, in Python, you must manually manage the endpoint, headers, and body to achieve the same result.
  • The 404 error is probably happening because the endpoint for the backup operation is incorrect
  • Double-check that the WORKSPACE_ID variable is the same as your actual workspace ID in Power BI. Make sure it's in the correct format (GUID).
  • Make sure that the database name variable exactly matches the name of the dataset you want to back up in your workspace.

Please follow the steps outlined below to resolve your issue.

  • Go to the Power BI Service and open your workspace.
  • Copy the Workspace Connection URL from the Workspace Settings under Premium.
  • Make sure your Python script appends /XMLA to the endpoint URL.
  • The request body now needs to be in XML format, as required by the XMLA endpoint. So, replace the JSON payload in your script with an XMLA query
  • Update your Python script to include the XML query in the request.
  • Decode your access token using jwt.io to verify it has the necessary permissions for XMLA operations.

For more detailed information, please refer to the following links:

Semantic model connectivity and management with the XMLA endpoint in Power BI - Power BI | Microsoft....

Use Python experience on Notebook - Microsoft Fabric | Microsoft Learn.

 

Key deference:

Vyubandimsft_0-1736224606257.png

NOTE:  Make sure your workspace is in Premium or Premium Per User (PPU) capacity to support the XMLA endpoint. This is necessary to access the XMLA API.

 

Please let us know if you need further assistance!

If my response solved your query, please mark it as the Accepted solution to help others find it easily!

And if my answer was helpful, I'd really appreciate a 'Kudos'.

Hi @nphadro07 ,

We wanted to touch base as we haven't received a response from you yet. We hope the solution provided was useful. If you require further assistance or have any more questions, don't hesitate to reach out. Your feedback is invaluable, and we look forward to your response.

 

Thank You.

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors