簡體   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