Skip to content

Instantly share code, notes, and snippets.

@halbuki
Last active September 6, 2022 12:02
Show Gist options
  • Save halbuki/2de75f8557552cb9d072924da46cbb91 to your computer and use it in GitHub Desktop.
Save halbuki/2de75f8557552cb9d072924da46cbb91 to your computer and use it in GitHub Desktop.
Excel Lambda functions for Power Query Table functions
/* USE NAMESPACE "Table" */
Item = LAMBDA(_table, _index,
MAKEARRAY(
2,
COLUMNS(_table),
LAMBDA(ir, ic, CHOOSE(ir, INDEX(_table, 1, ic), INDEX(_table, _index + 1, ic)))
)
);
SelectRows = LAMBDA(_table, _condition,
LET(
selected, MAKEARRAY(
ROWS(_table),
1,
LAMBDA(ir, ic, _condition(Table.Item(_table, ir)))
),
FILTER(_table, selected)
)
);
AddKey = LAMBDA(_table, _columnname,
LET(
ct, ROWS(_table),
MAKEARRAY(
ct,
COLUMNS(_table) + 1,
LAMBDA(ir, ic,
IF(
ic = 1,
IF(ir = 1, _columnname, ir - 1),
INDEX(_table, ir, ic - 1)
)
)
)
)
);
Column = LAMBDA(_table, _columnname,
LET(
columnnames, Table.ColumnNames(_table),
colpos, List.PositionOf(columnnames, _columnname),
INDEX(_table, 0, colpos)
)
);
ColumnNames = LAMBDA(_table, TRANSPOSE(INDEX(_table, 1, 0)));
SelectColumns = LAMBDA(_table, _columns,
LET(
columnnames, Table.ColumnNames(_table),
MAKEARRAY(
ROWS(_table),
COUNTA(ISERROR(_columns)),
LAMBDA(ir, ic,
LET(
colname, INDEX(_columns, ic),
colpos, List.PositionOf(columnnames, colname),
IF(
ir = 1,
colname,
INDEX(_table, ir, colpos)
)
)
)
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment