Power Query: Function to search a column for a list of keywords and return only rows with at least one match

空扰寡人 提交于 2020-06-17 12:58:38

问题


I am making a simple Google-like search function in Power Query. Let's say I have a column called Description in a table called Database. The user then inputs some search queries like "dog, cat, animals". I want to filter Database for rows that contain at least one of these keywords. They keywords can change each time, depending on what the user types in a named range in Excel.

I know you can filter a column in Power Query for multiple keywords, like this:

FilterRows = Table.SelectRows(LastStep, each Text.Contains([English], "dog") or Text.Contains([English], "cat")),

but those keywords are static, and the column is also static. I want to be able to control both the keywords and the column name as variables. I think I need to write a function but I am not sure how to start.


回答1:


Your question requires several moving parts.

First, I would get the keywords from a named range "Keywords" into a table like this:

{KeywordTbl}
let
    GetKeywords = if Excel.CurrentWorkbook(){[Name="Keywords"]}[Content]{0}[Column1] = null then null else Text.Split(Excel.CurrentWorkbook(){[Name="Keywords"]}[Content]{0}[Column1], ", "),
    ConvertToTable = Table.FromList(GetKeywords,null,{"Keywords"})
in
    ConvertToTable

Secondly, store the column name where you want to search in an Excel named range called "ColName". Then pull the named range into Power Query like this:

{ColName}
let
    GetColName = Excel.CurrentWorkbook(){[Name="ColName"]}[Content]{0}[Column1]
in
    GetColName

Then I would write a function that takes 4 variables, the table and column you want to look in, and the table and column containing the keywords:

{SearchColForKeywords}
(LookInTbl as table, KeywordTbl as table, LookInCol as text, KeywordCol as text) =>
let
    RelativeMerge = Table.AddColumn(LookInTbl, "RelativeJoin", 
        (Earlier) => Table.SelectRows(KeywordTbl, 
            each Text.Contains(Record.Field(Earlier, LookInCol), Record.Field(_, KeywordCol), Comparer.OrdinalIgnoreCase))),
    ExpandRelativeJoin = Table.ExpandTableColumn(RelativeMerge, "RelativeJoin", {KeywordCol}, {"Keywords found"}),
    FilterRows = Table.SelectRows(ExpandRelativeJoin, each [Keywords found] <> null and [Keywords found] <> ""),

    // Concatenate multiple keyword founds line into one line 
    GroupAllData = Table.Group(FilterRows, {"Word ID"}, {{"AllData", each _, type table [First column=text, Second column=text, ... your other columns=text]}}),
    AddCol = Table.AddColumn(GroupAllData, "Keywords found", each [AllData][Keywords found]),
    ExtractValues = Table.TransformColumns(AddCol, {"Keywords found", each Text.Combine(List.Transform(_, Text.From), ", "), type text}),
    DeleteAllData = Table.RemoveColumns(ExtractValues,{"AllData"}),
    MergeQueries = Table.NestedJoin(DeleteAllData, {"Word ID"}, FilterRows, {"Word ID"}, "DeleteAllData", JoinKind.LeftOuter),
    ExpandCols = Table.ExpandTableColumn(MergeQueries, "DeleteAllData", {"First Col name", "Second col name", ... "Your Other column names here"}),
    DeleteKeywordsFound = Table.RemoveColumns(ExpandCols,{"Keywords found"})
in
    DeleteKeywordsFound

FYI, half of this function has been developed by a user named lmkeF on PowerBI community. The full discussion is here. I merely improved on his solution.

Finally, I will use that function in another query like this:

StepName = SearchColForKeywords(MainTbl, KeywordTbl, ColName, "Keywords"),

You may customize the 4 variable names.



来源:https://stackoverflow.com/questions/61117213/power-query-function-to-search-a-column-for-a-list-of-keywords-and-return-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!