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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
poweruser1
Frequent Visitor

Create partition via powershell error

I am trying to write a Powershell script to create a partition in a Power BI dataset. Anyone know how to resolve this error? I am receiving the following error:

poweruser1_0-1687960687058.png

My Powershell Code:

$m_code = "let
Source = Sql.Database(`"source`", `"db`", [Query=`"sel column form table`", CommandTimeout=#duration(0, 1, 0, 0)])
in
Source"

$tmsl = "{
`"create`": {
`"parentObject`": {
`"database`": `"model name`",
`"table`": `"Table name`"
},
`"partition`": {
`"name`": `"test partition`",
`"source`": {
`"type`": `"m`",
`"expression`": [
`"$m_code`"
]
}
}
}
}"

invoke-ascmd -Server "powerbi://api.powerbi.com/v1.0/myorg/workspace" -Database "model Name" -Query $tmsl

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @poweruser1,

Perhaps you can take a look the following sample code if it help with your scenario:

$groupID = "<your group ID>"
$datasetID = "<your dataset ID>"
$token = Get-PowerBIAccessToken
$authHeader = @{ "Authorization" = "Bearer $token" }
$uri = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/datasets/$datasetID/Partitions"
$body = @{
    "name" = "ImportName"
    "mode" = "Import"
    "source" = @{
        "type" = "Query"
        "query" = "SELECT * FROM $tableName"
        "dataSource" = @{
            "type" = "Sql"
            "connectionDetails" = @{
				"server" = "$serverName.database.windows.net"
				"database" = "$databaseName"
                "authentication" = "Windows"
            }
        }
    }
}
$jsonBody = $body | ConvertTo-Json
Invoke-RestMethod -Uri $uri -Headers $authHeader -Method Post -Body $jsonBody

Regards,

Xiaoxin Sheng

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @poweruser1,

Perhaps you can take a look the following sample code if it help with your scenario:

$groupID = "<your group ID>"
$datasetID = "<your dataset ID>"
$token = Get-PowerBIAccessToken
$authHeader = @{ "Authorization" = "Bearer $token" }
$uri = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/datasets/$datasetID/Partitions"
$body = @{
    "name" = "ImportName"
    "mode" = "Import"
    "source" = @{
        "type" = "Query"
        "query" = "SELECT * FROM $tableName"
        "dataSource" = @{
            "type" = "Sql"
            "connectionDetails" = @{
				"server" = "$serverName.database.windows.net"
				"database" = "$databaseName"
                "authentication" = "Windows"
            }
        }
    }
}
$jsonBody = $body | ConvertTo-Json
Invoke-RestMethod -Uri $uri -Headers $authHeader -Method Post -Body $jsonBody

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.