Skip to content

Instantly share code, notes, and snippets.

@ncalm
ncalm / powerquery-simplejoin.m
Created January 30, 2023 18:06
Custom M function to simplify table joins in Power Query
(LeftTable as table, JoinColumns as list, RightTable as table, optional JoinColumnsRight as list,
optional DuplicateColumnPrefix as text, optional JoinKind as number) =>
let
//a function to handle null (optional) parameters
fnIfNull = (arg1, arg2) => if arg1 = null then arg2 else arg1,
//if join kind is null, default to inner join
_JoinKind = fnIfNull(JoinKind, JoinKind.Inner),
@ncalm
ncalm / excel-lambda-ifomitted.txt
Created January 18, 2023 14:49
This gist supports breadth-first search of graph data using Excel Lambda functions
/*
Note, this function is used by the functions in the graph namespace
*/
IFOMITTED = LAMBDA(arg, then, [else],
LET(_else, IF(ISOMITTED(else), arg, else), IF(ISOMITTED(arg), then, _else))
);
@ncalm
ncalm / powerquery-enumeration-list.m
Created December 26, 2022 19:54
M queries to inspect enumeration documentation
/*
A simple query to get the list of enumerations
*/
let
Source = Web.Page(Web.Contents("https://learn.microsoft.com/en-us/powerquery-m/enumerations")){0}[Data]
in
Source
@ncalm
ncalm / powerquery-GetTypeTransforms.m
Last active December 23, 2022 16:50
Power Query function for using a control table to build a list of type transformations to pass to Table.TransformColumnTypes
(typeTable as table) as list
=>
let
//check that the columns in the supplied table have the expected names
checkColumnNames = Table.ColumnNames(typeTable) = {"ColumnName","Type"},
/*
primitive types use this format in TransformColumnTypes
type typeName
Listed here are most common formats - full list on this page:
@ncalm
ncalm / powerquery-fnGetFunctionsByParameter.m
Created December 18, 2022 22:38
M function to return a list of functions that have a specific parameter
(parameterName as text) as table =>
let
Source = Record.ToTable(#shared),
AddColumn = Table.AddColumn(
Source,
"Params",
each try Text.Combine(Record.FieldNames(Type.FunctionParameters(Value.Type([Value]))),", ") otherwise null
),
Result = Table.SelectRows(AddColumn, each Text.Contains([Params],parameterName))
in
@ncalm
ncalm / excel-lambda-RANDOMDB.txt
Last active June 15, 2023 09:39
This Excel Lambda gist provides a lambda for building random combinations of dimension members
/*
Creates an empty array of r rows and c columns
Author: Owen Price
Date: 2022-09-14
*/
EMPTYARRAY = LAMBDA(r, [c], EXPAND({""}, r, IF(ISOMITTED(c), 1, c), ""));
/*
CROSSJOIN creates the cross-product of two arrays
@ncalm
ncalm / excel-lambda-L.txt
Created November 28, 2022 22:41
This Excel Lambda function provides a simplified interface for creating lists of integers
/*
from is the first integer in the list
to is the target integer in the list
step is the difference between successive integers
*/
L =LAMBDA(from,to,[step],
LET(
_step, IF(ISOMITTED(step), IF(from > to, -1, 1), step),
//arguments should be single integers
@ncalm
ncalm / excel-lambda-stacker.txt
Last active October 18, 2025 10:16
stacker namespace for Lambda
/*
array is a column of stuff to which we want to apply element function
row_function is some function that produces an array with a fixed number of columns
the column count produced by row_function must be identical regardless of input
stack_function is one of V or H
If you're unsure how these work or why we would use them, please review these videos:
https://youtu.be/04jOeiMypXw
https://youtu.be/wEBLT9QfQRw
@ncalm
ncalm / powerquery-m-listcrossjoin.m
Created November 15, 2022 19:09
M function to cross join two lists
(list1 as list, list2 as list) as list => List.TransformMany(list1, each list2, (x,y) => {x,y})
@ncalm
ncalm / Part 1 of Working with JSON in SQL Server.sql
Last active November 8, 2022 15:21
Accompanying scripts for "SQL Server - Working with JSON" video series
/*
Simple example of FOR JSON PATH
With FOR JSON PATH, we can convert table data to JSON structured text.
By using the . notation, we can create hierarchies of related attributes.
*/
SELECT TOP 5 *
FROM DimCustomer;