Power Query Search by Column Header EndsWith

brawnystaff

Board Regular
Joined
Aug 9, 2012
Messages
109
Office Version
  1. 365
I currently am using a Custom Column to return TRUE/FALSE if various rows contain any matching keywords in I have in a separate list (called Keywords) in specific columns. The issue I am having is there is like 60 columns I need to check for the keyword list. However, all columns end with "EIN". Is their way to use Text.EndsWith to search all Table columns that end in EIN? Listed below is M code I have that works, but only for three of the columns. Thanks.

Power Query:
List.ContainsAny({[#"Subject 1: EIN"],[#"Subject 2: EIN"],[#"Subject 3: EIN"]},Keywords)
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tcnEIN = List.RemoveNulls(List.Transform(Table.ColumnNames(Source), each if Text.EndsWith(_,"EIN") then _ else null)),
    KeyWords = {1..20},
    tbl = Table.AddColumn(Source, "TF", each List.ContainsAny(List.Transform(tcnEIN, (x)=> Record.Field(_,x)), KeyWords))
in
    tbl

Book1
ABCDEFGHIJKLMNOPQRSTUVWXY
1ABC EINDE EINFG EINHIJKABC EINDE EINFG EINHIJKTF
216312313367691764171182158111631231336769176417118215811TRUE
39218917382208188132037811116292189173822081881320378111162TRUE
412124986212822954571671651181212498621282295457167165118FALSE
519618045136191481032119020531961804513619148103211902053FALSE
610819201475392132271811691741081920147539213227181169174TRUE
7714928291122262241215510635714928291122262241215510635FALSE
83196147514642125604614420731961475146421256046144207TRUE
997661585105137216225802042069766158510513721622580204206TRUE
10154727365505637833132223154727365505637833132223FALSE
115219364194611351616818511612652193641946113516168185116126FALSE
1215647214931798819795215241451564721493179881979521524145FALSE
13209149231120211520186211148159209149231120211520186211148159FALSE
14198166152100891391027010916044198166152100891391027010916044FALSE
1518621819951143771923622150591862181995114377192362215059FALSE
161221341017113172104129171271812213410171131721041291712718FALSE
172314120011911781157168210130523141200119117811571682101305FALSE
181551531516131322281951782219915515315161313222819517822199FALSE
19762841209122016943014225762841209122016943014225TRUE
20164222386339114110908721910164222386339114110908721910FALSE
212713821241581701773426437427138212415817017734264374FALSE
22140791841832171758340230187107140791841832171758340230187107FALSE
23
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
X2:X22Cell Value=TRUEtextNO
O2:O22,Q2:Q22,S2:S22Expression=O2<21textNO
 
Upvote 0
tbl = Table.AddColumn(Source, "TF", each List.ContainsAny(List.Transform(tcnEIN, (x)=> Record.Field(_,x)), KeyWords))

Can you kindly elaborate this, I kind of understand what each function does, but can't seem to get my head around List.Transform(tcnEIN, (x)=> Record.Field(_,x)
 
Upvote 0
the each keyword is equivalent to this (_)=>, that is creating a function of _.

But if you don't use each, then you are free to use any variable name you want, like (x)=> or (myVariable)=> or any legitimate variable name.

The important thing to know is what the variable relates to,

In Table.AddColumns the function variable is always a record of the table as the Table.AddColumns function steps through the rows of the table.

In List.Transform the function variable is always an element of the list as the List.Transform function steps through the elements of the list.

with regard to tbl = Table.AddColumn(Source, "TF", each List.ContainsAny(List.Transform(tcnEIN, (x)=> Record.Field(_,x)), KeyWords))

tcnEIN is a list of column names that end with "EIN"

The _ goes with the each part of the Table.AddColumns function. The _ represents a record (row) of the table Source as the function steps through the rows.

the (x)=> part is needed because I can't use each again in the List.Transform call because the _ variable is already used by Table.AddColumn. x being used in List.Transform represents an item in the list, in this case an item in tcnEIN as List.Transform steps through the elements of tcnEIN. So the List.Transform is changing from a list of column names ending in EIN to a list of items in the current row of the table in those tcnEIN columns. Record.Field(_,x) returns a field in column whose name is contained in the variable x from a record contained in the variable _.
 
Upvote 0
the each keyword is equivalent to this (_)=>, that is creating a function of _.

But if you don't use each, then you are free to use any variable name you want, like (x)=> or (myVariable)=> or any legitimate variable name.

The important thing to know is what the variable relates to,

In Table.AddColumns the function variable is always a record of the table as the Table.AddColumns function steps through the rows of the table.

In List.Transform the function variable is always an element of the list as the List.Transform function steps through the elements of the list.

with regard to tbl = Table.AddColumn(Source, "TF", each List.ContainsAny(List.Transform(tcnEIN, (x)=> Record.Field(_,x)), KeyWords))

tcnEIN is a list of column names that end with "EIN"

The _ goes with the each part of the Table.AddColumns function. The _ represents a record (row) of the table Source as the function steps through the rows.

the (x)=> part is needed because I can't use each again in the List.Transform call because the _ variable is already used by Table.AddColumn. x being used in List.Transform represents an item in the list, in this case an item in tcnEIN as List.Transform steps through the elements of tcnEIN. So the List.Transform is changing from a list of column names ending in EIN to a list of items in the current row of the table in those tcnEIN columns. Record.Field(_,x) returns a field in column whose name is contained in the variable x from a record contained in the variable _.
Thanks for this.
 
Upvote 0

Forum statistics

Threads
1,223,677
Messages
6,173,798
Members
452,535
Latest member
berdex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top