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 / excel-lambda-DICECOEFF.txt
Created April 2, 2024 20:16
This Excel LAMBDA function calculates the Sørensen–Dice coefficient between two text strings
/*
GETBIGRAMS - Get the bigrams of a text string
Inputs: 1. str - a text string
Returns: A vertical array of the bigrams of the text string
Example:
=GETBIGRAMS("banana")
={"ba";"an";"na";"a"}
*/
@ncalm
ncalm / office-scripts-protection.ts
Last active March 22, 2024 13:32
This gist demonstrates how to set protection options in Office Scripts for Excel
function main(workbook: ExcelScript.Workbook) {
const activeSheet = workbook.getActiveWorksheet()
// Returns an object representing the worksheet's protection
const activeSheetProtection = activeSheet.getProtection()
// Protect the sheet according to preferred options (password as 2nd arg is optional)
// This line uses the ternary operator. The protect call is only used if the getProtected() call returns false
@ncalm
ncalm / create-format-chart.ts
Last active March 9, 2024 21:33
This Office Scripts function creates and formats a chart - for exploration and learning
/*
* Create and format a chart from some AdventureWorksDW2019 data
* The table has these columns: Year, Month, SalesAmount
* The worksheet must include a named cell called 'referenceLineValue'. This value is used to draw the line on the chart.
*/
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
@ncalm
ncalm / excel-lambda-GROUPAGGREGATE.txt
Last active February 17, 2024 12:13
This Excel LAMBDA function creates summary tables similar to SQL GROUP BY queries
/*
GROUPAGGREGATE
Creates summary tables of data similar to SQL's GROUP BY queries
Inputs
- dat: a range or array of data with at least two columns,
one of which's control value must be "group" and one not "group"
- control: a single-row array, where COLUMNS(control)=COLUMNS(dat), of values from this list:
group - the values in this column will be output as row headers
@ncalm
ncalm / excel-lambda-rgb2hex.txt
Last active February 7, 2024 15:31
Resources for retrieving, understanding and using the ColorIndex property in VBA
RGB2HEX = LAMBDA(rgb_array,
LET(
_rgb_array,TOROW(0+rgb_array),
IF(COUNT(_rgb_array)<>3,#VALUE!,
CONCAT("#",DEC2HEX(_rgb_array,2))
)
)
);
@ncalm
ncalm / powerquery-TextSplit.m
Created February 7, 2024 01:09
This code demonstrates one way to parse a string of fields into multiple rows and columns
let
Source = "APS Deposit 04/01/2022 $5,174.27 APS ACH Deposit 04/04/2022 $65,186.66",
Split = Text.Split(Source, " "),
Typed = List.Transform(Split,
each try Number.From(_) otherwise
try Date.From(_, "en-gb") otherwise _ ),
Accumulate = List.Accumulate({0..List.Count(Typed)-1}, "",
(a, b) =>
let
c = Typed{b},
@ncalm
ncalm / powerquery-m-fixnestedheaders.txt
Last active January 19, 2024 05:39
This Power Query custom function will right-fill nested headers then combine n levels of headers into a single promoted column header
/*
Author: Owen Price
Date: 2022-09-17
https://www.flexyourdata.com/
Handles multi-level nested column headers, optionally combines them into a single header row and optionally promotes the first row to the column header of the resulting table.
*/
let
func =
@ncalm
ncalm / excel-lambda-AreWeThereYet.txt
Last active January 19, 2024 05:39
This Excel Lambda calculates how long it is until your birthday.
AreWeThereYet = LAMBDA(date,
LET(
n, NOW(),
days, date - n,
countdown, INT(days) & " days, " &
TEXT(days, "h"" hours ""m"" minutes ""s"" seconds"""),
working_days, NETWORKDAYS(n, date),
working_hours, working_days * 8,
hours, days * 24,
minutes, days * 24 * 60,
@ncalm
ncalm / excel-lambda-depn.txt
Last active January 19, 2024 05:33
This Excel Lambda namespace allows for parameterized creation of a depreciation schedule
namespace_author = "Owen Price";
namespace_author_url = "https://www.flexyourdata.com";
namespace_description="This namespace provides functionality for building depreciation schedules";
schedule_demo = depn.schedule(100000,10000,6,2015,depn.db,true,depn.byyear,true);
schedule_header = {"Period labels","Periods","Depreciation","Accumulated Depreciation","Depreciated Asset Value"};
/*
Author: Owen Price
https://www.flexyourdata.com
@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