+ Reply to Thread
Results 1 to 7 of 7

Power Query Select Columns based on column content

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    792

    Power Query Select Columns based on column content

    I have come up with this way to select columns based on text content ( cat , dog) , but it involves nested
    List.Transforms, so I wondered if anyone has a different approach , I have attached the workbook with two examples,

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        ToCol = Table.ToColumns( Source ),
        Split_contain = List.Transform(  ToCol, (A)=> 
       List.Transform(A, (B)=> List.ContainsAny( Splitter.SplitTextByWhitespace() (B), {"cat","dog"} ) ) ),
        TrueFalse = List.Transform(  Split_contain, (x)=> List.AnyTrue(x)),
        Positions = List.PositionOf( TrueFalse, true, Occurrence.All ),
        ColNames = List.Transform(  Positions , (A)=> Table.ColumnNames( Source ){A} ),
        Custom7 = Table.SelectColumns( Source , 
     ColNames )
    in
        Custom7

    If anyone has any thought on a different approach I would be interested.

    Richard.
    Attached Files Attached Files

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,028

    Re: Power Query Select Columns based on column content

    You could use something like:

    Table.SelectColumns(Source, List.Accumulate(Table.ColumnNames(Source), {}, (s, c)=>if List.Count(List.Select(Table.Column(Source, c), each Text.Contains(_, "dog") or Text.Contains(_, "cat"))) > 0 then s & {c} else s))
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    05-05-2024
    Location
    Moscow, Russia
    MS-Off Ver
    365
    Posts
    30

    Re: Power Query Select Columns based on column content

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        headers = List.Buffer(Table.ColumnNames(Source)), 
        contains = (lst, var) => List.Contains(lst, var, (x, y) => Text.Contains(x, y)),
        columns = Table.ToColumns(Source), 
        poz = List.PositionOfAny(
            columns, 
            {"cat", "dog"}, 
            Occurrence.All, 
            (x, y) => contains(x, y)
        ), 
        select = Table.SelectColumns(
            Source,
            List.Transform(poz, (x) => headers{x})
        )
    in
        select

  4. #4
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    792

    Re: Power Query Select Columns based on column content

    Thanks, have not had a chance to go through it step by step but looks good.

    RD

  5. #5
    Registered User
    Join Date
    05-05-2024
    Location
    Moscow, Russia
    MS-Off Ver
    365
    Posts
    30

    Re: Power Query Select Columns based on column content

    yet another option
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        headers = List.Buffer(Table.ColumnNames(Source)), 
        columns = Table.ToColumns(Source), 
        poz = List.PositionOf(
            columns, 
            {"cat", "dog"}, 
            Occurrence.All, 
            (x, y) => List.ContainsAny(x, y, (w, z) => Text.Contains(w, z))
        ), 
        select = Table.SelectColumns(
            Source,
            List.Transform(poz, (x) => headers{x})
        )
    in
        select

  6. #6
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    792

    Re: Power Query Select Columns based on column content

    Ok, I'll close this that is enough to be going on with , thanks again, 😉

  7. #7
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    792

    Re: Power Query Select Columns based on column content

    Just to say thanks again, I have not yet gone through the second version,
    but after the first I came up with this

    = (alist as list , valuelist  as list  )=>  
    List.ContainsAny( 
        alist  , valuelist  , 
    (x,y)=> Text.Contains( x,y ) )
    I think this is close to your second , would never have got this without the help.

+ 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. Replies: 6
    Last Post: 05-10-2024, 12:55 AM
  2. Power Query: Split Column into two columns by every other row
    By HejHelloEver in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-10-2023, 05:59 PM
  3. [SOLVED] [Power Query] How to filter 0 from two columns in power query editor in one step?
    By daliye in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2023, 09:44 PM
  4. power query filter by content of other column
    By TtNl in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-03-2022, 10:28 AM
  5. [SOLVED] Identify duplicates based on other columns - Power query
    By gmalpani in forum Office 365
    Replies: 4
    Last Post: 06-07-2021, 11:34 AM
  6. Replies: 10
    Last Post: 06-25-2020, 12:19 AM
  7. Custom Permutations Column in Power Query from two columns in the same Query
    By PaintPaddy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2019, 02:48 PM

Tags for this Thread

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