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:
description
for the output will be the second word after colour
To create this:
Data => Get&Transform => From Table/Range
Home => Advanced Editor
description
column from the source datadescription
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.