Forum

Notifications
Clear all

Excel Sort within a cell

6 Posts
3 Users
2 Reactions
317 Views
(@robbieg)
Posts: 39
Trusted Member
Topic starter
 

I have a data table with 4 columns. I want to join together the data in columns B, C and D and have the result in column E sorted in Alphabetical order. I can solve this with a Function, but I would prefer to solve via a formula.

I was thinking of using SORT with TEXTJOIN
I've tried =TEXTJOIN(", ",TRUE,SORT(B2:D2)) but that fails

 
Posted : 05/03/2025 5:03 am
Topic Tags
Alan Sidman
(@alansidman)
Posts: 220
Member Moderator
 

 

A Power Query Solution

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Region"}, "Attribute", "Value"),
#"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"Region", Order.Ascending}, {"Value", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Attribute"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 1, 1, Int64.Type),
#"Inserted Modulo" = Table.AddColumn(#"Added Index", "Modulo", each Number.Mod([Index], 3), type number),
#"Removed Columns1" = Table.RemoveColumns(#"Inserted Modulo",{"Index"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns1", {{"Modulo", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns1", {{"Modulo", type text}}, "en-US")[Modulo]), "Modulo", "Value"),
#"Merged Columns" = Table.CombineColumns(#"Pivoted Column",{"1", "2", "0"},Combiner.CombineTextByDelimiter(", ", QuoteStyle.None),"Merged")
in
#"Merged Columns"
 
Posted : 05/03/2025 6:33 am
Riny van Eekelen
(@riny)
Posts: 1185
Member Moderator
 

@robbieg You can use last optional argument of the SORT function to indicate that the data is in a row across columns.

image
 
Posted : 05/03/2025 4:32 pm
Robert Green reacted
(@robbieg)
Posts: 39
Trusted Member
Topic starter
 

@alansidman

Thank you Alan. And thank you for showing me Modulo for creating an 'index' for pivoting on. I will be able to use that again.

A followup please:

How do i cope if col3 is empty? I seem to get the wrong result

 

@riny

Thank you. I was using SORT incorrectly. Lesson learnt.

This post was modified 2 months ago by Robert Green
 
Posted : 05/03/2025 7:39 pm
Alan Sidman
(@alansidman)
Posts: 220
Member Moderator
 

If column 3 is blank, then the Mcode will sort on the remaining two columns.  See the example attached

 

 
Posted : 06/03/2025 4:20 am
Robert Green reacted
(@robbieg)
Posts: 39
Trusted Member
Topic starter
 

Thank you for the followup, Alan.

 
Posted : 06/03/2025 5:22 am
Share: