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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
melina
Helper IV
Helper IV

merge related rows

Hi All,

 

It is possible to merge rows as below in PBI ?

Capture.PNG

1 ACCEPTED SOLUTION

The code below should do what you are looking for:

 

let
    Source = BeforeMerge,
    #"Grouped Rows" = 
        Table.Group(Source,
                    {"SO NUMBER", "Cust Name", "Order Qty", "DO Qty"},
                    {{"Container Number", each Text.Combine(List.Select([Container Number], each _ <> null and _ <> ""),", "), type text}})
in
    #"Grouped Rows"

This is the result before and after.

 

I added an example with 3 different container numbers for 1 order, just to clarify my remarks in the previous post.

 

Merge related rows.png

 

In general: when you want to group a table, you have 2 kinds of fields: the ones to group on, meaning you'll end up with the unique combinations in these fields, i.c. the first 4 fields.

You can not group on all 5 fields, otherwise you'll end up with the initial table, as a blank container number is something else as a container number.

For the other fields in your table, you need to specify how these must be aggregated, i.c. the Container Number.

The query code above will combine all not-empty container numbers, separated by ", ".

 

If you have any other colums in your table, then you should specify what to do with these as well (or they will just drop off).

Specializing in Power Query Formula Language (M)

View solution in original post

4 REPLIES 4
MarcelBeug
Community Champion
Community Champion

This can be done using "Group By" in Power Query (a.k.a. "M", a.k.a. the Query Editor).

 

However, you should consider all possible cases and your specific requirements: on which fields do you want to group, what must be done with the data in the other fields, e.g can you have multiple lines with different container numbers, should these all be concatenated, e.g. comma separated?

Specializing in Power Query Formula Language (M)

Thanks for your reply.

I want to group all the column, including container number. The rest data just remain the value. One SO number only for 1 container number, but the problem is there is certain container number is blank. So i want to merge, those same SO number with the container number by remaining other value in column. At the end of result, only have 1 SO number,not duplicate like picture above.

The code below should do what you are looking for:

 

let
    Source = BeforeMerge,
    #"Grouped Rows" = 
        Table.Group(Source,
                    {"SO NUMBER", "Cust Name", "Order Qty", "DO Qty"},
                    {{"Container Number", each Text.Combine(List.Select([Container Number], each _ <> null and _ <> ""),", "), type text}})
in
    #"Grouped Rows"

This is the result before and after.

 

I added an example with 3 different container numbers for 1 order, just to clarify my remarks in the previous post.

 

Merge related rows.png

 

In general: when you want to group a table, you have 2 kinds of fields: the ones to group on, meaning you'll end up with the unique combinations in these fields, i.c. the first 4 fields.

You can not group on all 5 fields, otherwise you'll end up with the initial table, as a blank container number is something else as a container number.

For the other fields in your table, you need to specify how these must be aggregated, i.c. the Container Number.

The query code above will combine all not-empty container numbers, separated by ", ".

 

If you have any other colums in your table, then you should specify what to do with these as well (or they will just drop off).

Specializing in Power Query Formula Language (M)

Thanks a lot! Your explanation help me to solve my problem.Smiley Wink

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors