cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sherville Occasional Visitor
Occasional Visitor

Sort number within a cell separated by comma

Hi, guys! I just wanna know if there is any way to sort a value within a cell that's separated by comma

 

Like for example, my column has a value of 4,5,2,3,7,8,6,1

 

is there any way I could sort this and make it as 1,2,3,4,5,6,7,8 or the other way around like 8,7,6,5,4,3,2,1?

 

Thank you! I hope anyone could help me with this.

2 ACCEPTED SOLUTIONS

Accepted Solutions
Super User
Super User

Re: Sort number within a cell separated by comma

You could do it in Query Editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtEx1THSMdYx17HQMdMxVIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.Sort(List.RemoveItems(Text.ToList([Column1]),{","}))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text})
in
    #"Extracted Values"

To reverse sort, use the optional second parameter for your List.Sort " , Order.Descending"


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

View solution in original post

Community Support Team
Community Support Team

Re: Sort number within a cell separated by comma

Hi @sherville,

 

For your scenario, I think you can add a custom column with 'Text.Split', 'List.Sort', 'Text.Combine' to achieve your requirement.

 

Sample:

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Sort DESC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Descending),","))
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Sort ASC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Ascending),","))

7.PNG

 

Full Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQx1THSMdMx17HQMVCK1YlWstQx0TEG8sDiSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Sort DESC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Descending),",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Sort ASC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Ascending),","))
in
    #"Added Custom1"

 

Function Description
List.Sort Returns a sorted list using comparison criterion.
Text.Combine Returns a text value that is the result of joining all text values with each value separated by a separator.
Text.Split Returns a list containing parts of a text value that are delimited by a separator text value.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin Sheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.



For learning resources/Release notes, please visit: | |

View solution in original post

2 REPLIES 2
Super User
Super User

Re: Sort number within a cell separated by comma

You could do it in Query Editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtEx1THSMdYx17HQMdMxVIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.Sort(List.RemoveItems(Text.ToList([Column1]),{","}))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text})
in
    #"Extracted Values"

To reverse sort, use the optional second parameter for your List.Sort " , Order.Descending"


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

View solution in original post

Community Support Team
Community Support Team

Re: Sort number within a cell separated by comma

Hi @sherville,

 

For your scenario, I think you can add a custom column with 'Text.Split', 'List.Sort', 'Text.Combine' to achieve your requirement.

 

Sample:

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Sort DESC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Descending),","))
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Sort ASC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Ascending),","))

7.PNG

 

Full Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQx1THSMdMx17HQMVCK1YlWstQx0TEG8sDiSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Sort DESC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Descending),",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Sort ASC", each Text.Combine(List.Sort(Text.Split([Value],","),Order.Ascending),","))
in
    #"Added Custom1"

 

Function Description
List.Sort Returns a sorted list using comparison criterion.
Text.Combine Returns a text value that is the result of joining all text values with each value separated by a separator.
Text.Split Returns a list containing parts of a text value that are delimited by a separator text value.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin Sheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.



For learning resources/Release notes, please visit: | |

View solution in original post

Helpful resources

Announcements
October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 415 members 3,509 guests
Please welcome our newest community members: