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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
David_V
Frequent Visitor

Power Query: Group By function ignore previous sorting

Hello,

I've an issue with the group by function. It seems to ignore sorting from previous steps.

 

Example with a very simple data set (the goal is to keep the latest value for an ID):

raw data:

raw.jpg

 

sorted data:

= Table.Sort(#"Changed Type",{{"date", Order.Ascending}})

sorted.jpg

 

 

After the group by:

= Table.Group(#"Sorted Rows", {"id"}, {{"nb", each List.Last([nb]), type number}, {"date", each List.Last([date]), type date}})

groupBy.jpg

 

This should've keep the values for the 03/09/2017.

 

Thanks,

David

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

One should always bear in mind that there may be a huge difference between the code steps and the way those code steps are actually evaluated when Power Query executes a query.

 

Your issue is similar to first sorting a table and next remove duplicates (Table.Distinct): it may be that not the first record will be kept.

 

Also the solution seems to be similar: buffer the table after sorting (and before grouping).
This will give you the expected results:

 

= Table.Buffer(Table.Sort(Source,{{"date", Order.Ascending}}))
Specializing in Power Query Formula Language (M)

View solution in original post

5 REPLIES 5
MarcelBeug
Community Champion
Community Champion

One should always bear in mind that there may be a huge difference between the code steps and the way those code steps are actually evaluated when Power Query executes a query.

 

Your issue is similar to first sorting a table and next remove duplicates (Table.Distinct): it may be that not the first record will be kept.

 

Also the solution seems to be similar: buffer the table after sorting (and before grouping).
This will give you the expected results:

 

= Table.Buffer(Table.Sort(Source,{{"date", Order.Ascending}}))
Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Thanks for the information, I was looking for a logical issue in my code.
Nice solution, easy to understand and to the point.

Thank you for your response. I did find this workaround yesterday but:

1) is there a consequence in term of performances when dealing large amount of data (why is this not the default behaviour)

2) is there other cases when we should buffer the table (for instance when using Table.Distinct like you said in your post)

 

Thanks,

David

1) a - performance.

Table.Buffer is meant to improve performance.

Otherwise it will stop any query folding in which case performance may be degraded substantially.

 

1) b - why is this not the default behaviour.

That would be a question for Microsoft,

You can also ask: "Why would this be the default behaviour?":

grouping as such doesn't say anything about the sort order within the groups.

If you want to be certain, you can also select the maximum date during grouping:

 

let
    Source = Table1,
    #"Grouped Rows" = Table.Group(Source, {"id"}, {{"AllRows", each Table.MaxN(_,"date",1), Value.Type(Source)}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"date", "nb"}, {"date", "nb"})
in
    #"Expanded AllRows"

 

2) Other cases

Table buffering is highly recommended before initiating any loops with either List.Accumulate, List.Generate or recursive functions. In the latter case, also buffering is required within the recursive function.

Also before performing approximate lookups, the lookup table should be buffered.

 

Example code (untested) using approximate lookup: 

 

MyBufferedLookupTable = Table.Buffer(MyLookupTable),
AddColumn = Table.AddColum(MyTable,"LookupValue", (CurrentRecord) => Table.SelectRows(MyBufferedLookupTable, each [LookupValue] <= CurrentRecord[SearchValue]){0})
Specializing in Power Query Formula Language (M)

Thanks a lot. I understand a little bit better now and your examples are very interesting.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.