Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在两列之间匹配行以获得精确或部分匹配_Excel_Powerbi_Powerquery - Fatal编程技术网

Excel 在两列之间匹配行以获得精确或部分匹配

Excel 在两列之间匹配行以获得精确或部分匹配,excel,powerbi,powerquery,Excel,Powerbi,Powerquery,我在power bi中编写部分或完全匹配两行的查询时遇到问题。除此之外,我还试图计算匹配百分比(如果找到),并将结果放入新列中 实际的数据集包含很多行和表,但是为了这个示例,我只使用了4列 “ID”和“Text”列是唯一标识的。”“KI ID”和“KI Test”也是唯一的,但它们仅在匹配时与列“ID”和“文本”无关 我需要实现以下内容: 我想将“文本”列中每一行的输入与“KI文本”列中的每一行匹配。如果有匹配,那么我想知道“KI ID”和匹配百分比。查看数据集以获得更好的洞察力 ps:这真的可

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

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

“ID”和“Text”列是唯一标识的。”“KI ID”和“KI Test”也是唯一的,但它们仅在匹配时与列“ID”和“文本”无关

我需要实现以下内容:

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

ps:这真的可以通过power查询实现吗?还是仅仅是一个幻想,因为在我看来,我正在走向机器学习,我想

数据集

匹配计算基于“Text”和“KI Text”两列中出现的字数。例如,如果“文本”列中的一行包含两个句子,并且这些句子与“KI文本”行中的一个部分匹配,该行总共有6个句子。行之间的匹配是部分的,所以基本上它应该计算为2/6,所以它就像33,3%的匹配


此外,“KI Text”列包含许多行,这些行可以与“Text”列中的一行相匹配。只有当它大于或等于80%时,它才应该显示结果,否则它就不有趣了。

希望这就是你想要的,或者至少让你更接近答案

虽然我相信您的ID/Text和KI ID/KI Text数据很可能来自两个不同的表,但您在一个电子表格中显示了它们作为您的数据,所以我从这开始。在您的电子表格中,我唯一可用的内容是Sheet2。我只是简单地复制并粘贴了您的电子表格Sheet2的内容到我自己的Excel电子表格中。然后我使用默认表名Table1来引用它。我带来的就像你展示的一样:

然后,仍然在同一个查询中工作,我将它分为两个新表:一个ID表和一个kiid表。您可以通过选择现有ID和文本列,然后选择Home>Remove columns>Remove Other columns来创建第一个表;但要生成第二个表,需要使用公式栏

在创建第二个表之前,我在第一个表(ID表)中添加了一列,每行中都有数字1。我调用了列ID匹配键

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

然后我在第二个表(KIID表)中添加了一个每行数字为1的列。我把那个列称为kiid匹配键

然后,我对刚才制作的两个表进行了一次完整的外部合并——ID和kiid表。为此,我首先使用Home>Merge查询并将Table1自身合并。(匹配使用哪一列并不重要,因为这是临时的。)我选择Full Outer作为连接类型。合并完成后,我在公式栏中编辑了合并,将表更改为#“Added ID Match Key”和#“Added KI ID Match Key”(这恰好是我创建这两个“表”并添加其匹配键以用于此合并后的名称),并将相应的匹配字段更改为“ID Match Key”和“KI ID Match Key”:

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

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

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

然后我添加了一列,并确定文本中的单词在KI文本中。我用List.Intersect做的。它列出了重复项,这是我想要的:

然后我添加了一列,并做了相反的操作——确定文本中的单词。同样,我使用了List.Intersect:

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

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

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

    一定要检查我的数学

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

在你最初的三条评论之后,我又加了一条
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"