Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
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.
Solved! Go to Solution.
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
I don't believe so in the commandlet
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.
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.
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?
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
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?
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!