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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

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

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

@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

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
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors