Skip to content

Instantly share code, notes, and snippets.

View data-goblin's full-sized avatar

Kurt data-goblin

View GitHub Profile
@data-goblin
data-goblin / format-m-expression.csx
Created March 9, 2023 20:01
A C# Script for tabular editor that formats the power query of a shared expression.
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// URL of the powerqueryformatter.com API
string powerqueryformatterAPI = "https://m-formatter.azurewebsites.net/api/v2";
// HttpClient method to initiate the API call POST method for the URL
@data-goblin
data-goblin / 🐍Snek!.csx
Last active March 3, 2023 10:22
a Tabular Editor script to play a Snake game.
#r "System.Drawing"
// PLAY SNEK!
// By Kurt Buhler, Data Goblins; revisions by Daniel Otykier
// To use this script:
// 1. Open it in the Tabular Editor 3 Script Window.
// 2. Run it and have fun.
using System.Drawing;
using System.Drawing.Imaging;
@data-goblin
data-goblin / PreviewColumnsAndMeasures.csx
Last active March 8, 2023 14:13
A Tabular Editor C# script to test/check any valid combination of selected columns and/or measures in the model.
// Instructions
// ------------
// 1. Save this script as a macro with a context of 'Column' and 'Measure'
// 2. Configure a keyboard shortcut for the macro (i.e. ALT + C) if using Tabular Editor 3
// 3. Select any combination of columns & measures related in the model & run the script
// 4. The output will show you the evaluation result for all selected objects, presuming evaluation is valid
// Get column names
var _ColumnsList = new List<string>();
@data-goblin
data-goblin / CountModelObjects.csx
Created January 4, 2023 15:43
A Tabular Editor script to count model objects and output their results to a pop-up info box, to give a quick overview of the data model opened.
// Count calculation groups & calculation items
int _calcgroups = 0;
int _calcitems = 0;
foreach ( var _calcgroup in Model.CalculationGroups )
{
_calcgroups = _calcgroups + 1;
  foreach ( var _item in _calcgroup.CalculationItems )
  {
  _calcitems = _calcitems + 1;
   }
@data-goblin
data-goblin / FormatPowerQuery.csx
Last active January 25, 2024 16:13
A Tabular Editor C# script to format Power Query code for Partitions (M - Import) or Shared Expressions.
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// URL of the powerqueryformatter.com API
string powerqueryformatterAPI = "https://m-formatter.azurewebsites.net/api/v2";
// HttpClient method to initiate the API call POST method for the URL
@data-goblin
data-goblin / selected-measure-dependancies_devops_mermaid.csx
Created June 29, 2022 15:07
A Tabular Editor C# script to generate a context-dependent mermaid diagram for Azure DevOps Wikis (flowchart) of measure dependencies based on the selected measure.
// This code is still WIP, it doesn't entirely filter the lineage. Feel free to make adjustments.
string dependancies = "::: mermaid\ngraph LR;\n%% Measure dependancy mermaid flowchart";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
foreach(var _measures in Model.AllMeasures )
{
// Deep lineage for upstream measures
@data-goblin
data-goblin / selected-measure-dependencies_mermaid.csx
Created June 29, 2022 15:04
A Tabular Editor C# script to generate a context-dependent mermaid diagram (flowchart) of measure dependencies based on the selected measure.
// This code is still WIP, it doesn't entirely filter the lineage. Feel free to make adjustments.
string dependancies = "flowchart LR\n%% Measure dependancy mermaid flowchart";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
foreach(var _measures in Model.AllMeasures )
{
// Deep lineage for upstream measures
@data-goblin
data-goblin / all-measure-dependencies_mermaid.csx
Created June 29, 2022 14:57
A Tabular Editor C# script to get all measures in a model as a mermaid diagram syntax.
string dependancies = "flowchart LR\n%% Measure dependancy mermaid flowchart";
foreach(var _measures in Model.AllMeasures )
{
var _upstream = _measures.DependsOn;
var _upstream_measures = _upstream.Measures.OfType<Measure>().Select(c => c).Distinct();
dependancies += string.Format("\r\n\n%% [{1}] Dependancies:\n\t{0}[\"{1}\"]",
@data-goblin
data-goblin / power-bi_export-report.py
Created May 5, 2022 22:19
Quick python script to export a report using interactive browser authentication of the user
from azure.identity import InteractiveBrowserCredential
import requests, json
# Authenticate through the browser - not automated but alternatives exist
# Docs: https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity?view=azure-python
creds = InteractiveBrowserCredential()
scope = 'https://analysis.windows.net/powerbi/api/.default'
user_token = creds.get_token(scope)
user_token = user_token.token
user_header = {'Authorization': f'Bearer {user_token}'}
@data-goblin
data-goblin / powerbi-admin-get-workspaces.py
Created May 5, 2022 20:22
Python script to get all workspaces managed in a tenant and containing objects (users, artifacts) with the Power BI Admin REST API
#########################################################################################
# Authentication - Replace string variables with your relevant values
#########################################################################################
import json, requests, pandas as pd
try:
from azure.identity import ClientSecretCredential
except Exception:
!pip install azure.identity
from azure.identity import ClientSecretCredential