Skip to content

Instantly share code, notes, and snippets.

@dgosbell
dgosbell / Move-PowerBICapacityWorkspaces.ps1
Last active December 3, 2023 23:45
Moves all Power BI / Fabric workspaces from one premium capacity to another. Useful for when you want to migrate between a P SKU and an F SKU and cannot simply scale up/down the existing SKU
#Requires -Modules MicrosoftPowerBIMgmt
<#
.SYNOPSIS
This script will move all workspace in one capacity to another capacity
.DESCRIPTION
.NOTES
File Name : Move-PowerBICapacityWorkspaces.ps1
Author : Darren Gosbell
@dgosbell
dgosbell / Add-PBIUserToRole.ps1
Last active June 25, 2023 23:59
Uses the XMLA endpoint to add a user to a role for a Power BI dataset
## NOTE: editing your dataset via the XMLA endpoint will prevent you from downloading it
## so make sure you have a backup of the original pbix file
$clientID = '<your-clientId>'
$clientSecret = '<your-client-secret>'
$tenantID = '<your-teantId>'
$workspaceName = 'Xmla%20Test'
$datasetName = 'Role Test'
$roleName = 'DynamicRole'
## This script will apply an incremental refresh policy to generate
## empty partitions for that policy using a Service Principal via the XMLA endpoint
$workspace = "XMLA%20Test"
$databaseName = "Adv Works Inc Refresh Base"
$tableName = "Internet Sales"
$clientID = "<Your SPN Client ID>"
$tenantID = "<Your TenantID>"
$clientSecret = "<Your SPN Secret>"
// =====================================
// Author: Darren Gosbell
// Date: 12 Apr 2023
// This script works in Tabular Editor and sets the MaxParallelismPerQuery
// setting at the database level.
// =====================================
if (Model.Database.CompatibilityLevel < 1569)
{
Model.Database.CompatibilityLevel = 1569;
@dgosbell
dgosbell / AutoPopulateMeasureDescriptionsFromAzureOpenAI.csx
Created March 29, 2023 22:33
Update Tabular Model Descriptions from Azure OpenAI with rate limiting logic
#r "System.Net.Http"
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;
// you need to create a resource and deploy a model in Azure OpenAI
// to get the following 3 pieces of information
// see https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal
const string apiKey = "<your API key";
const string resource_name = "<your resource name>";
@dgosbell
dgosbell / AutoPopulateMeasureDescriptionsFromOpenAIWithDelay.csx
Last active April 6, 2024 03:46
Update Tabular Model Descriptions from ChatGPT with rate limiting logic
#r "System.Net.Http"
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;
// You need to signin to https://platform.openai.com/ and create an API key for your profile then paste that key
// into the apiKey constant below
const string apiKey = "<YOUR API KEY HERE>";
const string uri = "https://api.openai.com/v1/completions";
const string question = "Explain the following calculation in a few sentences in simple business terms without using DAX function names:\n\n";
@dgosbell
dgosbell / AutoPopulateMeasureDescriptionsFromOpenAI.csx
Last active February 17, 2024 03:53
This is a script for tabular editor that will loop through all the measures in a model and get ChatGPT to write a description of the calculation.
#r "System.Net.Http"
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;
// You need to signin to https://platform.openai.com/ and create an API key for your profile then paste that key
// into the apiKey constant below
const string apiKey = "<YOUR API KEY HERE>";
const string uri = "https://api.openai.com/v1/completions";
const string question = "Explain the following calculation in a few sentences in simple business terms without using DAX function names:\n\n";
@dgosbell
dgosbell / Extract-PowerQuery-From-Xlsx.ps1
Created October 20, 2022 01:04
Extract PowerQuery code directly from xlsx file
# Open the xlsx file and find the DataMashup item
$xlPkg = [System.IO.Packaging.Package]::Open($xlFile)
$itemParts = $xlPkg.GetParts() | where {$_.Uri -like "/customXml/item*" -and $_.ContentType -eq "application/xml"}
foreach ($p in $itemParts)
{
$itemStrm = $p.GetStream();
$itemRdr = [System.IO.StreamReader]::new($itemStrm)
$content = [xml]$itemRdr.ReadToEnd()
$base64 = $content["DataMashup"].InnerText
if ($base64 -ne $null) {break;}
using System.Data.SqlClient;
using sysData = System.Data;
// Update the following 2 variables for your environment
var sqlConnStr = "Data Source=localhost\\sql19;Database=AdventureWorksDW2019;Integrated Security=SSPI";
var isPowerBI = true;
// Get all "PBI_Description" column extended Properties
SqlConnection conn = new SqlConnection(sqlConnStr);
conn.Open();
@dgosbell
dgosbell / AmoTraceTestHarness.ps1
Created October 26, 2021 00:16
Powershell test harness for capturing XMLA trace events
$serverName = 'localhost\tab19'
add-type -Path 'C:\Program Files\DAX Studio\bin\Microsoft.AnalysisServices.dll'
$server = New-Object Microsoft.AnalysisServices.Server
$server.Connect($serverName)
$trace = $server.Traces.Add()
$event1 = $trace.Events.Add( [Microsoft.AnalysisServices.TraceEventClass]::DiscoverBegin)