简体   繁体   中英

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

I have an excel list, and one of the columns has all the information I need. Unfortunately this information needs to be split up into several columns.

Example of how it is right now:

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

Example of how it needs to be:

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

I have had limited success with the FIND funciton in Excel. Unfortunately the numeric values do not always have the same length. So sometimes I would get 5 instead of 50 as a result, which would be wrong.

The values of interest are not always in the same order. The goal is to search for several specific strings and return the value after it to a column.

Here is a Power Query method that makes the following assumptions:

  • The description for the output will be the second word after colour
  • The other descriptors will be followed by a single word with the value of the descriptor
  • The descriptors do not have to be in any particular order.
  • There is only a single space between words.

To create this:

  • Select a cell in your two column data table
  • Data => Get&Transform => From Table/Range
  • In the PQ UI, navigate to Home => Advanced Editor
  • Make note of the Table name in Line 2
  • Paste the M-Code below to replace the existing M-Code
  • Examine the inline comments, as well as the Applied Steps panel in PQ to understand what is going on
    • In summary, we
      • create a list of the desired descriptors
      • create another list of the words in description column from the source data
      • Finding the position of each descriptor, we return the adjacent word (adjacent second word to find the description
      • Combine the results to create our output table.

M Code

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"

Source

在此处输入图像描述

Results

在此处输入图像描述

And if your Source data changes, merely Refresh the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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