Reputation: 39
My company uses a spreadsheet to record manufacturing data, but they also use the spreadsheet to give the user instructions. Those instructions are in the column header for the data they need to record. I am trying to create a report from this spreadsheet for the Quality Engineer and I need to be able to pull a specific string from the column to use as a column name. Luckily, the string that I need is within parentheses and by demoting the headers and transposing, I was able to add a new column and extract text between delimiters. But, at this point, I do not know how to replace the original column names with the extracted one.
Upvotes: 0
Views: 749
Reputation: 60224
No need to transpose etc.
You can just make a list of the new column names and do a rename.
The below code assumes
let
Source = Excel.CurrentWorkbook(){[Name="Table10"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Date", type date}, {"Shop Order", Int64.Type}, {"Month", type text},
{"Day", Int64.Type}, {"Year", Int64.Type}, {"Shift", Int64.Type},
{"SCAN: New Antioxidant:#(lf)(P/N A14656-1)#(lf) Lot Number", type text},
{"Scan: #(lf)Resin (PN A14658-1#(lf))Lot Number", type text}}),
//rename columns
origColNames=Table.ColumnNames(#"Changed Type"),
//List.Transform modifies only those column names that have the parenthesized substring
newColNames = List.Transform(origColNames, each let x=Text.SplitAny(_,"()") in if List.Count(x)=1 then x{0} else x{1}),
renamedHdrs = Table.RenameColumns(#"Changed Type", List.Zip({origColNames,newColNames}))
in
renamedHdrs
Edit with new column header data
newColNames
line: newColNames = List.Transform(origColNames, each
let x=Text.SplitAny(_,"()")
in
if List.Last(x) = "" or List.Count(x) = 1
then _
else x{1}),
Algorithm
""
) or if there is only one element, then return the original header; otherwise return the 2nd element.Upvotes: 1