[英]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 方法,它做出以下假设:
description
将是colour
之后的第二个字要创建这个:
Data => Get&Transform => From Table/Range
Home => Advanced Editor
description
列中的单词列表description
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.