匹配两列之间的行以获得完全匹配或部分匹配

时间:2020-03-10 12:21:30

标签: excel powerbi powerquery

我在使用power bi编写查询时遇到了问题,该查询可以部分或完全匹配两行。除此之外,我还要尝试计算匹配百分比(如果找到)并将结果放在新列中。

实际的数据集包含很多行和表,但是为了这个示例,我仅使用4列。

“ ID”和“文本”列是唯一标识的。 “ KI ID”和“ KI测试”也是唯一的,但仅在发生匹配时才与“ ID”和“文本”列无关。

我需要实现的是以下内容:

我想将“文本”列中每一行的输入与“ KI文本”列中的每一行进行匹配。如果有匹配项,那么我想知道“ KI ID”和“匹配项”百分比。查看数据集以获得更好的见解。

ps:这实际上是可以通过功率查询实现的,还是只是一种幻想,因为我认为我正朝着机器学习的方向迈进?

数据集 https://drive.google.com/open?id=1JrsxPa6DICNi5N-tI5ESukh62W_uedaQ

enter image description here

匹配计算基于“文本”和“ KI文本”两列中出现的单词数量。例如,如果“文本”列中的一行包含两个句子,并且这些句子与“ KI文本”行中的一部分匹配,而其中“ KI文本”行共有6个句子。行之间的匹配是部分匹配的,因此基本上应该将其计算为2/6,这样匹配率为33,3%。

此外,“ KI文本”列包含许多行,这些行可以与“文本”列行之一相匹配。仅当它大于或等于80%时,它才应显示结果,否则就没有意思了。

1 个答案:

答案 0 :(得分:0)

希望这是您正在寻找的东西,或者至少使您更接近答案。

虽然我相信您的ID /文本和KI ID / KI文本数据很可能来自两个不同的表,但是您将它们作为data set呈现在一个电子表格中,所以我从此开始。电子表格中对我唯一可用的内容是Sheet2。我只是将电子表格的Sheet2的内容复制并粘贴到Excel中的电子表格中。然后,我使用Table1的默认表名称来引用它。我把它带进去就像您展示它一样:

enter image description here

然后,仍在同一查询中工作,我将其分为两个新表:一个ID表和一个KI ID表。您可以通过选择现有的ID和Text列,然后选择Home> Remove Columns> Remove Other Columns创建第一个表;但是要制作第二张表,您需要使用编辑栏。

enter image description here

在制作第二张表之前,我在第一张表(ID表)中添加了一列,每一行的编号为1。我称该列为ID匹配键。

enter image description here

然后,我从创建该表的应用步骤复制了第一张表的公式,以在公式栏中使用它来为KI ID创建第二张表。我对其进行了编辑,并根据KI ID更改了列的名称:

enter image description here

然后,我在第二个表(KI ID表)中添加了一行,每行的编号为1。我将该列称为KI ID匹配键。

enter image description here

然后,我对刚才创建的两个表(ID和KI ID表)进行了完全外部合并。为此,我首先使用Home> Merge Queries并将Table1与自身合并。 (用于匹配的列无关紧要,因为这是临时的。)我选择Full Outer作为Join Kind。合并完成后,我在编辑栏中编辑了合并,将表更改为#“添加的ID匹配键”和#“添加的KI ID匹配键”(恰好是这两个“表”的名称)在我创建它们并将它们的匹配键添加到此合并中之后),并将相应的匹配字段分别添加到“ ID匹配键”和“ KI ID匹配键”:

enter image description here

合并之后,我展开了结果列:

enter image description here

然后,我通过选择两个列(通过单击一个列,然后单击并按住Ctrl),然后单击变换>替换值>在“要查找的值”中输入null,将Text和KI Text列中的所有空值都替换为空白文本。 ”框,将“替换为”框留空,然后单击“确定”。

然后,我添加了列以将每个“文本”和“ KI文本”列单元格拆分为它们的单词列表。在进行每次拆分之前,我先过滤掉所有内容,除了小写和大写文本a到z,数字0到9,空格和等号。我使用Text.SplitAny在任何空格或等号处分割单词:

enter image description here

enter image description here

然后,我添加一列并确定KI Text中Text中的哪些单词。我使用List.Intersect来做到这一点。它列出了重复项,这是我想要的:

enter image description here

然后我添加一列,然后做相反的操作-确定KI Text中的单词在Text中是什么。同样,我使用了List.Intersect:

enter image description here

然后我收集了计数信息。我创建了一个新列,其中列出了我创建的每个列表中的单词数(所有出现的次数)。我对文本,KI文本,KI文本中的文本和KI文本中的文本分别执行了一次。我只是使用List.Count来做到这一点。实际上,这不必作为带有单独列的单独步骤来执行。我只是为了清楚地看到数字。我在这四列之后的两列中生成的M代码不使用这四列。我也在接下来的两列中进行了计数:

enter image description here

接下来,我创建了两个新列来计算KI文本中的Text百分比和Text中的KI Text的百分比。我使用了这些基本公式(希望以清晰的英语和M代码呈现在这里):

  • KI文本中的%文本=(KI文本中来自文本的单词数/ KI文本中的单词数)* 100
  • %KI文本=( KI文字中的文字(文字/文字数)* 100

    请务必检查我的数学。

enter image description here

最后,我删除了所有不再需要的列。

enter image description here

在您最初发表三条评论之后,我在查询中添加了一些步骤以获取此信息:

enter image description here

这是M代码,其中包含我在您的三个评论之后所做的修改。请参阅我的评论以获取更多上下文。

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Text", type text}, {"KI ID", type text}, {"KI Text", type text}, {"Outcome", type text}}),
    #"Made ID Table" = Table.SelectColumns(#"Changed Type",{"ID", "Text"}),
    #"Added ID Match Key" = Table.AddColumn(#"Made ID Table", "ID Match Key", each 1),
    #"Made KI ID Table" = Table.SelectColumns(#"Changed Type",{"KI ID", "KI Text"}),
    #"Added KI ID Match Key" = Table.AddColumn(#"Made KI ID Table", "KI ID Match Key", each 1),
    #"Merged Queries" = Table.NestedJoin(#"Added ID Match Key", {"ID Match Key"}, #"Added KI ID Match Key", {"KI ID Match Key"}, "KI Table", JoinKind.FullOuter),
    #"Expanded KI Table" = Table.ExpandTableColumn(#"Merged Queries", "KI Table", {"KI ID Match Key", "KI ID", "KI Text"}, {"KI ID Match Key", "KI ID", "KI Text"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded KI Table",null,"",Replacer.ReplaceValue,{"Text", "KI Text"}),
    #"Split Text to List" = Table.AddColumn(#"Replaced Value", "Text Listed", each Text.SplitAny(Text.Select([Text],{"a".."z","A".."Z","0".."9"," ","="})," =")),
    #"Split KI Text to List" = Table.AddColumn(#"Split Text to List", "KI Text Listed", each Text.SplitAny(Text.Select([KI Text],{"a".."z","A".."Z","0".."9"," ","="})," =")),
    #"Got Text in KI Text" = Table.AddColumn(#"Split KI Text to List", "Text in KI Text", each List.Intersect({[KI Text Listed],[Text Listed]})),
    #"Got KI Text in Text" = Table.AddColumn(#"Got Text in KI Text", "KI Text in Text", each List.Intersect({[Text Listed], [KI Text Listed]})),
    //You don't actually need the next four lines. I included them so you can see the numbers in the table. They are calculated in the two #"Calculated..." lines below.
    //If you choose to remove them, you'll need to replace #"Got Count of KI Text in Text", in the first #"Calculated..." line, with #"Got KI Text in Text".
    #"Got Count of Text" = Table.AddColumn(#"Got KI Text in Text", "Text Count", each List.Count([Text Listed])),
    #"Got Count of KI Text" = Table.AddColumn(#"Got Count of Text", "KI Text Count", each List.Count([KI Text Listed])),
    #"Got Count of Text in KI Text" = Table.AddColumn(#"Got Count of KI Text", "Text in KI Text Count", each List.Count([Text in KI Text])),
    #"Got Count of KI Text in Text" = Table.AddColumn(#"Got Count of Text in KI Text", "KI Text in Text Count", each List.Count([KI Text in Text])),
    #"Calculated % Text in KI Text" = Table.AddColumn(#"Got Count of KI Text in Text", " % Text in KI Text", each Number.Round((List.Count([Text in KI Text])/List.Count([KI Text Listed]))*100, 2), type number),
    #"Calculated % KI Text in Text" = Table.AddColumn(#"Calculated % Text in KI Text", "% KI Text in Text", each Number.Round((List.Count([KI Text in Text])/List.Count([Text Listed]))*100, 2), type number),
    #"Removed Other Columns" = Table.SelectColumns(#"Calculated % KI Text in Text",{"ID", "Text", "KI ID", "KI Text", " % Text in KI Text", "% KI Text in Text"}),
    //
    //What follows is my edit after your three comments.
    #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Outcome", each Text.From([#" % Text in KI Text"]) & "% match with " & [KI ID]),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"ID"}, {{"AllData", each _, type table [ID=text, Text=text, KI ID=text, KI Text=text, #" % Text in KI Text"=number, #"% KI Text in Text"=number, Outcome=text]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Text", each [AllData][Text]{0}),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Outcome", each [AllData][Outcome]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom2", {"Outcome", each Text.Combine(List.Transform(_, Text.From), "#(cr)"), type text}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Extracted Values",{"ID", "Text", "Outcome"})
in
    #"Removed Other Columns1"