Forum Discussion
Table Sort , sort order , comparer
- 4 months ago
You gave incomplete sorting instructions - only sorting by item. That gives Power Query the liberty to sort the rows within each group lazily. There will be no guaranteed sort order within each item group.
For more details refer to Ben Gribaudo's primer (best resource on Power Query)
Power Query M Primer (part 1): Introduction, Simple Expressions & let | Ben Gribaudo
- 4 months ago
Power Query's Table.Sort is stable and uses a multi-key comparison, but when you sort only by one column, the relative order of rows with the same value in that column is not guaranteed to preserve the original order from Table.Combine.
In your example, the "c" rows from the two tables are getting interleaved in a way that feels inconsistent.
Why this happens
- Table.Combine basically appends the rows (it tries to keep the order of the input tables, but it's not a strict guarantee in all cases).
- Table.Sort is a stable sort in theory, but in practice when you only specify one column, the M engine compares rows using their internal representation.
- When two rows have the exact same value in the sort column (item = "c"), the comparison often falls back to other columns or internal row order, which can produce results that look "random" or different from Excel.
This is different from Excel, which is usually more predictable with "sort by this column only" while preserving original order for ties.
How to get predictable / Excel-like behavior
If you want all rows with the same item grouped together (and within the same item, a predictable order), you should sort by multiple columns:
let details = #table( type table [item = text, id = text, num = number], {{"c", "4", 3}, {"c", "3", 3}, {"c", "2", 4}, {"c", "1", 6}} ), all = #table( type table [item = text, id = text, num = number], {{"a", "", "12"}, {"b", "", "53"}, {"c", "", "55"}, {"d", "", "23"}, {"e", "", "33"}} ), Combined = Table.Combine({all, details}), Sorted = Table.Sort( Combined, {{"item", Order.Ascending}, {"id", Order.Ascending}, // secondary sort {"num", Order.Ascending} // tertiary sort (optional) } ) in Sorted
Hi,
möglicherweise habe ich Dich falsch verstanden, aber die Liste der Spalten und deren Sortierreihenfolge beschränkt sich nicht auf nur eine Spalte.
Meintest Du vielleicht etwas in der Art?
= Table.Sort(Table.Combine({all, details }), {{"item",Order.Ascending},{"num", Order.Descending}})