Transpose Data in Columns to Rows

DFNJ

New Member
Joined
Jul 7, 2023
Messages
8
Office Version
  1. 2021
Platform
  1. MacOS
I'm trying to move data from certains columns to rows. I've tried unpivot, but it doesn't seem to keep certain data points together. This is my PQ.
View attachment 103352

Here's the new format I need to create. Can you please advise? Thank you!
Screenshot 2023-12-11 at 5.15.57 PM.png
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
cannot manipulate data in a picture. Suggest you reload using XL2BB and then show us also, what you wish the end result to look like. If that was in your attachment, then it was unreadable.
 
Upvote 0
cannot manipulate data in a picture. Suggest you reload using XL2BB and then show us also, what you wish the end result to look like. If that was in your attachment, then it was unreadable.
I downloaded XL2BB. I thought I had uploaded the query table in the attachment. The image is the new format that it needs to be in. I'll try to reload the image in XL2BB.
SSNFirst NameLast NameBirthdateGenderRelationship CodeAddress 1Address 2CityStateZipSPOUSE First NameSPOUSE Last NameSPOUSE BirthdateSPOUSE GenderSPOUSE RelationshipDEP1. First NameDEP1. Last NameDEP1. BirthdateDEP1. GenderDEP1. RelationshipDEP2. First NameDEP2. Last NameDEP2. BirthdateDEP2. GenderDEP2. RelationshipDEP3. First NameDEP3. Last NameDEP3. BirthdateDEP3. GenderDEP3. RelationshipPlanElection Effective date
111-11-1111MARIABROWN9/28/67FSUB1 MAIN STUnit 103ChicagoIL60614KELLYBROWN7/28/05FDEPMatthewBROWN9/17/07MDEPHIGH PLAN1/1/24
222-22-2222LISASMITH7/23/66FSUB2 TREE LNFALLS CHURCHVA22046JamesSMITH12/24/62MSPSHIGH PLAN1/1/24
333-33-3333ANNFORD6/20/81FSUB3 BROOK DRAPT 1SKOKIEIL60077ANDREWFORD12/14/76MSPSJAKEDAVIS2/7/06MDEPJohnWHITE2/3/12MDEPPeterWHITE4/13/13MDEPLOW PLAN1/1/24
 
Upvote 0
This is the new format I'm trying to achieve.
Reformat_New.xlsx
ABCDEFGHIJKLM
1Reformat
2SUB SSNFirst NameLast NameBirthdateGenderRelationship CodeAddress 1Address 2CityStateZipPlanElection Effective date
3361-70-1988MARIABROWN9/28/67FSUB1 MAIN STUnit 103ChicagoIL60614HIGH PLAN1/1/24
4361-70-1988KELLYBROWN7/28/05FDEP1 MAIN STUnit 103ChicagoIL60614HIGH PLAN1/1/24
5361-70-1988MatthewBROWN9/17/07MDEP1 MAIN STUnit 103ChicagoIL60614HIGH PLAN1/1/24
6213-92-7188LISASMITH7/23/66FSUB2 TREE LNFALLS CHURCHVA22046HIGH PLAN1/1/24
7213-92-7188JamesSMITH12/24/62MSPS2 TREE LNFALLS CHURCHVA22046HIGH PLAN1/1/24
8319-08-9364ANNFORD6/20/81FSUB3 BROOK DRAPT 1SKOKIEIL60077LOW PLAN1/1/24
9319-08-9364ANDREWFORD12/14/76MSPS3 BROOK DRAPT 1SKOKIEIL60077LOW PLAN1/1/24
10319-08-9364JAKEDAVIS2/7/06MDEP3 BROOK DRAPT 1SKOKIEIL60077LOW PLAN1/1/24
11319-08-9364JohnWHITE2/3/12MDEP3 BROOK DRAPT 1SKOKIEIL60077LOW PLAN1/1/24
12319-08-9364PeterWHITE4/13/13MDEP3 BROOK DRAPT 1SKOKIEIL60077LOW PLAN1/1/24
Enr
 
Upvote 0
try

Power Query:
let
    tbl = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Source = Table.RenameColumns(tbl, {"Relationship Code", "Relationship"}),
    tcn = Table.ColumnNames(Source),
    headers = List.Range(tcn, 1, 5),
    rel = {"", "SPOUSE ", "DEP1. ", "DEP2. ", "DEP3. "},
    commoncols =  List.Range(tcn, 6,5) & List.LastN(tcn,2), 
    lstheaders = List.Accumulate(rel, {}, (s,c)=> s & {{"SSN"} & List.Transform(headers, each c & _) & commoncols}),
    lsttables = List.Transform(lstheaders, each Table.SelectColumns(Source, _)),
    tcn1 = Table.ColumnNames(lsttables{0}),
    lsttables1 = List.Transform(lsttables, each Table.RenameColumns(_, List.Zip({Table.ColumnNames(_), tcn1}))),
    listtables2 = List.Zip({lsttables1, {1..List.Count(lsttables1)}}),
    listtables3 = List.Transform(listtables2, each Table.AddColumn(_{0}, "Order", (x)=> _{1})),
    tbl1 = Table.Combine(listtables3),
    tbl2 = Table.SelectRows(tbl1, each ([Last Name] <> null)),
    tbl3 = Table.Sort(tbl2,{{"SSN", Order.Ascending}, {"Order", Order.Ascending}}),
    Result = Table.RenameColumns(Table.RemoveColumns(tbl3,{"Order"}),{"Relationship", "Relationship Code"})
in
    Result
 
Upvote 0
try

Power Query:
let
    tbl = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Source = Table.RenameColumns(tbl, {"Relationship Code", "Relationship"}),
    tcn = Table.ColumnNames(Source),
    headers = List.Range(tcn, 1, 5),
    rel = {"", "SPOUSE ", "DEP1. ", "DEP2. ", "DEP3. "},
    commoncols =  List.Range(tcn, 6,5) & List.LastN(tcn,2),
    lstheaders = List.Accumulate(rel, {}, (s,c)=> s & {{"SSN"} & List.Transform(headers, each c & _) & commoncols}),
    lsttables = List.Transform(lstheaders, each Table.SelectColumns(Source, _)),
    tcn1 = Table.ColumnNames(lsttables{0}),
    lsttables1 = List.Transform(lsttables, each Table.RenameColumns(_, List.Zip({Table.ColumnNames(_), tcn1}))),
    listtables2 = List.Zip({lsttables1, {1..List.Count(lsttables1)}}),
    listtables3 = List.Transform(listtables2, each Table.AddColumn(_{0}, "Order", (x)=> _{1})),
    tbl1 = Table.Combine(listtables3),
    tbl2 = Table.SelectRows(tbl1, each ([Last Name] <> null)),
    tbl3 = Table.Sort(tbl2,{{"SSN", Order.Ascending}, {"Order", Order.Ascending}}),
    Result = Table.RenameColumns(Table.RemoveColumns(tbl3,{"Order"}),{"Relationship", "Relationship Code"})
in
    Result
Thank you. I copied the code as a new step, but I received an error.
try

Power Query:
let
    tbl = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Source = Table.RenameColumns(tbl, {"Relationship Code", "Relationship"}),
    tcn = Table.ColumnNames(Source),
    headers = List.Range(tcn, 1, 5),
    rel = {"", "SPOUSE ", "DEP1. ", "DEP2. ", "DEP3. "},
    commoncols =  List.Range(tcn, 6,5) & List.LastN(tcn,2),
    lstheaders = List.Accumulate(rel, {}, (s,c)=> s & {{"SSN"} & List.Transform(headers, each c & _) & commoncols}),
    lsttables = List.Transform(lstheaders, each Table.SelectColumns(Source, _)),
    tcn1 = Table.ColumnNames(lsttables{0}),
    lsttables1 = List.Transform(lsttables, each Table.RenameColumns(_, List.Zip({Table.ColumnNames(_), tcn1}))),
    listtables2 = List.Zip({lsttables1, {1..List.Count(lsttables1)}}),
    listtables3 = List.Transform(listtables2, each Table.AddColumn(_{0}, "Order", (x)=> _{1})),
    tbl1 = Table.Combine(listtables3),
    tbl2 = Table.SelectRows(tbl1, each ([Last Name] <> null)),
    tbl3 = Table.Sort(tbl2,{{"SSN", Order.Ascending}, {"Order", Order.Ascending}}),
    Result = Table.RenameColumns(Table.RemoveColumns(tbl3,{"Order"}),{"Relationship", "Relationship Code"})
in
    Result
Thank you. I received this error message. Do I need to rename "Table1?" Where do I find this?
Screenshot 2024-01-10 160306.png
 
Upvote 0
Create a blank query, go to the advanced editor, delete the blank query code, and paste the code I wrote.

The first step in the query is to retrieve the starting table. So if your starting table is in excel and has the name Table1, then it will be retrieved in the first step. If your table is named something else then change "Table1" in the first step to your table name.
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,459
Members
452,644
Latest member
gjcase

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