Skip to content

Instantly share code, notes, and snippets.

View ncalm's full-sized avatar
💭
hitting computer with hammer

Owen Price ncalm

💭
hitting computer with hammer
View GitHub Profile
@ncalm
ncalm / powerquery-m-clean-currency-suffix
Last active April 28, 2022 00:19
This M snippet is a pattern for converting currency stored as text to a proper numeric value e.g. $18B becomes 18000000000
#"Converted currency text" = Table.AddColumn(#"Previous query step","new_column_name",each
let
//convert the original text to lower case
lower = Text.Lower([currency_as_text]),,
//add as many Text.Replace as you need to remove unwanted words
//in case of many words to remove, could iterate a list of words
words_removed = Text.Replace(lower,"unknown",""),
//for text $180B, following split creates a list {"$180","b"}
@ncalm
ncalm / excel-lambda-CLEANCURRENCYTEXT.txt
Last active June 15, 2023 14:15
This collection of Excel lambda functions will quickly convert currency values stored as text with a scaling suffix to a numerical representation of the same number in a standard scale
/*
CLEANCURRENCYTEXT
This lambda function will quickly convert currency values stored as text
with a scaling suffix such as "B" (for billions) or "M" (for millions)
and so on, to a number all in the same scale
inputs:
- val, a single value to be converted as described above
- [mapping], a two-column array or range of suffix:power pairs such as
@ncalm
ncalm / excel-lambda.LIST.COMPARE.txt
Created May 4, 2022 21:04
This Excel lambda function compares two lists and returns a three column array showing the differences
/*
LIST.COMPARE
Compares the items in two lists and shows where an item exists in one list and not the other
Inputs:
Required:
- list1 - a one-dimensional range or array, either vertical or horizontal
- list2 - a one-dimensional range or array, either vertical or horizontal
@ncalm
ncalm / powerquery-m-change-column-type-dynamic-column-list.txt
Last active May 7, 2022 13:34
This M pattern shows how we can build a dynamic list of column names and column types then change the column types using the list. In this way, we can avoid hard-coding into the query the column names whose types we want to change
/*
Intent here is to transform many columns at once without hard-coding the column names into the query (in case the column names change in an update to the source)
Longer with comments to explain (shorter embedded version below)"
*/
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
@ncalm
ncalm / excel-lambda-pd.qcut.txt
Created May 7, 2022 14:07
This Excel Lambda function mimics behavior of the Python Pandas method pd.qcut
/*
pd.qcut
First in a series of Excel Lambda implementations of Python Pandas methods.
pd.qcut will create groups, or bins, for a continuous numerical variable.
Inputs
@ncalm
ncalm / excel-lambda-pd.rolling.aggregates.txt
Last active June 15, 2023 09:42
This Excel lambda function intends to mimic the key functionality of the pandas window functions via the rolling object
/*
pd.rolling.aggregate
Calculates a rolling aggregate of a variable where each window is of size window and the function agg is applied to each window
Inputs:
x: a single-column numerical variable, sorted in the order the user expects to calculate rolling calculations on
window: an integer specifying the window length/width. For example, if window is 3, then the aggregate will be applied over the set of 3 rows ending in the current row
agg: a text string specifying which aggregate function should be applied over each window
@ncalm
ncalm / excel-lambda-INFLECTIONPOINTS.txt
Last active July 24, 2023 12:44
This Excel lambda function identifies the points in a series that are either higher or lower than both adjacent points
/*
INFLECTIONPOINTS
Identifies the points y_x in an ordered numerical variable where the adjacent points y_x-1 and y_x+1 are either both higher or both lower than y_x
Inputs:
y: an ordered numerical array
Returns:
An array the same size as y containing -1 where y_x is lower than the adjacent points, 1 where y_x is higher than the adjacent points, otherwise 0.
@ncalm
ncalm / excel-lambda.SQL.PREP.txt
Last active January 18, 2024 07:01
This gist contains several Excel lambda functions for preparing Excel data for use in SQL statements such as INSERT/VALUES and WHERE/IN
/*
SQL.TYPE
Returns an array of values from the set {"text","date","number"} indicating how a cell's value should be formatted for use in a SQL statement
Inputs:
- in_list: a 1-dimensional array or range, which can be horizontal or vertical
Return:
an array the same size as in_list, containing:
@ncalm
ncalm / excel-lambda-RANK.DENSE.txt
Last active June 15, 2023 09:42
This Excel lambda function provides a ranking mechanism similar to SQL's DENSE_RANK, but follows the pattern of RANK.AVG and RANK.EQ
/*
RANK.DENSE
Ranks a one-dimensional array using the provided sort order. If adjacent items have the same value, they are all given the rank of the first of them.
Ranks following equally ranked groups increment by one from the most recent rank. This is as opposed to RANK.EQ where following ranks are given the rank
they would have received if the same-ranked items were given different ranks. As such, this function behaves similarly to SQL's DENSE_RANK
Inputs:
Number - A number or array of numbers to find the rank for from the ranks given by Ref sorted by Order
Ref - A list of numbers to be ranked, from which the rank of Number will be found
@ncalm
ncalm / excel-lambda-RANK.AVERAGE.txt
Created May 29, 2022 21:00
This Excel lambda function implements the native RANK.AVG in a way that can be used inside MAKEARRAY and SCAN
/*
RANK.AVERAGE
Implementation of RANK.AVG that can be used in MAKEARRAY, SCAN etc.
Inputs and output is the same as RANK.AVG with the exception that if Number is not found in Ref, the cell will output "No rank" instead of an error value.
*/
RANK.AVERAGE =LAMBDA(Number,Ref,[Order],