excel - 列值等于列表项,如果项是多个列之一中的子字符串
问题描述
使用 PowerQuery 我想创建一个新列,其值等于列表 {MED_NAME_LIST} 中的一个项目,如果该项目是多个列之一中的子字符串:[Name]、[GenericName]、[SimpleGenericName]。
在上一篇文章中,我询问了如何根据多个文本列([Name]、[GenericName] 或 [SimpleGenericName])是否包含列表中的子字符串进行过滤。
该解决方案运行良好,但现在我意识到我还需要知道匹配的列表项是什么。
以前基于列表进行过滤的解决方案:
MED_NAME_LIST = {"MEthYlprednisolone", "hYdroxychloroquine", "rEMdesivir"},
initialTable = Table.FromRows({
{"Methylprednisolone Tab", "train", "car", "bike"},
{"no", "no", "no", "no"},
{"tram", "teleport", "hydroxychloroQuine Tab", "jet"},
{"no", "no", "no", "yes"},
{"REMdesivir Tab", "bus", "taxi", "concord"}
}, type table [Name = text, GenericName = text, SimpleGenericName = text, SomeOtherColumn = text]),
filtered = Table.SelectRows(initialTable, each List.ContainsAny(
{[Name], [GenericName], [SimpleGenericName]},
MED_NAME_LIST,
(rowValue as text, medicineFromList as text) as logical => Text.Contains(rowValue, medicineFromList, Comparer.OrdinalIgnoreCase)
))
in
filtered```
解决方案
自定义将是最后一场比赛。
如果有多个匹配项,Custom.1 将是所有匹配项的列表
let MED_NAME_LIST = {"MEthYlprednisolone", "hYdroxychloroquine", "rEMdesivir"},
initialTable = Table.FromRows({
{"Methylprednisolone Tab", "train", "car", "bike"},
{"no", "no", "no", "no"},
{"tram", "teleport", "hydroxychloroQuine Tab", "jet"},
{"no", "no", "no", "yes"},
{"REMdesivir Tab", "bus", "taxi", "concord"}
}, type table [Name = text, GenericName = text, SimpleGenericName = text, SomeOtherColumn = text]),
filtered = Table.SelectRows(initialTable, each List.ContainsAny(
{[Name], [GenericName], [SimpleGenericName]},
MED_NAME_LIST,
(rowValue as text, medicineFromList as text) as logical => Text.Contains(rowValue, medicineFromList, Comparer.OrdinalIgnoreCase)
)),
#"Added Custom" = Table.AddColumn(filtered, "Custom", each List.Accumulate(List.Transform(MED_NAME_LIST, each Text.Lower(_)), null, (state, current) => if Text.Contains(Text.Lower([Name]&[GenericName]&[SimpleGenericName]), current) then current else state)),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each List.Accumulate(List.Transform(MED_NAME_LIST, each Text.Lower(_)), {}, (state, current) => if Text.Contains(Text.Lower([Name]&[GenericName]&[SimpleGenericName]), current) then List.Combine({{current}, state}) else state))
in #"Added Custom1"
推荐阅读
- java - 对 2 个数组进行排序但在 Java 中只考虑其中一个的最有效方法?
- c# - 如何将手机连接到 Rest Api,以便我可以通过它发送短信?
- python - 如何使用 numpy 对泊松回归进行最大似然估计(MLE)
- typescript - 带有 TypeScript 的 Cypress 代码覆盖率插件
- visual-studio-code - 在 VScode 中导入测试模块时出现导入错误
- sql - SQL Server 2016:如何获取单行视图
- amazon-web-services - 无法再使用 React native 将图像上传到 Amazon S3 存储桶
- reactjs - 如何在 KeyboardDatePicker 中显示自定义文本(占位符)
- python - 将嵌套的 Jsonl 文件转换为 CSV 格式:取消嵌套 Jsonl 并提取为 CSV
- python - 使用 Python 文件在 Robot Framework 中将变量定义为变量