Forum Discussion
Excel formula function or macro? Data Migration for Horizontal Columns to Vertical Columns
Excel formula function or macro? Data Migration for Horizontal Columns to Vertical Columns
This must be a common problem?
I have old database exported to csv.
The Old columns are;
First Name Surname DOB 1 Address
The new database format - Columns
1 First name 1 Surname 1 DOB 1 1 Address 2 First name 2 Surname 2 DOB 3 First name 3 Surname 3 DOB 4 First name 4 Surname 4 DOB 5 First name 5 Surname 5 DOB
I have over 6500 Row of data to migration.
1 First name | 1 Surname | 1 DOB | 1 1 Adresss | 2 First name | 2 Surname | 2 DOB | 3 First name | 3 Surname | 3 DOB | 4 First name | 4 Surname | 4 DOB | 5 First name | 5 Surname | 5 DOB |
Columns from above formatted better
I need an Automated process to move the data? So would this be formula function or macro?
Here is the As Is format
First Name | Surname | DOB | 1 address |
Alan | Smith | 01/01/1959 | 1 Murton |
Gavin | Smith | 02/01/1959 | 1 Murton |
Justin | Smith | 03/01/1959 | 1 Murton |
Bill | Smith | 04/01/1959 | 1 Murton |
Kate | Smith | 05/01/1959 | 1 Murton |
Tony | Jones | 06/01/1959 | 2 Murton |
Lee | Jones | 07/01/1959 | 2 Murton |
Olivia | Jones | 08/01/1959 | 2 Murton |
Noah | King | 09/01/1959 | 3 Murton |
Oliver | SMITH | 10/01/1959 | 4 Murton |
William | JOHNSON | 11/01/1959 | 4 Murton |
Elijah | WILLIAMS | 12/01/1959 | 4 Murton |
James | JONES | 13/01/1959 | 4 Murton |
Benjamin | BROWN | 14/01/1959 | 4 Murton |
Lucas | DAVIS | 15/01/1959 | 7 Murton |
Mason | MILLER | 16/01/1959 | 7 Murton |
Ethan | WILSON | 17/01/1959 | 7 Murton |
Emma | MOORE | 18/01/1959 | 7 Murton |
Ava | TAYLOR | 19/01/1959 | 8 Murton |
Sophia | ANDERSON | 20/01/1959 | 8 Murton |
Isabella | THOMAS | 21/01/1959 | 8 Murton |
Charlotte | JACKSON | 22/01/1959 | 9 Murton |
Amelia | WHITE | 23/01/1959 | 9 Murton |
Mia | HARRIS | 24/01/1959 | 10 Murton |
Harper | MARTIN | 25/01/1959 | 10 Murton |
Evelyn | THOMPSON | 26/01/1959 | 10 Murton |
Example 1
Here is the TO BE Format
1 First name | 1 Surance | 1 DOB | 1 1 Adresss | 2 First name | 2 Surance | 2 DOB | 3 First name | 3 Surance | 3 DOB | 4 First name | 4 Surance | 4 DOB | 5 First name | 5 Surance | 5 DOB |
Alan | Smith | 01/01/1959 | 1 Murton | Gavin | Smith | 02/01/1959 | Justin | Smith | 03/01/1959 | Bill | Smith | 04/01/1959 | Kate | Smith | 05/01/1959 |
Mia | HARRIS | 24/01/1959 | 10 Murton | Harper | MARTIN | 25/01/1959 | Evelyn | THOMPSON | 26/01/1959 | ||||||
Tony | Jones | 06/01/1959 | 2 Murton | Lee | Jones | 07/01/1959 | Olivia | Jones | 08/01/1959 | ||||||
Noah | King | 09/01/1959 | 3 Murton | ||||||||||||
Oliver | SMITH | 10/01/1959 | 4 Murton | William | JOHNSON | 11/01/1959 | Elijah | WILLIAMS | 12/01/1959 | James | JONES | 13/01/1959 | Benjamin | BROWN | 14/01/1959 |
Lucas | DAVIS | 15/01/1959 | 7 Murton | Mason | MILLER | 16/01/1959 | Ethan | WILSON | 17/01/1959 | Emma | MOORE | 18/01/1959 | |||
Ava | TAYLOR | 19/01/1959 | 8 Murton | Sophia | ANDERSON | 20/01/1959 | Isabella | THOMAS | 21/01/1959 | ||||||
Charlotte | JACKSON | 22/01/1959 | 9 Murton | Amelia | WHITE | 23/01/1959 |
Example 2
I am currently just using the filter by 1 address to copy and paste the data.
Any thoughts or feedback would be greatly appreciated!
I have a sample excel file if needed.
10 Replies
- sashakorniakUKCopper Contributor
I have been using power query to find a solution
So far I found this works, however I not find my 65000 rows of data has more than 5 names per address. However I need to either add names 6+ to a new row or stop that a source etc.
here the code so far
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"First Name", type text}, {"Surance", type text}, {"DOB", type text}, {"1 address", type text}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"1 address"}, "Attribute", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"1 address"}, {{"Count", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Text.Combine([Count][Value],",##,")),
#"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter(",##,", QuoteStyle.Csv), {"Custom.1", "Custom.2", "Custom.3", "Custom.4", "Custom.5", "Custom.6", "Custom.7", "Custom.8", "Custom.9", "Custom.10", "Custom.11", "Custom.12", "Custom.13", "Custom.14", "Custom.15"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Custom.1", type text}, {"Custom.2", type text}, {"Custom.3", type datetime}, {"Custom.4", type text}, {"Custom.5", type text}, {"Custom.6", type datetime}, {"Custom.7", type text}, {"Custom.8", type text}, {"Custom.9", type datetime}, {"Custom.10", type text}, {"Custom.11", type text}, {"Custom.12", type datetime}, {"Custom.13", type text}, {"Custom.14", type text}, {"Custom.15", type datetime}}),
#"Removed Columns1" = Table.RemoveColumns(#"Changed Type1",{"Count"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns1",{{"Custom.3", type date}, {"Custom.6", type date}, {"Custom.9", type date}, {"Custom.12", type date}, {"Custom.15", type date}})
in
#"Changed Type2"
- SergeiBaklanDiamond Contributor
Same idea, but without hardcoding column names
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Grouped Rows" = Table.Group( Source, {"address"}, { {"Tables", each [ #"Removed Columns" = Table.RemoveColumns(_,{"address"}), #"Added Index" = Table.AddIndexColumn( #"Removed Columns", "Index", 1, 1, Int64.Type ), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns( #"Added Index", {"Index"}, "Attribute", "Value" ), #"Merged Columns" = Table.CombineColumns( Table.TransformColumnTypes( #"Unpivoted Other Columns", {{"Index", type text}}, "en-GB" ), {"Attribute", "Index"}, Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged" ), #"Transposed Table1" = Table.Transpose(#"Merged Columns"), #"Promoted Headers" = Table.PromoteHeaders( #"Transposed Table1", [PromoteAllScalars=true] ) ][#"Promoted Headers"] }, {"Count", each Table.RowCount(_), Int64.Type} } ), // Generate Column Names MaxRows = List.Max(#"Grouped Rows"[Count]), RemoveAddress = Table.RemoveColumns(Source,{"address"}), #"Kept First Rows" = Table.FirstN(RemoveAddress,1), #"Added Custom" = Table.AddColumn(#"Kept First Rows", "Index", each {1..MaxRows}), #"Expanded Index" = Table.ExpandListColumn(#"Added Custom", "Index"), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Index", {"Index"}, "Attribute", "Value"), GetTogether = Table.CombineColumns( Table.TransformColumnTypes( #"Unpivoted Other Columns", {{"Index", type text}}, "en-GB"),{"Attribute", "Index"}, Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"ColumnNames" ), ColumnNames = GetTogether[ColumnNames], #"Expanded Tables" = Table.ExpandTableColumn(#"Grouped Rows", "Tables", ColumnNames) in #"Expanded Tables"
- sashakorniakUKCopper Contributor
SergeiBaklan - I cannot download the file?
testing the code shortly.
Any thoughts on the issue with if an address has more than 5 names?
- adversiIron Contributor
VBA or Power Query would probably be the best approach. Can you share your workbook to further test the dataset?
- sashakorniakUKCopper Contributor
`Hey there,
Trust you are well and happy?
Thank you for helping with this. I have attached the example workbook.
🙂
- sashakorniakUKCopper Contributor