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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mm456
Frequent Visitor

Creating a dynamic calculated column based on 'Parametised' data source

Hi all,

 

I'm trying to automate a PowerBi report that I have published that uses parameters to change the values for data coming from AzureBlobs.  I have one report and I'm using powershell to deploy changes to various workspaces such as changing the title of the report. However, I have a calculated called Area (below) that checks for specific strings in another column and renames them.

 

Area = SWITCH (

    TRUE(),
    CONTAINSSTRING(Data[Service],"Dev"),"Test",
    CONTAINSSTRING(Data[Service],"demo"),"Demo",
     "Other"
    )

Since, I'm using multiple datasources and expect this column to change from time to time with different values thus the calculation must be done on the fly. How can I calcucate this field either using PowerShell or within PowerBI or any other suggestions?
 


Thanks,

Marl

3 REPLIES 3
Adamboer
Responsive Resident
Responsive Resident

You can use the Power BI REST API to update the calculated column in your report. You can use PowerShell to call the API and pass the appropriate parameters to update the column.

First, you'll need to get the report ID and dataset ID of the report you want to update. You can do this using the Power BI API or by navigating to the report in Power BI and looking at the URL.

Then, you can use the Update Dataset API endpoint to update the calculated column. In the request body, you can include the updated DAX expression for the calculated column.

Here's an example PowerShell script to update the calculated column:

$ReportId = "<your report ID>"
$DatasetId = "<your dataset ID>"
$Token = "<your access token>"

$Headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $Token"
}

$Body = @{
updateDetails = @{
resourcePackage = @{
properties = @{
content = @{
DataTransformations = @{
Transformations = @{
CalculationGroups = @{
[guid]::NewGuid().ToString() = @{
Tables = @{
Data = @{
CalculatedColumns = @{
Area = "SWITCH ( TRUE(), CONTAINSSTRING(Data[Service],`"Dev`"),`"Test`", CONTAINSSTRING(Data[Service],`"demo`"),`"Demo`",`"Other`" )"
}
}
}
}
}
}
}
}
}
}
}
} | ConvertTo-Json

Invoke-RestMethod -Method Post -Uri "https://api.powerbi.com/v1.0/myorg/groups/$($WorkspaceId)/datasets/$($DatasetId)/Default.UpdateDatas..." -Headers $Headers -Body $Body

This script will update the Area calculated column in the Data table of your dataset. You can modify the DAX expression to fit your specific needs.


Hi @Adamboer,

 

I'm getting the error below when running the above query;mm456_1-1679355792079.png

Before the error, using the Microsoft API test https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/update-datasources#code-try-0 threw a "connection details" error which I added into the above code using the format


"connectionDetails": {
"account": "AzureBlobAccount",
"domain": "blob.core.windows.net"},

On further reading the Update Datasources documentation it seems that Azure Blob Storage are not supported under "Limitations". Would this be the case or I'm missing something?

mm456_2-1679356082731.png

 

v-yueyunzh-msft
Community Support
Community Support

Hi , @mm456 

Depending on your requirements, it seems that you have some dynamic calculated columns, you need to deploy changes to various workspaces such as changing the title of the report, if convenient, you can try the "deployment pipelines" feature that comes with Power BI:

Get started using deployment pipelines, the Power BI Application lifecycle management (ALM) tool - P...

 

According to my research, it can also support editing the deployment rule when deploying, and modifying the corresponding data source of the data set after the deploy:
Create deployment rules Power BI Application lifecycle management (ALM) - Power BI | Microsoft Learn

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors