繁体   English   中英

在单元格中搜索特定字符串,然后只返回字符串后面的特定值

[英]Searching for a specific string in a cell, and then returning only specific values after the string

我有一个 excel 列表,其中一列包含我需要的所有信息。 不幸的是,这些信息需要分成几列。

现在的例子:

article number; description
12345; apple random strings colour red random strings size medium random strings random strings weight 50g 
random strings

它需要如何的示例:

article number; description; size; colour; weight
12345; apple; medium; red; 50g

我在 Excel 中的 FIND 功能取得了有限的成功。 不幸的是,数值并不总是具有相同的长度。 所以有时我会得到 5 而不是 50 结果,这是错误的。

感兴趣的值并不总是以相同的顺序排列。 目标是搜索几个特定的字符串并将其后面的值返回到一列。

这是一种 Power Query 方法,它做出以下假设:

  • output 的description将是colour之后的第二个字
  • 其他描述符后跟一个带有描述符值的单词
  • 描述符不必按任何特定顺序排列。
  • 单词之间只有一个空格。

要创建这个:

  • Select 你的两列数据表中的一个单元格
  • Data => Get&Transform => From Table/Range
  • 在 PQ UI 中,导航到Home => Advanced Editor
  • 记下第 2 行中的表名
  • 粘贴下面的 M-Code 以替换现有的 M-Code
  • 检查内联注释以及 PQ 中的 Applied Steps 面板以了解发生了什么
    • 总之,我们
      • 创建所需描述符的列表
      • 从源数据创建另一个description列中的单词列表
      • 找到每个描述符的position,我们返回相邻的字(相邻的第二个字找到description
      • 结合结果创建我们的 output 表。

M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],

//Valid column headers in desired order
colHdrs = {"colour","size", "weight"},


    #"Changed Type" = Table.TransformColumnTypes(Source,{{"article number", Int64.Type}, {"description", type text}}),

//Split the description cell into a list by <space>
    #"Added Custom" = Table.AddColumn(#"Changed Type", "splitDescription", each Text.Split([description]," ")),

//Match each descriptor (colHdrs) with the following word in the split descriptor list
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Descriptions", each 
        List.Accumulate(colHdrs,

//Note that `Description` is matched with the second word following "colour"
            {[splitDescription]{List.PositionOf([splitDescription], "colour")+2}},
            (state,current)=> 
                if List.PositionOf([splitDescription],current) >= 0
                    then List.Combine({state,{[splitDescription]{List.PositionOf([splitDescription], current)+1}}})
                else List.Combine({state,{null}}))),

//create a column of table records
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "descriptorRecords", each Record.FromList([Descriptions], List.Combine({{"description"},colHdrs}))),

//clean up and expand the Records column into a Table
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"description", "splitDescription", "Descriptions"}),
    #"Expanded descriptorRecords" = Table.ExpandRecordColumn(#"Removed Columns", "descriptorRecords", {"description", "colour", "size", "weight"}, {"description", "colour", "size", "weight"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded descriptorRecords",{{"description", type text}, {"colour", type text}, {"size", type text}, {"weight", type text}})
in
    #"Changed Type1"

资源

在此处输入图像描述

结果

在此处输入图像描述

如果您的源数据发生变化,只需刷新查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM