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
Anonymous
Not applicable

How can I adapt M code to do a double Sort?

Hi community, @edhans & @v-lili6-msft 

Recently @edhans gave me M code to get a table that i needed;
https://community.powerbi.com/t5/Desktop/How-can-I-create-this-table-or-matrix/m-p/1196273#M535699 

But now, i need a little bit more help to adapt M to sort first by Pallet Pcs, and then by Pallet Id. 

Just like the @v-lili6-msft helped me to adapt the measure;
https://community.powerbi.com/t5/Desktop/Double-Sort-or-Sort-into-another-previous-Sort/m-p/1200991

How can i do this?
To sort first by Pallet Pcs and then, by Pallet ID, to bring less pallets... But into the M code.
I hope you can help me again...

2 REPLIES 2
dax
Community Support
Community Support

Hi @Anonymous , 

If you want to sort in M code, you could just click corresponding columns(click Pallet Pcs first,  then click Pallet Id ) to set sort by.

773.PNG

772.PNG

But I think sort in M code will change when you put data in visual, so I suggest you could set sort in visual. If you want to set double sort, you could click corresponding column headers. You could refer to power-bi-desktop-march-2020-feature-summary(Multi-column sort for tables)  for details.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @dax,
The thing is that i need to sort previously to get the table, becuase it double sort will bring me less pallets,
Let me explain with an example...


Table A;

temRequestOnSite PcsOffSite PcsPcs to complete request
609048-109-1012020160100


Table B;

ItemPallet IDPallet Pcs
609048-109-10710
609048-109-101040
609048-109-101220
609048-109-101320
609048-109-10920
609048-109-101140
609048-109-10810

 

M will bring me the pallets 7, 8, 9, 10 and 11 because this pallets, in order to the sort by Pallet ID, sum 120 pcs to complete the 100 requested. Right?

But, If i sort first by Pallet Pcs, and then by Pallet ID in M, like the edited the Measure, (Something like the next table).

mPallet IDPallet Pcs
609048-109-101040
609048-109-101140
609048-109-10920
609048-109-101220
609048-109-101320
609048-109-10710
609048-109-10810


The result table return me only the Pallets 10, 11 & 9, this pallets sum 100 pcs.
That's what I need, edit M like the edited measure with the doule sort to bring less pallets. 

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