cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
TheoM Regular Visitor
Regular Visitor

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?

Knipsel.JPGsummarized by activity IDKnipsel2.JPGResult should look like this for selected row

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Combine values of multiple rows in one row

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)
10 REPLIES 10
Super User
Super User

Re: Combine values of multiple rows in one row

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)
TheoM Regular Visitor
Regular Visitor

Re: Combine values of multiple rows in one row

Thanks, this does the job!

Brian_M
Advisor

Re: Combine values of multiple rows in one row

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!

 

 

Brian_M
Advisor

Re: Combine values of multiple rows in one row

The previous reply beat me to it!!

ankku Member
Member

Re: Combine values of multiple rows in one row

Can you please explain How you did this? I have same requirement where I have same month in different rows and want to combine data of same month in one row. I am still in learning phase of Power BI so a detailed explanation will be very helpful.

ThanksSmiley Happy

TheoM Regular Visitor
Regular Visitor

Re: Combine values of multiple rows in one row

Hi,

You need to group on "Month" and then use each Text.Combine statement like the example earlier in this thread. Text.Combine can not be chosen in the user interface, so you need to enter the code in the advanced query editor OR choose on of the options you can choose from the user interface (like minimum or maximum) and change the code in the advanced editor.

If this doesnt help, send me an example (or screenshot) so i can see how your table looks like. In that case i can try to create the code for you.

Have fun!

ankku Member
Member

Re: Combine values of multiple rows in one row

So here is the screen shot of my Table. I understand when we need to select Column and then click on Grouping. Do I need to enable any settings while writing this query?

As you see in screen shot I need to have my Planned month grouping done and have a column with rows on different Month and summarize the Initial FC value in one row for each month. I know I am asking very basic question and hopefully If i get a detailed steps from you, I will be more comfortable in handling the queries moving forward.. Thanks for being my Tutor.


Testdata.JPG

 

 

TheoM Regular Visitor
Regular Visitor

Re: Combine values of multiple rows in one row

Hi,

I understand your goal is to summarize the FC column by month. This can be done in PowerQuery, but I would prefer to create a measure in DAX, because a measure would provide a more dynamic way to get summarized data based on filter context. If you do decide to do it in Power Query, you should take the next steps:

Select Date Column

Hit Group By, select Basic and enter a name for the summary column (for example FC Month)

Operation: choose Sum

Column: choose FC (the column for which you want to sum the values)

It should look like this:

screenshot 1.png

Hit OK and your output looks like this:

screenshot 2.png

 

When you take a look at the advanced query editor, the code you created looks like this:

#"Grouped Rows" = Table.Group(#"Changed Type", {"Planned Date"}, {{"FC Month", each List.Sum([Initial FC]), type number}})

 

I hope this is helpful

 

Theo

 

 

ankku Member
Member

Re: Combine values of multiple rows in one row

Hi Theo,

Yes it was helpful but it seems I am not going to leave you soon.Smiley Happy ..When I tried to do the same I am able to create a new Table alltogether.

Now, I have other fields as well where I would like to do the grouping for examplesee in my below table, I have Planned Date and Actual Date column, I would like to have Planned Date grouping done by Initial FC and Actual Date grouping done by YTD. I want this to be in a Same table so that I can plot a graph with Month and YTD and FC.

 

Hope I am now clear on what I am trying to achieve and you will be able to guide me in right direction. I am there but not sure how to keep it in one table everything. Thanks for being helpful.

 

Capture.JPG