Data Cleaning: All data in rows > Convert to a proper table

goko

New Member
Joined
Jan 3, 2022
Messages
12
Office Version
  1. 2021
Platform
  1. Windows
Hi,
I want to solve this in Power Query

I have the following data.

Book1
D
8Sr. no.
9Product
10Qty
11Quality
121
13apple
1499
15average
162
17oranges
1842
19good
203
21banana
2210
23low
Sheet1


I want to transform this into a proper table

Book1
GHIJ
7Sr. no.ProductQtyQuality
81apple99average
92oranges42good
103banana10low
Sheet1


Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Do you have a header in D7?
 
Upvote 0
Do you have a header in D7?
No.

D8 till D11 are the headers and the subsequent data is in rows.

I tried to play around with index columns and adding +4 to index but that got me nowhere
 
Upvote 0
Assuming D8 onwards is a table called Table1, you could use:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Demoted Headers" = Table.DemoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.RoundDown(([Index]-1)/4)+1),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Number.Mod([Index],4)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Custom.1", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Custom.1", type text}}, "en-US")[Custom.1]), "Custom.1", "Column1"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Custom"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns1", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Sr. no.", Int64.Type}, {"Product", type text}, {"Qty", Int64.Type}, {"Quality", type text}})
in
    #"Changed Type1"
 
Upvote 0
Solution
Assuming D8 onwards is a table called Table1, you could use:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Demoted Headers" = Table.DemoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.RoundDown(([Index]-1)/4)+1),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Number.Mod([Index],4)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Custom.1", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Custom.1", type text}}, "en-US")[Custom.1]), "Custom.1", "Column1"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Custom"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns1", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Sr. no.", Int64.Type}, {"Product", type text}, {"Qty", Int64.Type}, {"Quality", type text}})
in
    #"Changed Type1"
Thank you very much
 
Upvote 0
Another way:

Power Query:
let
    lst = Excel.CurrentWorkbook(){[Name="Table1"]}[Content][Column1],
    ColCt = 4,
    Headers = List.FirstN(lst,ColCt),
    Rows = List.Generate(()=>ColCt, (x)=> x < List.Count(lst), (x)=>x+ColCt, (x)=> List.Range(lst,x,ColCt)),
    Result= Table.FromRows(Rows,Headers)
in
    Result

Book1
ABCDEFG
1Column1Sr. no.ProductQtyQuality
2Sr. no.1apple99average
3Product2oranges42good
4Qty3banana10low
5Quality
61
7apple
899
9average
102
11oranges
1242
13good
143
15banana
1610
17low
18
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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