Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created May 31, 2023 18:51
Show Gist options
  • Select an option

  • Save ncalm/39a6ad1c4babace54485bcce20677711 to your computer and use it in GitHub Desktop.

Select an option

Save ncalm/39a6ad1c4babace54485bcce20677711 to your computer and use it in GitHub Desktop.
Power Query function for selecting a subset of column names from a table using an arbitrary Text Membership function
let fn =
(
Table as table,
MembershipFunction as function,
Substring as text,
optional IgnoreCase as nullable logical
) as list =>
let
// Handle omitted IgnoreCase with default false
_IgnoreCase = if IgnoreCase = null then false else IgnoreCase,
// Convert a text value to lower case if _IgnoreCase is true
_ReCase = (text as text) as text => if _IgnoreCase then Text.Lower(text) else text,
ColumnNames = Table.ColumnNames(Table),
Filtered = List.Select(ColumnNames, each MembershipFunction( _ReCase(_) , _ReCase(Substring) ))
in
Filtered,
fn_type =
type function (
Table as (type table meta [
Documentation.FieldCaption = "Table",
Documentation.FieldDescription = "Table from which to select a subset of column names"
]),
MembershipFunction as (type function meta [
Documentation.FieldCaption = "MembershipFunction",
Documentation.FieldDescription = "A Text Membership Function to use to select a subset of the Table's column names"
]),
Substring as (type text meta [
Documentation.FieldCaption = "Substring",
Documentation.FieldDescription = "A string to search for within the Table's column names"
]),
optional IgnoreCase as (type nullable logical meta [
Documentation.FieldCaption = "IgnoreCase",
Documentation.FieldDescription = "A logical value indicating whether or not to ignore case (default is false)"
])
)
as list meta [
Documentation.Name = "fnSelectColumnNames",
Documentation.LongDescription = "Returns a subset of column names from a Table using an arbitrary Text Membership function"
]
in
Value.ReplaceType(fn,fn_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment