Question about renaming / creating sub categories

gpierson2018

New Member
Joined
Nov 17, 2018
Messages
3
Hi, can anyone help me with this in Power Query? I'm trying to get data like this first table (Table 1) to look like (Table 2).

I understand how to unpivot and pivot in Power Query (and it doesn't work here - I get an error with Sally's hobby)

What I don't understand is how to go through and rename an Attribute that is repeated more than once per unique person/entry, as in my example, "Hobby".

I don't even know what it's called I'm trying to do. This isn't "grouping" I don't believe.
Any help would be appreciated!


..................


TABLE 1
NameAttributeEntry
TomAge40
TomEyesBlue
TomHobbyBowling
BobAge45
BobEyesBrown
SallyAge35
SallyEyesBlue
SallyHobbyFishing
SallyHobbySewing
SallyHobbySinging
TABLE 2
NameAgeEyesHobby_1Hobby_2Hobby_3
Tom40BlueBowling
Bob45Brown
Sally35BlueFishingSewingSinging

<tbody>
</tbody>


 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
you can try

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Name", "Attribute"}, {{"Count", each _, type table}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Count"),
    #"Added Custom" = Table.AddColumn(#"Pivoted Column", "Age.1", each Table.Column([Age],"Entry")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Eyes.1", each Table.Column([Eyes],"Entry")),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Hobby.1", each Table.Column([Hobby],"Entry")),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom2", {"Age.1", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Sorted Rows" = Table.Sort(#"Extracted Values",{{"Age.1", Order.Ascending}}),
    #"Extracted Values1" = Table.TransformColumns(#"Sorted Rows", {"Eyes.1", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Extracted Values2" = Table.TransformColumns(#"Extracted Values1", {"Hobby.1", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Extracted Values2", {{"Hobby.1", ""}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Errors", "Hobby.1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Hobby.1.1", "Hobby.1.2", "Hobby.1.3"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Age", "Eyes", "Hobby"})
in
    #"Removed Columns"[/SIZE]

NameAttributeEntryNameAge.1Eyes.1Hobby.1.1Hobby.1.2Hobby.1.3
TomAge
40​
Sally35BlueFishingSewingSinging
TomEyesBlueTom40BlueBowling
TomHobbyBowlingBob45Brown
BobAge
45​
BobEyesBrown
SallyAge
35​
SallyEyesBlue
SallyHobbyFishing
SallyHobbySewing
SallyHobbySinging

maybe there is any shorter way so you need to analyse and try your own :)
 
Upvote 0
Probably this is even more complicated.I created two separate queries for personal data and hobbies then merged them.

Query named Hobbies

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each ([Attribute] = "Hobby")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Name"}, {{"Count", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Hobbies", each [Count][Entry]),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Hobbies", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
#"Removed Columns1" = Table.RemoveColumns(#"Extracted Values",{"Count"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns1",{{"Name", type text}})
in
#"Changed Type"


Query named PersonalData

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each ([Attribute] <> "Hobby")),
#"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Attribute]), "Attribute", "Entry"),
#"Changed Type" = Table.TransformColumnTypes(#"Pivoted Column",{{"Age", Int64.Type}, {"Name", type text}, {"Eyes", type text}})
in
#"Changed Type"

Query named MergeData

let
Source = Table.NestedJoin(PersonalData,{"Name"},Hobbies,{"Name"},"Hobbies",JoinKind.LeftOuter),
#"Expanded Hobbies" = Table.ExpandTableColumn(Source, "Hobbies", {"Hobbies"}, {"Hobbies.1"}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Expanded Hobbies", "Hobbies.1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Hobbies.1.1", "Hobbies.1.2", "Hobbies.1.3"}),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Hobbies.1.1", type text}, {"Hobbies.1.2", type text}, {"Hobbies.1.3", type text}})
in
#"Changed Type"
 
Upvote 0
Thanks
This is slightly tidier but still not perfect:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Name"}, {{"Count", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [Count][Entry]),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Custom", Splitter.SplitTextByEachDelimiter({","}, QuoteStyle.Csv, false), {"Age", "Eyes"}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Eyes", Splitter.SplitTextByEachDelimiter({","}, QuoteStyle.Csv, false), {"Eyes", "Hobby"}),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Age", Int64.Type}, {"Eyes", type text}, {"Hobby", type text}}),
#"Split Column by Delimiter2" = Table.SplitColumn(#"Changed Type", "Hobby", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Hobby.1", "Hobby.2", "Hobby.3"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter2",{{"Hobby.1", type text}, {"Hobby.2", type text}, {"Hobby.3", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Count"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns",{{"Name", type text}})
in
#"Changed Type2"
 
Upvote 0
IMHO your code is ok :)
I usually use (or not) #"Change Type" with end result but this is not a problem
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top