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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
smpa01
Super User
Super User

Sorting each column individually in ascending order

Hello experts,

 

Is it possbile to sort each column values indiaviaully in ascending orders?

 

For example, the source data is following

 

Column 1Column 2Column 3Column 4Column 5
12345
21111
33234
44452
55523

 

Is it possible to achieve the following result by using M and not DAX

 

Column 1Column 2Column 3Column 4Column 5
11111
22222
33333
44444
55555

 

Thank you in advance.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION

@smpa01

 

But if you have many columns (lets saymore than 50), a better approach would be to automate this using List.Generate
Please see attached file with both these ways

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiU6VYnWiwiCEKBokaQ9Uh1INETaA6IbpBsiBRUyjPFK4+NhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t, #"Column 2" = _t, #"Column 3" = _t, #"Column 4" = _t, #"Column 5" = _t]),
    CT = Table.TransformColumnTypes(Source,{{"Column 1", Int64.Type}, {"Column 2", Int64.Type}, {"Column 3", Int64.Type}, {"Column 4", Int64.Type}, {"Column 5", Int64.Type}}),
    DH = Table.DemoteHeaders(CT),
    MyColumnNames=Record.ToList(DH{0}),
    Final=Table.FromColumns(
                    List.Generate(()=>
                    [x=0,y=Table.Column(CT,MyColumnNames{x})],
                    each [x] < List.Count(MyColumnNames),
                    each [x=[x]+1,y=Table.Column(CT,MyColumnNames{x})],
                    each List.Sort([y],Order.Ascending)),
            
            MyColumnNames)
    
    

    
in
    Final

Regards
Zubair

Please try my custom visuals

View solution in original post

3 REPLIES 3
Zubair_Muhammad
Community Champion
Community Champion

@smpa01

 

One way could be to select individual columns as list >>then sort them and recombine them using Table.FromColumns

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiU6VYnWiwiCEKBokaQ9Uh1INETaA6IbpBsiBRUyjPFK4+NhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t, #"Column 2" = _t, #"Column 3" = _t, #"Column 4" = _t, #"Column 5" = _t]),
    CT = Table.TransformColumnTypes(Source,{{"Column 1", Int64.Type}, {"Column 2", Int64.Type}, {"Column 3", Int64.Type}, {"Column 4", Int64.Type}, {"Column 5", Int64.Type}}),
    Final=Table.FromColumns(
        {List.Sort(CT[Column 1],Order.Ascending),
         List.Sort(CT[Column 2],Order.Ascending),
         List.Sort(CT[Column 3],Order.Ascending),
         List.Sort(CT[Column 4],Order.Ascending),
         List.Sort(CT[Column 5],Order.Ascending)},
         
         {"Column 1","Column 2","Column 3","Column 4","Column 5"})
in
    Final

Regards
Zubair

Please try my custom visuals

@smpa01

 

But if you have many columns (lets saymore than 50), a better approach would be to automate this using List.Generate
Please see attached file with both these ways

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiU6VYnWiwiCEKBokaQ9Uh1INETaA6IbpBsiBRUyjPFK4+NhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t, #"Column 2" = _t, #"Column 3" = _t, #"Column 4" = _t, #"Column 5" = _t]),
    CT = Table.TransformColumnTypes(Source,{{"Column 1", Int64.Type}, {"Column 2", Int64.Type}, {"Column 3", Int64.Type}, {"Column 4", Int64.Type}, {"Column 5", Int64.Type}}),
    DH = Table.DemoteHeaders(CT),
    MyColumnNames=Record.ToList(DH{0}),
    Final=Table.FromColumns(
                    List.Generate(()=>
                    [x=0,y=Table.Column(CT,MyColumnNames{x})],
                    each [x] < List.Count(MyColumnNames),
                    each [x=[x]+1,y=Table.Column(CT,MyColumnNames{x})],
                    each List.Sort([y],Order.Ascending)),
            
            MyColumnNames)
    
    

    
in
    Final

Regards
Zubair

Please try my custom visuals

Thanks @Zubair_Muhammad for your help.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.