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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Omid_Motamedise
Super User
Super User

Mastering Table.Group in Power Query: Unlocking the fifth argument

The Table.Group function in Power Query is an incredible tool for summarizing and grouping data efficiently. Here's a quick overview of its syntax:
Table.Group(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as nullable function) as table

Omid_Motamedise_2-1724718262129.jpeg

 


While the last two parameters are optional, they can significantly enhance the performance of this function. In this post, I’ll focus on the fifth input, optional comparer as nullable function parameter.

This parameter is typically defined as a function with two input parameters mostly as below:
(x,y)=>Number.From(……..)
At the beginning the x define the first row of the table (Beginning of grouping) and y is determined the every next row in the table. For each y, the Number.From(……..) is evaluated, if the result of this function be 0, y is changed to the next row of table, but if the result of this function be 1, then the group is created and then x is changed to the next row and y determined the rows after x. This process is continued to reach the end of table.


Three different example is provided as bellow (the examples are more clear on the attached images)

📢 If you want to group all rows where the "Region" column is not null, use the following formula:
(x,y)=>Number.From(y[Region]<>null)

Omid_Motamedise_3-1724718270384.jpeg

 

📢 To group data every 7 days based on the "Date" column, apply this formula:
(x,y)=>Number.From(y[Date]- x[Date]>=hashtag#duration(7,0,0,0))

Omid_Motamedise_4-1724718277050.jpeg

 

📢 If your goal is to group rows where the total cost of each group is less than $130, use this function:
(x,y)=>Number.From(List.Sum(List.Range(Source[Cost], x[Index],y[Index]-x[Index]+1))>130))

Omid_Motamedise_5-1724718282624.jpeg

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
4 REPLIES 4
lbendlin
Super User
Super User

Thank you for your observation. There's no doubt that thebiccountant.com is an amazing website with inspiring content. However, it's important to note that the combination of Table.Group and Number.From isn't exclusive to any single source. In fact, I was inspired by a video from Mike Girvin that demonstrated a similar approach, which led me to explore this combination in my work.If you look beyond a superficial comparison, you'll notice that the first two examples in my content are introductory and serve to set the stage for the third example. This third example is unique in its approach, as it doesn't simply focus on merging based on starting and ending rows. Instead, it considers all the values within a group to meet a specific condition, which is a distinct application of these functions.

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

Hello, @Omid_Motamedise your 3rd usage of GroupKind.Local is so "unique" that I'd never recommend using it 😁 Have you ever tried it on lets say 5K rows of data? In Excel? Your code hits Source[Cost] list with List.Sum(List.Range(...)) so many times while you did not even bother with buffering it... Well, maybe I am wrong and PQ delegates this job to your source database (if it's your case) but I would prefer doing this with (pre-calculated) buffered list of running total cost.

let
    Source = Table1,
    cost = List.Buffer(Source[cost]), 
    rt_cost = List.Buffer(
        List.Generate(
            () => [i = 0, rt = cost{0}],
            (x) => x[i] < List.Count(cost),
            (x) => [i = x[i] + 1, rt = x[rt] + cost{i}],
            (x) => x[rt]
        )
    ),
    res = Table.Group(
        Source,
        "Index", 
        {"Count", each List.Sum([cost])},
        GroupKind.Local, 
        (s, c) => Number.From((rt_cost{c} - rt_cost{s}) >= 130)
    )
in
    res

 Meanwhile, you may skip column reference in 5th argument (x[Index] or y[Index] in your code) and use just "x" and "y" if you use just single column to group by ("Index") and skip curley braces {} in 2nd argument of Table.Group.

Hi @AlienSx ,

Thanks for pointing out Table.Buffer. I chose not to mention it to keep the solution simpler.

I also appreciate you sharing your solution—it's brilliant to use accumulated values instead of exact ones. However, when I applied your formula to the next set of data, the grouping resulted in sums like 142, 155, 170, 189, and so on. It stopped grouping when the total exceeded 130, whereas we need it to stop before reaching the max value.



9/07/2024 12:00:00 AM190
28/06/2024 12:00:00 AM551
26/07/2024 12:00:00 AM352
30/06/2024 12:00:00 AM333
4/06/2024 12:00:00 AM574
20/05/2024 12:00:00 AM305
10/07/2024 12:00:00 AM686
11/06/2024 12:00:00 AM557
11/06/2024 12:00:00 AM468
19/06/2024 12:00:00 AM699
7/06/2024 12:00:00 AM6310
23/05/2024 12:00:00 AM6211
2/07/2024 12:00:00 AM3012
17/07/2024 12:00:00 AM3413
13/08/2024 12:00:00 AM3814
20/06/2024 12:00:00 AM6315
31/05/2024 12:00:00 AM3616
17/07/2024 12:00:00 AM4517
10/06/2024 12:00:00 AM5118
16/06/2024 12:00:00 AM6919
19/07/2024 12:00:00 AM2720
16/06/2024 12:00:00 AM1421

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors