Power Query: CSV file with no headers has headers and values in alternate columns

raskolnikov

New Member
Joined
Dec 10, 2013
Messages
17
I have a CSV resembling the following from our payroll software

Gross Wages650Employers NI100Employers Pension30Week Number1
Gross Wages650Employers NI100Employers Pension30Week Number2
Gross Wages650Employers NI100Employers Pension30Week Number3
Gross Wages650Employers NI100Employers Pension30Week Number4
Gross Wages650Employers NI100Employers Pension30Week Number5
Gross Wages650Employers NI100Employers Pension30Week Number6
Gross Wages650Employers NI100Employers Pension30Week Number7
Gross Wages650Employers NI100Employers Pension30Week Number8
Gross Wages650Employers NI100Employers Pension30Week Number9
Gross Wages650Employers NI100Employers Pension30Week Number10
Gross Wages650Employers NI100Employers Pension30Week Number11
Gross Wages650Employers NI100Employers Pension30Week Number12

As you can see what should be Headers are in columns to the left of their values. I would like to produce a table that looks like this:

Gross WagesEmployers NIEmployers PensionWeek Number
650​
100​
30​
1​
650​
100​
30​
2​
650​
100​
30​
3​
650​
100​
30​
4​
650​
100​
30​
5​
650​
100​
30​
6​
650​
100​
30​
7​
650​
100​
30​
8​
650​
100​
30​
9​
650​
100​
30​
10​
650​
100​
30​
11​
650​
100​
30​
12​

I've tried pivoting, unpivoting, grouping... even ChatGPT couldn't crack it :cautious: Any help would be appreciated.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Well, I am no expert but I was able to do this. I brought the data into PowerQuery. There I renamed the relevant columns and removed the others.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}, {"Column3", type text}, {"Column4", Int64.Type}, {"Column5", type text}, {"Column6", Int64.Type}, {"Column7", type text}, {"Column8", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column2", "Gross Wages"}, {"Column4", "Employers NI"}, {"Column6", "Employers Pension"}, {"Column8", "Week Number"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Column1", "Column3", "Column5", "Column7"})
in
    #"Removed Columns"
 
Upvote 0
Thanks for your input. Thats what I ended up doing yesterday but it would be better to be able to automatically change headers as the actual data set is quite large with many more 'headers' so depending on what I need to analyse, changing every column name manually would be time consuming.
 
Upvote 0
I have no doubt there are others here that could do this more elegantly, and there is one thing that may create a problem which I'm sure someone could fix.
My solution requires four Queries - one to get the Old Column Names (Column1, etc.), the new Column Names (Gross Wages, etc.), one to merge those tables into a list of lists which is used to rename the columns in the fourth Report query. Here goes.
Original Table:
Headers.xlsx
ABCDEFGH
1Column1Column2Column3Column4Column5Column6Column7Column8
2Gross Wages650Employers NI100Employers Pension30Week Number1
3Gross Wages650Employers NI100Employers Pension30Week Number2
4Gross Wages650Employers NI100Employers Pension30Week Number3
5Gross Wages650Employers NI100Employers Pension30Week Number4
6Gross Wages650Employers NI100Employers Pension30Week Number5
7Gross Wages650Employers NI100Employers Pension30Week Number6
8Gross Wages650Employers NI100Employers Pension30Week Number7
9Gross Wages650Employers NI100Employers Pension30Week Number8
10Gross Wages650Employers NI100Employers Pension30Week Number9
11Gross Wages650Employers NI100Employers Pension30Week Number10
12Gross Wages650Employers NI100Employers Pension30Week Number11
13Gross Wages650Employers NI100Employers Pension30Week Number12
Sheet1
OldHeaders:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    UnpivotedOtherColumns = Table.UnpivotOtherColumns(Source, {"Column2", "Column4", "Column6", "Column8"}, "Attribute", "Value"),
    RemovedOtherColumns = Table.RemoveColumns(UnpivotedOtherColumns,{"Attribute", "Value"}),
    AddedOldHeaders = Table.AddColumn(RemovedOtherColumns, "OldHeaders", each Table.ColumnNames( RemovedOtherColumns )),
    RemovedOtherColumns1 = Table.SelectColumns(AddedOldHeaders,{"OldHeaders"}),
    KeptFirstRows = Table.FirstN(RemovedOtherColumns1,1),
    ExpandedOldHeaders = Table.ExpandListColumn(KeptFirstRows, "OldHeaders")
in
    ExpandedOldHeaders
NewHeaders:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    UnpivotedOtherColumns = Table.UnpivotOtherColumns(Source, {"Column1", "Column3", "Column5", "Column7"}, "Attribute", "Value"),
    RemovedColumns = Table.RemoveColumns(UnpivotedOtherColumns,{"Attribute", "Value"}),
    PromotedHeaders = Table.PromoteHeaders(RemovedColumns, [PromoteAllScalars=true]),
    AddedHeadersList = Table.AddColumn(PromotedHeaders, "Headers", each Table.ColumnNames(PromotedHeaders)),
    RemovedOtherColumns = Table.SelectColumns(AddedHeadersList,{"Headers"}),
    KeptFirstRows = Table.FirstN(RemovedOtherColumns,1),
    ExpandedHeaders = Table.ExpandListColumn(KeptFirstRows, "Headers")
in
    ExpandedHeaders
Headers:
Power Query:
let
    Source = OldHeaders,
    AddedIndex = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    AddedCustom = Table.AddColumn(AddedIndex, "New Headers", each NewHeaders{[Index]}),
    ExpandedNewHeaders = Table.ExpandRecordColumn(AddedCustom, "New Headers", {"Headers"}, {"Headers"}),
    RemovedColumns = Table.RemoveColumns(ExpandedNewHeaders,{"Index"}),
    TransposedTable = Table.Transpose(RemovedColumns),
    Custom1 = Table.ToColumns( TransposedTable )
in
    Custom1
FinalReport:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    RemovedOtherColumns = Table.SelectColumns(Source,OldHeaders[OldHeaders]),
    Custom1 = Table.RenameColumns( RemovedOtherColumns, Headers ),
    ChangedType = Table.TransformColumnTypes(Custom1,{{"Gross Wages", Int64.Type}, {"Employers NI", Int64.Type}, {"Employers Pension", Int64.Type}, {"Week Number", Int64.Type}})
in
    ChangedType
Headers.xlsx
JKLM
1Gross WagesEmployers NIEmployers PensionWeek Number
2650100301
3650100302
4650100303
5650100304
6650100305
7650100306
8650100307
9650100308
10650100309
116501003010
126501003011
136501003012
Sheet1

The problem here is if/when more columns are added to the source data. However I'm sure someone here can come up with code that can determine the number of columns in the original data and generate a list of odd and even column names to replace the hard coded "ColumnX" references in the code above. It took me a while to figure this much out, even with the help of this video! But it's a start...
 
Upvote 0
try

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    headers = List.Alternate(Record.ToList(Source{0}), 1, 1, 1),
    deleteCols = List.Alternate(Table.ColumnNames(Source), 1, 1, 1),
    tbl = Table.RemoveColumns(Source, deleteCols),
    Result = Table.RenameColumns(tbl, List.Zip({Table.ColumnNames(tbl), headers}))
in
    Result
 
Upvote 1
try

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    headers = List.Alternate(Record.ToList(Source{0}), 1, 1, 1),
    deleteCols = List.Alternate(Table.ColumnNames(Source), 1, 1, 1),
    tbl = Table.RemoveColumns(Source, deleteCols),
    Result = Table.RenameColumns(tbl, List.Zip({Table.ColumnNames(tbl), headers}))
in
    Result
Freak'n AMAZING! Thanks for coming to the rescue!
 
Upvote 0
A shorter (maybe faster) version

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    headers = List.Alternate(Record.ToList(Source{0}), 1, 1, 1),
    Data = List.Alternate(Table.ToColumns(Source), 1, 1, 0),
    Result = Table.FromColumns(Data, headers)
in
    Result
 
Upvote 1

Forum statistics

Threads
1,223,964
Messages
6,175,657
Members
452,664
Latest member
alpserbetli

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top