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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I got ChatGPT to explain how the code works which I thought might be nourishing to others on the board:

This is a block of M code written in the Power Query Editor of Microsoft Excel. It performs a series of transformations on a table named "Table1" and returns a new table as the output.

Here is a step-by-step breakdown of the code:

  1. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content] - This line defines a variable named Source and sets it equal to the content of a table named "Table1" in the current Excel workbook.
  2. headers = List.Alternate(Record.ToList(Source{0}), 1, 1, 1) - This line defines a variable named headers and sets it equal to a list of column headers for the Source table. The List.Alternate function is used to select every other item in the list starting from the second item.
  3. Data = List.Alternate(Table.ToColumns(Source), 1, 1, 0) - This line defines a variable named Data and sets it equal to a list of columns for the Source table. The Table.ToColumns function is used to convert the table to a list of columns, and the List.Alternate function is used to select every other item in the list starting from the first item.
  4. Result = Table.FromColumns(Data, headers) - This line defines a variable named Result and sets it equal to a new table created from the Data list and the headers list.
  5. in Result - This line specifies that the output of the block of code should be the Result table.
Overall, the code takes a table named "Table1", separates the headers and data into separate lists, selects every other item in each list, and then combines them into a new table with the headers as column names.
 
Upvote 0
It's close, but not 100%.

items 2 and 3 would be correct if changed as follows:

  1. headers = List.Alternate(Record.ToList(Source{0}), 1, 1, 1) - This line defines a variable named headers and sets it equal to a list derived from every other item in the first row of Source. The List.Alternate function is used to skip every other item in the list starting by skipping the second item (identified as 1 since M is zero based).
  2. Data = List.Alternate(Table.ToColumns(Source), 1, 1, 0) - This line defines a variable named Data and sets it equal to a list of columns for the Source table. The Table.ToColumns function is used to convert the table to a list of columns, and the List.Alternate function is used to skip every other item in the list starting by skipping the first item (identified as 0 since M is zero based).
 
Upvote 0

Forum statistics

Threads
1,223,367
Messages
6,171,669
Members
452,416
Latest member
johnog

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