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
Balajis
Microsoft Employee
Microsoft Employee

Power Bi workspace metrics - REST API

Hi, I need to monitor the usage metrics for a certain workspace. Can you please guide me to any step by step tutorial or YT video on how to fetch the user activity data through REST API.

I have tried setting up the log analytics workspace and connecting the powerBI workspace with the log analytics instance, but it is only logging data on Datasets usage not the user activities on PBI reports.

I have referred to few tutorials but it was using C# to build an app. I am looking for tutorials which uses azure cloud services and REST API to fetch user activity data.

1 ACCEPTED SOLUTION
bcdobbs
Community Champion
Community Champion

No blog on it I'm afraid however the code I used is:

#Variables
$BasePath = "C:\FolderName" # Local path in form "C:\FolderName\".
$FileNameStem = "PBIActivity" #Name to prefix on file.
$HistoricDaysToFetch = 30 #Maximum that will return data is 30.

#Import modules
Import-Module MicrosoftPowerBIMgmt

#Connect to PowerBI API.
Write-Host -ForegroundColor White "Connect to PowerBI service";
Connect-PowerBIServiceAccount

for ($DaysInPast = -$HistoricDaysToFetch; $DaysInPast -le -1; $DaysInPast++)
{
    #Set start and end points.
    $Start = [DateTime]::Today.AddDays($DaysInPast).ToString("yyyy-MM-ddTHH:mm:ss.000")
    $End = [DateTime]::Today.AddDays($DaysInPast + 1).AddSeconds(-1).ToString("yyyy-MM-ddTHH:mm:ss.999")

    Write-Host -ForegroundColor White "Get data between:";
    Write-Host -Foregroundcolor White $Start
    Write-Host -Foregroundcolor White $End

    #Get data (max one day).
    Write-Host -ForegroundColor White "Fetching Data";
    $activities = Get-PowerBIActivityEvent -StartDateTime $Start -EndDateTime $End;

    #Set output location variables.
    $FileDate = [DateTime]::Today.AddDays($DaysInPast).ToString("yyyyMMdd")
    $LocalPath = $BasePath + $FileNameStem + $FileDate + ".json"

    #Output On Prem.
    Write-Host -ForegroundColor White "Locally saving to $LocalPath `r`n`n";
    $activities | Out-File -FilePath $LocalPath;
}

 

Code will pull back the last 30 days of logs. You then end up with lots of json files in a folder which you can read directly with Power BI from folder.

 

If you want to run it on a schedule you can pass it service principal credentials with a slight modification (Connect-PowerBIServiceAccount (MicrosoftPowerBIMgmt.Profile) | Microsoft Learn)

 

Let me know if you need any more help with that.

 

Ben

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

View solution in original post

5 REPLIES 5
bcdobbs
Community Champion
Community Champion

I don't believe so in the commandlet

https://community.powerbi.com/t5/Service/retrieve-specific-workspaces-data-using-Get-PowerBIActivity...

 

I think you can pipe ConvertFrom-JSON and then filter within your Powershell script. Following might give some ideas: https://learn.microsoft.com/en-us/power-bi/guidance/admin-activity-log

 

Have to say I've not done anything like that in Powershell so would have to experiment/Google.

 

Personally would just read the json files in with PowerQuery and filter out anything I don't want there.

 

 

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
bcdobbs
Community Champion
Community Champion

Have a look at this:

https://dobbsondata.co.uk/2022/08/21/low-code-power-bi-rest-api-logs-synapse/

I've recently experimented with using the managed identity in Synapse to authenticate directly rather than using the service principal which can make it simpler but idea is the same.



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
Balajis
Microsoft Employee
Microsoft Employee

Great tutorial, in your article you have mentioned about using the Rest API via Powershell to download a log file each day. I want to try that first before using synapse, do you have any article about it?

bcdobbs
Community Champion
Community Champion

No blog on it I'm afraid however the code I used is:

#Variables
$BasePath = "C:\FolderName" # Local path in form "C:\FolderName\".
$FileNameStem = "PBIActivity" #Name to prefix on file.
$HistoricDaysToFetch = 30 #Maximum that will return data is 30.

#Import modules
Import-Module MicrosoftPowerBIMgmt

#Connect to PowerBI API.
Write-Host -ForegroundColor White "Connect to PowerBI service";
Connect-PowerBIServiceAccount

for ($DaysInPast = -$HistoricDaysToFetch; $DaysInPast -le -1; $DaysInPast++)
{
    #Set start and end points.
    $Start = [DateTime]::Today.AddDays($DaysInPast).ToString("yyyy-MM-ddTHH:mm:ss.000")
    $End = [DateTime]::Today.AddDays($DaysInPast + 1).AddSeconds(-1).ToString("yyyy-MM-ddTHH:mm:ss.999")

    Write-Host -ForegroundColor White "Get data between:";
    Write-Host -Foregroundcolor White $Start
    Write-Host -Foregroundcolor White $End

    #Get data (max one day).
    Write-Host -ForegroundColor White "Fetching Data";
    $activities = Get-PowerBIActivityEvent -StartDateTime $Start -EndDateTime $End;

    #Set output location variables.
    $FileDate = [DateTime]::Today.AddDays($DaysInPast).ToString("yyyyMMdd")
    $LocalPath = $BasePath + $FileNameStem + $FileDate + ".json"

    #Output On Prem.
    Write-Host -ForegroundColor White "Locally saving to $LocalPath `r`n`n";
    $activities | Out-File -FilePath $LocalPath;
}

 

Code will pull back the last 30 days of logs. You then end up with lots of json files in a folder which you can read directly with Power BI from folder.

 

If you want to run it on a schedule you can pass it service principal credentials with a slight modification (Connect-PowerBIServiceAccount (MicrosoftPowerBIMgmt.Profile) | Microsoft Learn)

 

Let me know if you need any more help with that.

 

Ben

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
Balajis
Microsoft Employee
Microsoft Employee

Thanks this helps, one more query I assume this code snippet will get the activity data for all the workspaces in the tenant. If there a way to restrict it to extract data for a specific workspace?

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.