SOLVED

Power Query: Filter table based on list with wildcards

%3CLINGO-SUB%20id%3D%22lingo-sub-2418638%22%20slang%3D%22en-US%22%3EPower%20Query%3A%20Filter%20table%20based%20on%20list%20with%20wildcards%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2418638%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20this%20list%20in%20an%20Excel%20sheet%3A%3C%2FP%3E%3CP%3EFOL%2F%3C%2FP%3E%3CP%3ECREDIT%3C%2FP%3E%3CP%3E...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20my%20Power%20Query%20to%20filter%20a%20given%20table%20so%20that%20everything%20beginning%20with%20%22FOL%2F%22%20and%20%22CREDIT%22%20in%20the%20column%20%22Name%22%20(eg.%20FOL%2F1254%2C%20FOL%2FGT556%2C%20...%20CREDIT1%2C%20CREDITCON%2C%20...)%20is%20filtered%20out.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20already%20found%20%3CA%20href%3D%22https%3A%2F%2Feriksvensen.wordpress.com%2F2017%2F12%2F12%2Fpowerquery-filter-a-table-based-on-another-table-column-or-list-and-some-filter-ahas%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ethis%20nice%20explanation%3C%2FA%3E%20on%20how%20to%20filter%20using%20values%20in%20a%20table%2C%20but%20not%20with%20wildcards%20and%20%22begins%20with%22%20functionality.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20very%20appreciated!%20Many%20thanks%20in%20advance!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2418638%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2418673%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%3A%20Filter%20table%20based%20on%20list%20with%20wildcards%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2418673%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EStandard%20filter%20includes%20quite%20a%20lot%20of%20options%2C%20includes%20%22Begins%20with...%22%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20538px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F286502iAB88596430C23DEC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2418965%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%3A%20Filter%20table%20based%20on%20list%20with%20wildcards%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2418965%22%20slang%3D%22en-US%22%3EYes%2C%20but%20I%20want%20it%20to%20fetch%20the%20%22begins%20with%22%20string%20from%20another%20table...%20How%20could%20that%20be%20done%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2418967%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%3A%20Filter%20table%20based%20on%20list%20with%20wildcards%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2418967%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnother%20table%20could%20have%20a%20lot%20of%20records.%20Would%20you%20like%20to%20filter%20on%20%22begin%20with%22%20for%20all%20of%20them%20or%20for%20some%20specific%20records%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2419019%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%3A%20Filter%20table%20based%20on%20list%20with%20wildcards%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2419019%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20variant%2C%20if%20you%20would%20like%20to%20filter%20first%20table%20on%20the%20texts%20in%20the%20second%20table%20from%20which%20first%20table%20text%20start%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20281px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F286524i9F8567BA1CE914BC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3Ethe%20query%20could%20be%20like%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table1%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20%23%22Added%20Custom%22%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20Source%2C%0A%20%20%20%20%20%20%20%20%22Custom%22%2C%0A%20%20%20%20%20%20%20%20each%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Ba%3D%5BA%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20b%3DList.Contains(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20List.Transform(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Table2%5BB%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20each%20Text.StartsWith(a%2C%20_)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%5Bb%5D%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Filtered%20Rows%22%20%3D%20Table.SelectRows(%0A%20%20%20%20%20%20%20%20%23%22Added%20Custom%22%2C%20each%20(%5BCustom%5D%20%3D%20true)%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Removed%20Other%20Columns%22%20%3D%20Table.SelectColumns(%0A%20%20%20%20%20%20%20%20%23%22Filtered%20Rows%22%2C%0A%20%20%20%20%20%20%20%20%7B%22A%22%7D%0A%20%20%20%20)%0Ain%0A%20%20%20%20%23%22Removed%20Other%20Columns%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EList.Transform%20checks%20if%20for%20any%20element%20of%20second%20table%20column%20A%20starts%20from%20it.%20With%20List.Contains%20we%20check%20if%20there%20are%20at%20least%20one%20TRUE.%20Finally%20filter%20on%20this%20column%20and%20remove%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

I have this list in an Excel sheet:

FOL/

CREDIT

...

 

I need my Power Query to filter a given table so that everything beginning with "FOL/" and "CREDIT" in the column "Name" (eg. FOL/1254, FOL/GT556, ... CREDIT1, CREDITCON, ...) is filtered out.

 

I already found this nice explanation on how to filter using values in a table, but not with wildcards and "begins with" functionality.

 

Any help would be very appreciated! Many thanks in advance!

 

11 Replies

@bartvana 

Standard filter includes quite a lot of options, includes "Begins with..."

image.png

Yes, but I want it to fetch the "begins with" string from another table... How could that be done?

@bartvana 

Another table could have a lot of records. Would you like to filter on "begin with" for all of them or for some specific records?

best response confirmed by bartvana (Frequent Contributor)
Solution

@bartvana 

As variant, if you would like to filter first table on the texts in the second table from which first table text start

image.png

the query could be like

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Custom" = Table.AddColumn(
        Source,
        "Custom",
        each
            [a=[A],
                b=List.Contains(
                    List.Transform(
                        Table2[B],
                        each Text.StartsWith(a, _)
                    ),
                true
                )
            ][b]
    ),
    #"Filtered Rows" = Table.SelectRows(
        #"Added Custom", each ([Custom] = true)
    ),
    #"Removed Other Columns" = Table.SelectColumns(
        #"Filtered Rows",
        {"A"}
    )
in
    #"Removed Other Columns"

List.Transform checks if for any element of second table column A starts from it. With List.Contains we check if there are at least one TRUE. Finally filter on this column and remove it.

Yes, thank you, that's exactly what I needed.

@bartvana 

You are welcome.

Perhaps variant with function will be more transparent.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    fnIsBeginsWith = (str, list) => List.Contains(
            List.Transform(list, each Text.StartsWith(str, _)),
            true),
    #"Added Custom" = Table.AddColumn(
        Source,
        "Custom",
        each fnIsBeginsWith([A], Table2[B])
    ),
    #"Filtered Rows" = Table.SelectRows(
        #"Added Custom", each ([Custom] = true)
    ),
    #"Removed Other Columns" = Table.SelectColumns(
        #"Filtered Rows",
        {"A"}
    )
in
    #"Removed Other Columns"
Will this filter Rows, or just Cells?

@Crafting4U 

Not sure I understood the question. In general in PQ we operate with records (aka rows in tables) and lists (aka columns in tables), what do you mean under filtering of cells?

I think that confirms that my fear is unfounded. In Excel, if I select a range and delete, I must decide how adjacent cells will shift to fill in, effectively changing an order of 3 blue widgets to an order of 1125 yellow turtles.

@Crafting4U 

In Power Query it works the same way as with filtering of structured tables in Excel grid. If you apply filter you say which rows to exclude from the next view.

Thanks. I have more questions but I'll start a new thread rather than hijacking this one.