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

Preparing for a certification exam? Ask exam experts all your questions on May 15th. Register now.

Reply
TheoM
Helper I
Helper I

Combine values of multiple rows in one row

Hi all,

 

I am stuck with a challenge in Power Query, which should not be very difficult to solve, but i haven't found the solution yet.

 

I have a table that links activities to persons. An activity can be linked to one or more persons, a person can be linked to one ore more activities. Every row contains one activity and one person, so if an activity has more than one person, there will be more rows for that activity.

I want to transform the table to a table that hase unique activity ID's and a column that contains all persons linked to that activity.

I have summarized the table on Activity ID (see below), but how do I get the multiple values into one column of the table?

summarized by activity IDsummarized by activity IDResult should look like this for selected rowResult should look like this for selected row

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

You can group on ACTIVITYID, choose some dummy operation for FULLNAME (e.g. Max, not "All Rows") and then adjust the generated code to combine the names:

 

let
    Source = #table(type table[ACTIVITYID = number, FULLNAME = text],{{1, "Pietje Puk"},{2, "Maarten de Ruijter"},{2, "Sjaak van den Hoek"}}),
    #"Grouped Rows" = Table.Group(Source, {"ACTIVITYID"}, {{"Persons", each Text.Combine([FULLNAME], ", "), type text}})
in
    #"Grouped Rows"
Specializing in Power Query Formula Language (M)

View solution in original post

30 REPLIES 30
bschirm
Regular Visitor

Hi, I have the following set of data (only the first several rows are shown).  I want to combine rows for every change in eDAS Code column.  So, the first 3 rows would be combined, the 4th row remains the same, the 5th, 6th, 7th rows are combined, etc.  I someone able to help me figure out how to accomplish this in Power Query?

 

bschirm_0-1744386036512.png

 

Hi,

Share some data to work with and show the expected result very clearly.  Share data in a format that can be pasted in an MS Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Original:

IndexService IdeDAS CodeQuantityRateAmount DueDescriptionEnd DateStart DateProject NameProject NumberResourceCustomerDateProjectTaskApplicationCOMPCODEELAPSEDINITDATEINITTIMEJOB$ADJJOBNAMEJOBNUMName, FirstName, LastSIDSYSPROCTERMDATETERMTIMEUSERIDApplication IDFirst NameLast NameSystem IDTransaction IDUser IDConnectingServer NameURLData Set NameDate CreatedDays BilledSize (GB)VolumeEndDateGB HoursStartDateLocationServerTSM Backup Space
1421900191139.54139.54Insight Inv 1101248267                                          
2      3/10/2026                                          
3       3/11/2025                                         
44219E2GG1290.94290.94Insight Inv 1101248674                                          
54219427S1674.39674.39Insight Inv 1101242246                                          
6      1/26/2026                                          
7       1/27/2025                                         
84219427Z1674.39674.39Insight Inv 1101242246                                          
9      1/26/2026                                          
10       1/27/2025                                         
114219427S1674.39674.39Insight Inv 1101242246                                          
12      1/26/2026                                          
13       1/27/2025                                         
144219E22M138230.538230.5Insight Inv 1101255381                                          
15      3/10/2026                                          
16       3/11/2025                                         
17421900191139.54139.54Insight Inv 1101256955                                          
18      3/10/2026                                          
19       3/11/2025                                         
2042190019125182.1825182.18Carahsoft Inv IN1836910                                          

 

Based on the data that you have shared, show the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Ok, I thought that I had uploaded that also.  Here is a screenshot:

 

bschirm_0-1744896387393.png

 

Here is the sample data:  Note that there are blank columns, additional data on rows uploaded are not in the sample data. 

IndexService IdeDAS CodeQuantityRateAmount DueDescriptionEnd DateStart DateProject NameProject NumberResourceCustomerDateProjectTaskApplicationCOMPCODEELAPSEDINITDATEINITTIMEJOB$ADJJOBNAMEJOBNUMName, FirstName, LastSIDSYSPROCTERMDATETERMTIMEUSERIDApplication IDFirst NameLast NameSystem IDTransaction IDUser IDConnectingServer NameURLData Set NameDate CreatedDays BilledSize (GB)VolumeEndDateGB HoursStartDateLocationServerTSM Backup Space
1421900191139.54139.54Insight Inv 11012482673/10/20263/11/2025                                         
24219E2GG1290.94290.94Insight Inv 1101248674                                            
34219427S1674.39674.39Insight Inv 11012422461/26/20261/27/2025                                         
44219427Z1674.39674.39Insight Inv 11012422461/26/20261/27/2025                                         
54219427S1674.39674.39Insight Inv 11012422461/26/20261/27/2025                                         
64219E22M138230.538230.5Insight Inv 11012553813/10/20263/11/2025                                         
7421900191139.54139.54Insight Inv 11012569553/10/20263/11/2025                                         
842190019125182.1825182.18Carahsoft Inv IN1836910                                            
94219309C1349.99349.99SSL Cert Renewal                                            

 

 

The data does not get pasted properly in an Excel sheet.  Please share the download link of an MS Excel file with 2 tabs - input and output.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

This M cdoe seems to work

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Service Id", Int64.Type}, {"eDAS Code", type text}, {"Quantity", Int64.Type}, {"Rate", type number}, {"Amount Due", type number}, {"Description", type text}, {"End Date", type text}, {"Start Date", type text}, {"Project Name", type any}, {"Project Number", type any}, {"Resource", type any}, {"Customer", type any}, {"Date", type any}, {"Project", type any}, {"Task", type any}, {"Application", type any}, {"COMPCODE", type any}, {"ELAPSED", type any}, {"INITDATE", type any}, {"INITTIME", type any}, {"JOB$ADJ", type any}, {"JOBNAME", type any}, {"JOBNUM", type any}, {"Name, First", type any}, {"Name, Last", type any}, {"SID", type any}, {"SYSPROC", type any}, {"TERMDATE", type any}, {"TERMTIME", type any}, {"USERID", type any}, {"Application ID", type any}, {"First Name", type any}, {"Last Name", type any}, {"System ID", type any}, {"Transaction ID", type any}, {"User ID", type any}, {"Connecting", type any}, {"Server Name", type any}, {"URL", type any}, {"Data Set Name", type any}, {"Date Created", type any}, {"Days Billed", type any}, {"Size (GB)", type any}, {"Volume", type any}, {"EndDate", type any}, {"GB Hours", type any}, {"StartDate", type any}, {"Location", type any}, {"Server", type any}, {"TSM Backup Space", type any}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"eDAS Code"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"eDAS Code"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filled Down", {"eDAS Code"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Attribute] <> "Index")),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 1, 1, Int64.Type),
    #"Merged Columns" = Table.CombineColumns(#"Added Index",{"eDAS Code", "Attribute"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
    #"Grouped Rows1" = Table.Group(#"Merged Columns", {"Merged"}, {{"Count", each Table.AddIndexColumn(_,"Index1",1)}}),
    #"Expanded Count1" = Table.ExpandTableColumn(#"Grouped Rows1", "Count", {"Value", "Index", "Index1"}, {"Value", "Index", "Index1"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Count1",{{"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Merged", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged.1", type text}, {"Merged.2", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Merged.2]), "Merged.2", "Value"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index1"})
in
    #"Removed Columns1"

Hope this helps.

Ashish_Mathur_0-1745033125040.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you.  I'll give it a try this week.  Appreciate your assistance.

pbinewbieee
Regular Visitor

any idea on how to combine 3 rows of comments? having a hard time combining 3 comments under comment_field column. need to combine 3 rows of column by latest time and date. 

Hi,

Share some data in a format that can be pasted in an MS Excel file, explain the question and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
samkamal230
Regular Visitor

Hi!

I am facing a problem similar to this. But my scenario differs as follows:

I have 3 columns, having the following data (there are more columns, but these are the ones that have unique values):

samkamal230_0-1686583218612.png


I want it to be converted to the following:

samkamal230_1-1686583271944.png


I want to do this transformation in Power Query, but am unable to do it completely (not able to achieve it completely). How can I achieve this? (Note: I don't want duplicate values to be concatenated in a single row when it comes to the columns "category" and "sub-category". There should be a unique value in columns "id" and "name" and the other columns should have distinct values concatenated.

Thanks in advance.



Hi,

This M code works

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRy1vX391TSAbKcHBWS84Esl9SkEpCAs7OjUqwOdjVAFODr6RuATwGKAQYGRiAhHx9HP4WckhSENcGOfvhUOecXFRA2C4QI2hTgi+RerGZA3BILAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, name = _t, category = _t, #"sub-category" = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"category", "sub-category"}),
    #"Grouped Rows" = Table.Group(#"Replaced Value", {"id", "name"}, {{"Categories", each Text.Combine(List.Distinct([category]),", "), type nullable text}, {"Sub categories", each Text.Combine(List.Distinct([#"sub-category"]),", "), type nullable text}})
in
    #"Grouped Rows"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashish_Mathur I tried it out, by tweaking the source to fit my case. It worked perfectly! Thank you so much once again!!!

You are welcome.  If my previous reply helped, please mark that reply as Answer.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks @Ashish_Mathur for your response. I don't understand the M code aparently. Can you please elaborate? I have created a merged table from various table and have 14 columns in that table (4 of which I have mentioned in my example because they have unique value). So do I need to make changes in the M code regarding the columns I want to include? I particularly don't understand the source step (as i am fetching data from dynamics 365 crm into tables and then merging those tables into a single table on which I want to achieve the desired functionality):

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRy1vX391TSAbKcHBWS84Esl9SkEpCAs7OjUqwOdjVAFODr6RuATwGKAQYGRiAhHx9HP4WckhSENcGOfvhUOecXFRA2C4QI2hTgi+RerGZA3BILAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, name = _t, category = _t, #"sub-category" = _t])

 
Can you please elaborate further? Thanks again for your input. 🙌

Hi,

The suggested solution in this thread should work for you scenario as well, the only difference is that you should group by two columns and add a dummy calculation for the columns you want to combine. Then edit the query by replacing the dummy calculation by the Text.Combine function.

I didn't have the chance to test this yet because I'm not near a computer atm but let me know if this works otherwise I would like to give it  a try in power query

Regards, Theo

Anonymous
Not applicable

Anyone know how to handle this?  I want attachments averaged and Limit added together for the rows with the identical company name.  

 

  LimitAttachmentBroker
 XYZ Company $                  25,000,000 $                     100,000,000XYZ Broker
 XYZ Company $                  10,000,000 $                     250,000,000XYZ Broker
 X Company $                  15,000,000 $                       25,000,000X Broker
 Y  Company $                  12,000,000 $                       50,000,000XYZF Broker
     
     
Desired Result    
 XYZ Company $                  35,000,000 $                     175,000,000XYZ Broker
 X Company $                  15,000,000 $                       25,000,000X Broker
 Y Company $                  12,000,000 $                       50,000,000XYZF Broker

Hi,

Drag Company and Broker to the row labels and write this measure

Limits = SUM(Data[Limit])

Attachments = AVERAGE(Data[Attachmnet])

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Brian_M
Responsive Resident
Responsive Resident

Hi,

 

I would expand the table to new rows so that each user is on its own row with the corresponding ActivityID and then Table.Group function to combine the names grouped by ActivityID

 

Ignore the first few rows setting up an example table. 

 

let
    Source = #table({"ActivityID","Users"}, {{ 1234,#table({"Users"},{{{"Alice Wonderland", "Bob Talon-Ted", "Jim McCustard"}}}) }, {1235,#table({"Users"},{{{"Billy Snob-Thornton", "Sally Harry","Willian E Bob Thornton"}}})}}),
    #"Expanded Users Table" = Table.ExpandListColumn(Source, "Users"),
    #"Expanded Users List Record" = Table.ExpandRecordColumn(#"Expanded Users Table", "Users", {"Users"}, {"UserList"}),
    #"Expanded Users List to Rows" = Table.ExpandListColumn(#"Expanded Users List Record", "UserList"),

    //Group all the user rows by ActivityID using the Text.Combine function
    #"Group Users by ActivityID" = Table.Group(#"Expanded Users List to Rows", {"ActivityID"}, {{"Persons", each Text.Combine([UserList],", "), type text}})
in
    #"Group Users by ActivityID"

Hope that helps!

 

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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