Power Query not sorting Properly (Excel 2013)

%3CLINGO-SUB%20id%3D%22lingo-sub-47717%22%20slang%3D%22en-US%22%3EPower%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-47717%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20all%2C%26nbsp%3BI%20am%20new%20to%20these%20forums%20so%20forgive%20if%26nbsp%3BI%20am%20doing%20anything%20wrong.%3C%2FP%3E%3CP%3EI%20have%20come%20across%20a%20problem%20with%20Power%20Query%20in%20Excel%202013.%26nbsp%3B%20I%20am%20importing%20from%20a%20Table%20in%20a%20different%20Excel%20workbook%2C%20I%20have%20removed%20some%20columns%20and%20i%20then%20want%20to%20sort%20the%20data%20by%20each%20of%20the%20four%20coulumns%20in%20turn.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIhave%20never%20had%20an%20issue%20with%20this%20in%20the%20past%20and%20it%20was%20always%20quite%20simple%2C%20in%20fact%20in%20the%20query%20editor%20it%20appears%20to%20sort%20OK.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYet%20when%20i%20select%20%22Close%20and%20Load%22%20when%20the%20table%20updates%20it%20is%20not%20sorted%20at%20all.%26nbsp%3B%20The%20columns%20to%20be%20sorted%20are%20text%20columns.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%20what%20to%20look%20for%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-47717%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analytics%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENeed%20Help%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191305%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191305%22%20slang%3D%22en-US%22%3E%3CP%3EI%20noticed%20that%20sorting%20follows%20the%20ASCII%20codes%2C%20which%20is%20different%20from%20Excel's%20sorting%20behavior.%20Does%20it%20explain%20the%20unexpected%20sorting%20result%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-149922%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-149922%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Sergei%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20a%20lot%20for%20your%20comments!%3CBR%20%2F%3EI%20have%20tha%20same%20issue%20of%20Peter.%20The%20sorting%20look%20correct%20in%20Query%20Editor%20Preview%2C%20however%20when%20I%20Load%20to%20table%20the%20sorting%20looks%20completely%20different%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-47987%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-47987%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Peter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20could%20be%20bit%20abstract%20without%20knowing%20the%20details%20of%20you%20model%2C%20however%2C%3C%2FP%3E%3CP%3E-%20on%20PQ%20site%20try%20to%20isolate%20your%20table%20in%20memory%20using%20'Add%20this%20data%20to%20Data%20Model'%20in%20Load%20To%20options%3B%20or%20by%20using%20Table.Buffer()%20in%20M%20script%3B%3C%2FP%3E%3CP%3E-%20on%20Excel%20site%20right%20click%20on%20your%20table%20and%20in%20Table-%26gt%3BExternal%20Data%20Properties%20be%20sure%20options%20are%20checked%20as%20you%20need%2C%20especially%20Preserve%20column%20sort%2Ffilter%2Flayout%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1589005%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1589005%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F41109%22%20target%3D%22_blank%22%3E%40Peter%20Downer%3C%2FA%3E%26nbsp%3BI%20know%20it's%20been%20a%20long%20time%20but%20I've%20been%20looking%20for%20an%20answer%20to%20this%20problem%20for%20a%20long%20time%20and%20found%20it%20on%20an%20even%20older%20thread%3A%20unchecking%20the%20%22Add%20this%20to%20the%20Data%20Model%22%20option%20in%20the%20%22Load%20To...%22%20settings%2C%20makes%20the%20data%20sorting%20in%20the%20table%20the%20same%20as%20the%20one%20seen%20in%20the%20query%20preview.%3CBR%20%2F%3E%3CBR%20%2F%3ESource%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fchandoo.org%2Fforum%2Fthreads%2Fpower-query-sorting.23455%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fchandoo.org%2Fforum%2Fthreads%2Fpower-query-sorting.23455%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1589284%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1589284%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F759683%22%20target%3D%22_blank%22%3E%40claudiapeitzp%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20sort%20correctly%20you%20need%20to%20fox%20the%20table%20in%20memory.%20IMHO%2C%20Load%20to%20data%20model%20is%20not%20the%20optimal%20way%20for%20that.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1589491%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1589491%22%20slang%3D%22en-US%22%3EI%E2%80%99m%20not%20sure%20what%20you%20mean%20by%20fixing%20the%20table%20in%20memory%20but%20I%20tried%20multiple%20solutions%20(sort%20by%20index%2C%20changing%20the%20data%20type%2C%20etc)%20and%20this%20was%20the%20only%20one%20that%20worked.%20Unlike%20for%20instance%20Pivot%20Tables%2C%20I%20also%20found%20no%20special%20benefits%20or%20changes%20by%20adding%20the%20data%20to%20the%20model%20so%20this%20solution%20didn%E2%80%99t%20really%20take%20away%20any%20%E2%80%9Coptimality%E2%80%9D%20for%20me%2C%20on%20the%20contrary.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1599687%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1599687%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F759683%22%20target%3D%22_blank%22%3E%40claudiapeitzp%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20may%20fix%20in%20memory%20by%20using%20Table.Buffer(Table.Sort...%20or%20by%20adding%20Index%20column%20right%20before%20sorting%20and%20remove%20it%20after%20that%20if%20not%20needed.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1599696%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1599696%22%20slang%3D%22en-US%22%3EThank%20you!%20The%20issue%20was%20corrected.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1629516%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20not%20sorting%20Properly%20(Excel%202013)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1629516%22%20slang%3D%22en-US%22%3EThank%20you%2C%20this%20resolved%20my%20sorting%20issue%3C%2FLINGO-BODY%3E
Occasional Visitor

Hello all, I am new to these forums so forgive if I am doing anything wrong.

I have come across a problem with Power Query in Excel 2013.  I am importing from a Table in a different Excel workbook, I have removed some columns and i then want to sort the data by each of the four coulumns in turn.

 

Ihave never had an issue with this in the past and it was always quite simple, in fact in the query editor it appears to sort OK.

 

Yet when i select "Close and Load" when the table updates it is not sorted at all.  The columns to be sorted are text columns.

 

Any ideas what to look for?

9 Replies

Hi Peter,

 

That could be bit abstract without knowing the details of you model, however,

- on PQ site try to isolate your table in memory using 'Add this data to Data Model' in Load To options; or by using Table.Buffer() in M script;

- on Excel site right click on your table and in Table->External Data Properties be sure options are checked as you need, especially Preserve column sort/filter/layout

Hi Sergei,

 

Thanks a lot for your comments!
I have tha same issue of Peter. The sorting look correct in Query Editor Preview, however when I Load to table the sorting looks completely different

I noticed that sorting follows the ASCII codes, which is different from Excel's sorting behavior. Does it explain the unexpected sorting result?

@Peter Downer I know it's been a long time but I've been looking for an answer to this problem for a long time and found it on an even older thread: unchecking the "Add this to the Data Model" option in the "Load To..." settings, makes the data sorting in the table the same as the one seen in the query preview.

Source: https://chandoo.org/forum/threads/power-query-sorting.23455/

@claudiapeitzp 

To sort correctly you need to fox the table in memory. IMHO, Load to data model is not the optimal way for that.

I’m not sure what you mean by fixing the table in memory but I tried multiple solutions (sort by index, changing the data type, etc) and this was the only one that worked. Unlike for instance Pivot Tables, I also found no special benefits or changes by adding the data to the model so this solution didn’t really take away any “optimality” for me, on the contrary.

@claudiapeitzp 

You may fix in memory by using Table.Buffer(Table.Sort... or by adding Index column right before sorting and remove it after that if not needed.

Thank you! The issue was corrected.
Thank you, this resolved my sorting issue