Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
Kagliostro
Frequent Visitor

Sort text in a single cell

Hi everyone,
I am would like to sort out text in a single cell and to do so I found in this forum a script in M which would fit perfectly with my need (link). However, while the majority of cells would have comma separated values, there are some with a single value. Cells with single values generates an error that I could circumvent with an if, but I am struggling to achieve it

 

 

let
    Split = Text.Split([NAME_ASSIGNED], ","),
    Trim = List.Transform(Split, each Text.Trim(_)),
    Sort = List.Sort(Trim),
    Combine = Text.Combine(Sort,",")
in
if List.Count([NAME_ASSIGNED],",") > 1 then Combine
    else [NAME_ASSIGNED]

 

 

 

1 ACCEPTED SOLUTION
Ahmedx
Super User
Super User

There shouldn't be an error, but try this code anyway

=[
  f = (x) => Text.Combine(List.Sort(List.Transform(Text.Split(x, ","), Text.Trim)), ","),
  Split = try f([NAME_ASSIGNED]) otherwise [NAME_ASSIGNED]
][Split]

 

Screenshot_1.png

View solution in original post

4 REPLIES 4
ronrsnfld
Super User
Super User

If your NAME_ASSIGNED column contains various text strings, I'm surprised your code works at all. Your last line reads:

if List.Count([NAME_ASSIGNED],",") > 1 then Combine
    else [NAME_ASSIGNED]

 

which should always result in an error if NAME_ASSIGNED contains text strings.

 

What is actually in that column, and what is your error message.

Ahmedx
Super User
Super User

There shouldn't be an error, but try this code anyway

=[
  f = (x) => Text.Combine(List.Sort(List.Transform(Text.Split(x, ","), Text.Trim)), ","),
  Split = try f([NAME_ASSIGNED]) otherwise [NAME_ASSIGNED]
][Split]

 

Screenshot_1.png

Omid_Motamedise
Super User
Super User

Rewrite the first step by the below formula

 

Split = if Text.Contains([NAME_ASSIGNED],",") then Text.Split([NAME_ASSIGNED], ",") else {[NAME_ASSIGNED]},

If my answer helped solve your issue, please consider marking it as the accepted solution.
lbendlin
Super User
Super User

There should not be an error, and there is no need to treat a single item list differently.

 

 

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45W8s9L1VEIKc/XUVBQcMssS1WK1YlWCskoSgWyYgE=", BinaryEncoding.Base64), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [NAME_ASSIGNED = _t]
  ), 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Custom", 
    each 
      let
        Split   = Text.Split([NAME_ASSIGNED], ","), 
        Trim    = List.Transform(Split, each Text.Trim(_)), 
        Sort    = List.Sort(Trim), 
        Combine = Text.Combine(Sort, ",")
      in
        Combine
  )
in
  #"Added Custom"

 

 

What's the error message?

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.