用于在 table 列表中搜索匹配关键字和 return 匹配行前面的 cel 中的文本的 Power Query 函数
Power Query Function to search for matching keywords in a table of lists and return the text in the cel in front of the matching row
我有一个类似的问题,但比这个问题更复杂:
Power Query: Function to search a column for a list of keywords and return only rows with at least one match and this one : https://community.powerbi.com/t5/Desktop/Power-query-Add-column-with-list-of-keywords-found-in-text/td-p/83109
我有一个包含很多列的数据库,其中一个是自由文本描述字符串。
在工作簿中的另一个 Excel Sheet 上,我设置了一个匹配 table 以根据关键字列表对行进行分类,如下所示:
- 类别 |关键词
- 宠物 |狗,猫,兔子,...
- 汽车 |保时捷、宝马、道奇、...
- ...
目标是在我的数据库中放置一个自定义列,该列将 return 上述类别(或多个类别?)基于它可以在描述字段中找到的列出的关键字。
我认为上面的解决方案和来自 ImkeF 的解决方案还不算太远,但我没有找到一种方法将其变成我的案例的成功查询。 (我擅长 Excel 但对 M 和编程查询相当菜鸟...)
以上面发布的链接为导向:
tbl_category 的 M 代码:关键字(以逗号分隔)将分成行
let
Source = Excel.CurrentWorkbook(){[Name="tbl_category"]}[Content],
#"Replaced Value" = Table.ReplaceValue(Source," ","",Replacer.ReplaceText,{"keywords"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value", {{"keywords", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "keywords"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"keywords", type text}})
in
#"Changed Type1"
tbl_text 的 M 代码。这里将添加一个名为 "Category":
的自定义列
let
Source = Excel.CurrentWorkbook(){[Name="tbl_text"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Category", (Earlier) => Table.SelectRows(tbl_category,
each Text.Contains(Record.Field(Earlier, "Text"), Record.Field(_, "keywords"), Comparer.OrdinalIgnoreCase))),
#"Expanded Category" = Table.ExpandTableColumn(#"Added Custom", "Category", {"Category"}, {"Category"})
in
#"Expanded Category"
好的,
我终于找到了如何根据您的上述步骤构建适合我需要的查询!
注意:为了清楚起见,我使用“行标签”替换了第一 tbl_category 列的 header 列。
我的解决方案并不像我想要的那样简洁(我不得不创建第二个自定义列,因为我不了解如何嵌套这两个步骤以便它们作用于同一个单元格)但它工作得很好!
再次感谢您的帮助,克里斯...如果没有您的线索,我不会找到这个迷宫出口!
这里修改了第二个代码:
let
Source = Excel.CurrentWorkbook(){[Name="tbl_text"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Category",
(Earlier) => Table.SelectRows(tbl_category,
each Text.Contains(Record.Field(Earlier, "Text"), Record.Field(_, "keywords"),
Comparer.OrdinalIgnoreCase))),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom",
each Text.Combine(Table.ToList(Table.Transpose(
Table.Distinct(Table.SelectColumns([Category],{"Row Labels"}))),
Combiner.CombineTextByDelimiter(",")), ", ")),
in
#"Added Custom1"
问候
仅作记录,
一旦应用于真实数据,查询就不再有效...给出错误“我们无法将值 null 转换为文本类型。”
解决方案就像先删除“空”单元格(空白单元格是尚未确定关键字的类别)一样简单!
tbl_category 的 M 代码:
let
Source = Excel.CurrentWorkbook(){[Name="tbl_category"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each ([keywords] <> null)),
#"Replaced Value" = Table.ReplaceValue(#"Filtered Rows"," ","",Replacer.ReplaceText,{"keywords"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value", {{"keywords", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "keywords"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"keywords", type text}})
in
#"Changed Type1"
问候
我有一个类似的问题,但比这个问题更复杂: Power Query: Function to search a column for a list of keywords and return only rows with at least one match and this one : https://community.powerbi.com/t5/Desktop/Power-query-Add-column-with-list-of-keywords-found-in-text/td-p/83109
我有一个包含很多列的数据库,其中一个是自由文本描述字符串。 在工作簿中的另一个 Excel Sheet 上,我设置了一个匹配 table 以根据关键字列表对行进行分类,如下所示:
- 类别 |关键词
- 宠物 |狗,猫,兔子,...
- 汽车 |保时捷、宝马、道奇、...
- ...
目标是在我的数据库中放置一个自定义列,该列将 return 上述类别(或多个类别?)基于它可以在描述字段中找到的列出的关键字。
我认为上面的解决方案和来自 ImkeF 的解决方案还不算太远,但我没有找到一种方法将其变成我的案例的成功查询。 (我擅长 Excel 但对 M 和编程查询相当菜鸟...)
以上面发布的链接为导向:
tbl_category 的 M 代码:关键字(以逗号分隔)将分成行
let
Source = Excel.CurrentWorkbook(){[Name="tbl_category"]}[Content],
#"Replaced Value" = Table.ReplaceValue(Source," ","",Replacer.ReplaceText,{"keywords"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value", {{"keywords", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "keywords"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"keywords", type text}})
in
#"Changed Type1"
tbl_text 的 M 代码。这里将添加一个名为 "Category":
的自定义列let
Source = Excel.CurrentWorkbook(){[Name="tbl_text"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Category", (Earlier) => Table.SelectRows(tbl_category,
each Text.Contains(Record.Field(Earlier, "Text"), Record.Field(_, "keywords"), Comparer.OrdinalIgnoreCase))),
#"Expanded Category" = Table.ExpandTableColumn(#"Added Custom", "Category", {"Category"}, {"Category"})
in
#"Expanded Category"
好的,
我终于找到了如何根据您的上述步骤构建适合我需要的查询!
注意:为了清楚起见,我使用“行标签”替换了第一 tbl_category 列的 header 列。
我的解决方案并不像我想要的那样简洁(我不得不创建第二个自定义列,因为我不了解如何嵌套这两个步骤以便它们作用于同一个单元格)但它工作得很好!
再次感谢您的帮助,克里斯...如果没有您的线索,我不会找到这个迷宫出口!
这里修改了第二个代码:
let
Source = Excel.CurrentWorkbook(){[Name="tbl_text"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Category",
(Earlier) => Table.SelectRows(tbl_category,
each Text.Contains(Record.Field(Earlier, "Text"), Record.Field(_, "keywords"),
Comparer.OrdinalIgnoreCase))),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom",
each Text.Combine(Table.ToList(Table.Transpose(
Table.Distinct(Table.SelectColumns([Category],{"Row Labels"}))),
Combiner.CombineTextByDelimiter(",")), ", ")),
in
#"Added Custom1"
问候
仅作记录,
一旦应用于真实数据,查询就不再有效...给出错误“我们无法将值 null 转换为文本类型。” 解决方案就像先删除“空”单元格(空白单元格是尚未确定关键字的类别)一样简单!
tbl_category 的 M 代码:
let
Source = Excel.CurrentWorkbook(){[Name="tbl_category"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each ([keywords] <> null)),
#"Replaced Value" = Table.ReplaceValue(#"Filtered Rows"," ","",Replacer.ReplaceText,{"keywords"}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value", {{"keywords", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "keywords"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"keywords", type text}})
in
#"Changed Type1"
问候