+ Reply to Thread
Results 1 to 12 of 12

PQ FindText exact matching columnname

  1. #1
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    PQ FindText exact matching columnname

    Hi all

    I get data from several environments, and they might not all have the same columns. In order to be able to "align" those, so I can use the same Power Query on them, I add some columns, if they do not already exist.

    I do this with the following code:
    Please Login or Register  to view this content.
    This works well, and it will create a column called "Colors", if it does not exist and if it already exists, it does nothing.

    The problem is that FindText does not search for exact text. So if I have 2 columns, respectively called "Fruits" and "BlueFruits", and does the same thing with "Fruits", it will count both columns, not just "Fruits".

    How can I use the same principle, to create new columns, if they not not exist, but only based on exact matching search?

    I attached a demo sheet that shows the problem.

    Best regards
    Imbizile
    Attached Files Attached Files
    Last edited by Imbizile; 03-30-2021 at 04:36 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,234

    Re: PQ FindText exact matching columnname

    The data from the external link is missing.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: PQ FindText exact matching columnname

    There is no data other than the column names. This issue is only related to column names, so I did not add any other content.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,234

    Re: PQ FindText exact matching columnname

    OK - then I can't help as I don't understand what you want. Sorry.

  5. #5
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: PQ FindText exact matching columnname

    Table looks like this:
    AddNonExistingColumns.png

    I need to check if there already is an existing column named "Colors", if so, it should do nothing, otherwise it should add a new column named "Colors".

    Then I need it to do the same with columns "GreenFruits" and "Fruits".

    This is the code is use in Table (2):
    Please Login or Register  to view this content.
    The first step goes well. List.Count(List.FindText(Table.ColumnNames(Source), "Colors" )) is 1, so it does nothing.
    The second step goes well. List.Count(List.FindText(Table.ColumnNames(Colors), "GreenFruits" )) is 0, so it creates the column named "GreenFruits".
    The last step does not go well. List.Count(List.FindText(Table.ColumnNames(GreenFruits), "Fruits" )) counts 3, as it finds both "Fruits", "GreenFruits" and "BlueFruits". Which means it tries to create a new column called "Fruits". But that one already exists, so it returns this error:
    AddNonExistingColumns2.png

    Instead of getting 3 counts on "Fruits", ie "Fruits", "GreenFruits" and "BlueFruits", I need it to only get 1 count, ie an exact match for "Fruits".

    The expected result should be 4 columns in the end:
    Fruits BlueFruits Colors GreenFruits
    Last edited by Imbizile; 03-30-2021 at 05:36 AM.

  6. #6
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: PQ FindText exact matching columnname

    perhaps something like:

    Please Login or Register  to view this content.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: PQ FindText exact matching columnname

    Perhaps you could try List.PositionOf to check if the column name is in the list?

    That function will return -1 if the column name you are looking for isn't found.

    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  8. #8
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: PQ FindText exact matching columnname

    Thank you both XLent and Norie - both solutions works as intended

  9. #9
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: PQ FindText exact matching columnname

    FWIW, I much prefer Norie's suggestion !

  10. #10
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,222

    Re: PQ FindText exact matching columnname

    Another one, Add list of columns

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: PQ FindText exact matching columnname

    Thanks for that contribution as well, Bo_Ry

  12. #12
    Registered User
    Join Date
    03-28-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    11

    Re: PQ FindText exact matching columnname

    Hidden behind the Table.SelectColumns function is the ability to add columns with null values if they do not exist in the source table.

    the formula looks like this:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VLOOKUP & Table ColumnName
    By dageci in forum Excel General
    Replies: 2
    Last Post: 06-02-2019, 10:32 AM
  2. [SOLVED] Find exact text in exact cell but on any sheet?
    By nobodyukno in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2017, 02:59 PM
  3. Replies: 4
    Last Post: 01-06-2015, 09:15 PM
  4. To Sort exact and partial exact match for a single column.
    By Jagdev in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-22-2014, 05:08 AM
  5. Formula to work out an exact average over an exact number
    By Sandyshirl in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-11-2013, 01:35 AM
  6. SQL against Excel Table returns nothing when "Where [ColumnName] = ""
    By rgi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2010, 04:21 PM
  7. FindText Extraction?
    By flembss2 in forum Excel General
    Replies: 4
    Last Post: 06-03-2008, 04:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1