[英]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.