Sort Rows - Keep Distinct Values

Mcook13

New Member
Joined
May 14, 2019
Messages
43
Calling all Query experts,

I'm sure this is a simple fix, but I am looking to sort a row by their Line # in the 2nd column, but maintain the original order in the 1st column.

Example below. Input is how my data on the table comes in and Output is what I am trying to achieve. Any help is appreciated.

Input
Col 1 - Col 2
def - 3
def - 1
def - 2
abc - 2
abc - 1
ghi - 1

Output
Col 1 - Col 2
def - 1
def - 2
def - 3
abc - 1
abc - 2
ghi - 1
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    GroupRows = Table.Group(Source, {"Column1"}, {{"Column2", each _, type table [Column1=text, Column2=number]}}),
    ttc = Table.TransformColumns(GroupRows, {"Column2", (x) => List.Sort(x[Column2])}),
    Expand = Table.ExpandListColumn(ttc, "Column2")
in
    Expand

Book2
ABCDEF
1Column1Column2Column1Column2
2def3def1
3def1def2
4def2def3
5abc2abc1
6abc1abc2
7ghi1ghi1
8
Sheet3
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    GroupRows = Table.Group(Source, {"Column1"}, {{"Column2", each _, type table [Column1=text, Column2=number]}}),
    ttc = Table.TransformColumns(GroupRows, {"Column2", (x) => List.Sort(x[Column2])}),
    Expand = Table.ExpandListColumn(ttc, "Column2")
in
    Expand

Book2
ABCDEF
1Column1Column2Column1Column2
2def3def1
3def1def2
4def2def3
5abc2abc1
6abc1abc2
7ghi1ghi1
8
Sheet3
Hi JGorden11,

It almost worked. I had not included the fact that there are columns after column 2. Those columns have distinct values.
If it helps to understand, Col 1 = PO number, Col 2 = Line #, Col 3 = Ship to address, Col 4 = Qty ordered

Input
Col 1 - Col 2 - Col 3 - Col 4
def - 3 - xyz - 200
def - 1 - xyz - 50
def - 2 - xyz - 10
abc - 2 - qrs - 35
abc - 1 - qrs - 40
ghi - 1 - lmn - 12

Output
Col 1 - Col 2 - Col 3 - Col 4
def - 1- xyz - 50
def - 2- xyz - 10
def - 3- xyz - 200
abc - 1- qrs - 40
abc - 2- qrs - 35
ghi - 1 - lmn - 12
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    tcn = Table.ColumnNames(Source),
    tcn2 = List.Skip(tcn,1),
    GroupRows = Table.Group(Source, {tcn{0}}, {{"Grouped", each _}}),
    AddIndex = Table.AddIndexColumn(GroupRows, "Index", 0, 1, Int64.Type),
    ExpandCount = Table.ExpandTableColumn(AddIndex, "Grouped", tcn2, tcn2),
    Result = Table.SelectColumns(Table.Sort(ExpandCount,{{"Index", Order.Ascending}, {tcn{1}, Order.Ascending}}),tcn)
in
    Result

delme1R1.xlsm
ABCDEFGHIJ
1Column1Column2Column3Column4Column1Column2Column3Column4
2def3xyz200def1xyz50
3def1xyz50def2xyz10
4def2xyz10def3xyz200
5abc2qrs35abc1qrs40
6abc1qrs40abc2qrs35
7ghi1lmn12ghi1lmn12
8
Sheet8
 
Upvote 0
Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    tcn = Table.ColumnNames(Source),
    tcn2 = List.Skip(tcn,1),
    GroupRows = Table.Group(Source, {tcn{0}}, {{"Grouped", each _}}),
    AddIndex = Table.AddIndexColumn(GroupRows, "Index", 0, 1, Int64.Type),
    ExpandCount = Table.ExpandTableColumn(AddIndex, "Grouped", tcn2, tcn2),
    Result = Table.SelectColumns(Table.Sort(ExpandCount,{{"Index", Order.Ascending}, {tcn{1}, Order.Ascending}}),tcn)
in
    Result

delme1R1.xlsm
ABCDEFGHIJ
1Column1Column2Column3Column4Column1Column2Column3Column4
2def3xyz200def1xyz50
3def1xyz50def2xyz10
4def2xyz10def3xyz200
5abc2qrs35abc1qrs40
6abc1qrs40abc2qrs35
7ghi1lmn12ghi1lmn12
8
Sheet8
Groovy baby! It worked! Thank you very much for your help
 
Upvote 0

Forum statistics

Threads
1,223,698
Messages
6,173,897
Members
452,536
Latest member
Chiz511

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