Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
I've created a simple table where I want to sort based on a calculation of month (as number) plus the number in the Value-column.
When I sort using a one-argument function (the step #"Only a"), using:
(a) => a[Sort function]
I get the expected behaviour, sorting in scending fashion.
When I sort using a two-argument function, using the same function and not referencing the second argument:
(a, b) => a[Sort function]
I get a sort order that switches between a larger and a smaller value.
I don't have the brains to figure out the pattern, or why Power Query is doing this.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMU9JRMlSK1YlWcktNArKNwGzfxCIg2xjMhqgxQVJjiqTGDEmNOZIaCyQ1lkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type text}, {"Value", Int64.Type}}),
#"Added Custom" = Table.StopFolding(
Table.AddColumn(#"Changed Type", "Sort function", each Date.Month(Date.From("01-" & [Month] & "2025")) * 10 + [Value], Int64.Type) //month as number * 10 + [Value]
),
#"Only a" = Table.Sort(#"Added Custom",
(a) => a[Sort function] //Sorts ascending
),
#"a and b, same function" = Table.Sort(#"Added Custom",
(a, b) => a[Sort function] //switches between bigger and smaller???
),
#"a minus b" = Table.Sort(#"Added Custom",
(a, b) => a[Sort function] - b[Sort function] //also sorts ascending
)
in
#"a minus b"
Solved! Go to Solution.
Hi @WesleyS ,
Power Query has certain expectations concerning the comparer function. If you don't meet these requirements Power Query doesn't necessarily react with an error message but with a unpredictable behaviour. Randomly switching is not unusual.
The requirements for the comparer function are:
In your case it shouldn't even be necessary to use a comparer function, the following should work:
Table.Sort(#"Added Custom", {{"Sort function", Order.Ascending}})
If you absolutely need to use a comparer function, the solution should be like this one:
Table.Sort(#"Added Custom", (a, b) => Value.Compare(a[Sort function], b[Sort function]))
Hope that helps!
Hi @WesleyS ,
Power Query has certain expectations concerning the comparer function. If you don't meet these requirements Power Query doesn't necessarily react with an error message but with a unpredictable behaviour. Randomly switching is not unusual.
The requirements for the comparer function are:
In your case it shouldn't even be necessary to use a comparer function, the following should work:
Table.Sort(#"Added Custom", {{"Sort function", Order.Ascending}})
If you absolutely need to use a comparer function, the solution should be like this one:
Table.Sort(#"Added Custom", (a, b) => Value.Compare(a[Sort function], b[Sort function]))
Hope that helps!
Yeah the actual function wasn't important, I just needed something to test with.
Glad to hear that PQ's odd behaviour with the (a, b) version is not unusual, since I haven't told PQ how to relate the rows.
Thank you!
Yeah the actual sort function used isn't important, I simply needed something to test out the (a, b) => a[Sort function] version.
Well, I'm glad to not it's not me going crazy but simply a quirk of me not telling Power Query how to act.
Thanks!
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 19 | |
| 10 | |
| 9 | |
| 7 | |
| 6 |