Last active
March 10, 2026 20:54
-
-
Save halbuki/2de75f8557552cb9d072924da46cbb91 to your computer and use it in GitHub Desktop.
Excel Lambda functions for Power Query Table functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* USE NAMESPACE "Table" */ | |
| Item = LAMBDA(_table, _index, | |
| LET( | |
| // Help | |
| Help, TRIM(TEXTSPLIT( | |
| "DESCRIPTION:->Returns the row at position _index from _table including the header row\nVERSION:->1.0\nPARAMETERS:->_table as table, _index as number", | |
| "->", | |
| "\n" | |
| )), | |
| // Error Messages | |
| ErrorMessages, {"_index is not a number."}, | |
| // Check Inputs: 1 if _index is not a number, otherwise 0 | |
| CheckInputs, IF(NOT(ISNUMBER(_index)), 1, 0), | |
| // Procedure | |
| Result, MAKEARRAY( | |
| 2, | |
| COLUMNS(_table), | |
| LAMBDA(ir, ic, CHOOSE(ir, INDEX(_table, 1, ic), INDEX(_table, _index + 1, ic))) | |
| ), | |
| // Return Result or Help/Error | |
| CHOOSE(CheckInputs + 1, | |
| Result, | |
| Help, | |
| INDEX(ErrorMessages, CheckInputs) | |
| ) | |
| ) | |
| ); | |
| SelectRows = LAMBDA(_table, _condition, | |
| LET( | |
| // Help | |
| Help, TRIM(TEXTSPLIT( | |
| "DESCRIPTION:->Filters _table rows where _condition returns TRUE for that row.\nVERSION:->1.0\nPARAMETERS:->_table as table, _condition as function", | |
| "->", | |
| "\n" | |
| )), | |
| // No error messages since types cannot easily be checked | |
| ErrorMessages, {}, | |
| CheckInputs, 0, | |
| // Procedure | |
| selected, MAKEARRAY( | |
| ROWS(_table), | |
| 1, | |
| LAMBDA(ir, ic, _condition(Table.Item(_table, ir))) | |
| ), | |
| Result, FILTER(_table, selected), | |
| CHOOSE(CheckInputs + 1, | |
| Result, | |
| Help, | |
| "" | |
| ) | |
| ) | |
| ); | |
| AddKey = LAMBDA(_table, _columnname, | |
| LET( | |
| // Help | |
| Help, TRIM(TEXTSPLIT( | |
| "DESCRIPTION:->Adds an index column named _columnname to _table.\nVERSION:->1.0\nPARAMETERS:->_table as table, _columnname as text", | |
| "->", | |
| "\n" | |
| )), | |
| ErrorMessages, {"_columnname is not text."}, | |
| CheckInputs, IF(NOT(ISTEXT(_columnname)), 1, 0), | |
| ct, ROWS(_table), | |
| Result, MAKEARRAY( | |
| ct, | |
| COLUMNS(_table) + 1, | |
| LAMBDA(ir, ic, | |
| IF( | |
| ic = 1, | |
| IF(ir = 1, _columnname, ir - 1), | |
| INDEX(_table, ir, ic - 1) | |
| ) | |
| ) | |
| ), | |
| CHOOSE(CheckInputs + 1, | |
| Result, | |
| Help, | |
| INDEX(ErrorMessages, CheckInputs) | |
| ) | |
| ) | |
| ); | |
| Column = LAMBDA(_table, _columnname, | |
| LET( | |
| Help, TRIM(TEXTSPLIT( | |
| "DESCRIPTION:->Returns a column from _table with the name _columnname.\nVERSION:->1.0\nPARAMETERS:->_table as table, _columnname as text", | |
| "->", | |
| "\n" | |
| )), | |
| ErrorMessages, {"_columnname is not text."}, | |
| CheckInputs, IF(NOT(ISTEXT(_columnname)), 1, 0), | |
| columnnames, Table.ColumnNames(_table), | |
| colpos, List.PositionOf(columnnames, _columnname), | |
| Result, INDEX(_table, 0, colpos), | |
| CHOOSE(CheckInputs + 1, | |
| Result, | |
| Help, | |
| INDEX(ErrorMessages, CheckInputs) | |
| ) | |
| ) | |
| ); | |
| ColumnNames = LAMBDA(_table, | |
| LET( | |
| Help, TRIM(TEXTSPLIT( | |
| "DESCRIPTION:->Returns a list of column names from _table.\nVERSION:->1.0\nPARAMETERS:->_table as table", | |
| "->", | |
| "\n" | |
| )), | |
| // no error checks | |
| Result, TRANSPOSE(INDEX(_table, 1, 0)), | |
| // Always return result by default; help when invoked accordingly | |
| CHOOSE(1, Result, Help, "") | |
| ) | |
| ); | |
| SelectColumns = LAMBDA(_table, _columns, | |
| LET( | |
| Help, TRIM(TEXTSPLIT( | |
| "DESCRIPTION:->Returns a new table containing only the columns listed in _columns.\nVERSION:->1.0\nPARAMETERS:->_table as table, _columns as list", | |
| "->", | |
| "\n" | |
| )), | |
| ErrorMessages, {}, | |
| CheckInputs, 0, | |
| columnnames, Table.ColumnNames(_table), | |
| Result, LET( | |
| cnt, COUNTA(_columns), | |
| MAKEARRAY( | |
| ROWS(_table), | |
| cnt, | |
| LAMBDA(ir, ic, | |
| LET( | |
| colname, INDEX(_columns, ic), | |
| colpos, List.PositionOf(columnnames, colname), | |
| IF( | |
| ir = 1, | |
| colname, | |
| INDEX(_table, ir, colpos) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ), | |
| CHOOSE(CheckInputs + 1, | |
| Result, | |
| Help, | |
| "" | |
| ) | |
| ) | |
| ); |
Comments are disabled for this gist.