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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
joooffice
Helper I
Helper I

Help with pivot columns

Hi

 

I have the data in my table as per the first table and need to it be like the second table. 

 

The origianl data is one row per submission  with the people and their selected session arranged in columns but every person in the submission can select different sessions. 

 

I need to get it so that each row contains one session per submission and then the people within the submission that have selected that session

 

Original Data

Submission IDAccount IDAdult 1 NameAdult 1 Day 1 SessionAdult 1 Day 2 SessionAdult 2 NameAdult 2 Day 1 SessionAdult 2 Day 2 SessionChild 1 NameChild 1 Day 1 SessionChild 1 Day 2 SessionChild 2 NameChild 2 Day 1 SessionChild 2 Day 2 Session
3062552449StephenSession ASession XCarolineSession ASession XAsherSession ASession XShaneSession ASession X
3052552737ElaineSession ASession YBobSession ASession X      
3042552818MarilynSession BSession RJohnSession CSession R      
3032553086PeterSession BSession TJackie Session T      
3022552925JulianSession CSession R   DavidSession CSession RJessica Session Y
3015988889ShaunSession C          
2993489538DavidSession DSession TJaneSession DSession T      
2985831111IanSession BSession XTimSession BSession XLouiseSession ASession XSam Session X

 

Expected output

 

  Day 1 SessionName 1Name 2 Name 3Name 4
3062552449Session AStephenCarolineAsherShane
3062552449Session XStephenCarolineAsherShane
3052552737Session AElaineBob  
3052552737Session YElaine   
3052552737Session XBob   
3042552818Session BMarilyn   
3042552818Session CJohn   
3042552818Session RMarilynJohn  
3032553086Session BPeter    
3032553086Session TPeter Jackie  
3022552925Session CJulianDavid  
3022552925Session RJulianDavid  
3022552925Session YJessica   
3015988889Session CShaun   
2993489538Session CDavidJane  
2993489538Session TDavidJane  
2985831111Session BIanTim  
2985831111Session ALouise   
2985831111Session XIanTimLouise Same

 

i know it is some combination of pivot coloumns and group by but I can't get it to work correctly.

 

I'm using power query rather than DAX

 

Thanks for your help

1 ACCEPTED SOLUTION

@joooffice here's my take on it:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLBjoIwEIZfxXD2IC1d26OihyVrYsSDxngYtQmNSDcgJr69M4FDQWxsevjSaeef/jOHQ8AnP8E4YEKwKFJI6V3/Z7og0lVlbDGaObxDjqG0uSm058qsynTpiacZeN4fx1SWaMua8inSMofPknvkuT15BPu7kYhaCRlKpBWUJn+6P587vEFObOaG4154WIQ3Inwiyei1vneccSW2JAHnq9FNAvd8ODdrP6AYuZXUuYGvClzAw1w8FxPiM3TL2LeiIZ4JJXGpppV1X9O3KQdT9JJHUgkuB8pZvJnSaX0/PCxBiYXkIS6kX/jUWBqPrbl5on+2NpVv3FO4da3CGT6+AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Submission ID" = _t, #"Account ID" = _t, #"Adult 1 Name" = _t, #"Adult 1 Day 1 Session" = _t, #"Adult 1 Day 2 Session" = _t, #"Adult 2 Name" = _t, #"Adult 2 Day 1 Session" = _t, #"Adult 2 Day 2 Session" = _t, #"Child 1 Name" = _t, #"Child 1 Day 1 Session" = _t, #"Child 1 Day 2 Session" = _t, #"Child 2 Name" = _t, #"Child 2 Day 1 Session" = _t, #"Child 2 Day 2 Session" = _t]),
    #"Unpivoted All but Submission ID & Account ID" = Table.UnpivotOtherColumns(Source, {"Submission ID", "Account ID"}, "Attribute", "Value"),
    #"Remove nulls" = Table.SelectRows(#"Unpivoted All but Submission ID & Account ID", each ([Value] <> "")),
    #"Filter Just Names" = Table.SelectRows(#"Remove nulls", each Text.EndsWith([Attribute], "Name")),
    #"Renamed Columns" = Table.RenameColumns(#"Filter Just Names",{{"Attribute", "Title"}, {"Value", "Name"}}),
    #"Final Name List" = Table.TransformColumns(#"Renamed Columns", {{"Title", each Text.BeforeDelimiter(_, " Name"), type text}}),
    #"Filter Just Sessions" = Table.SelectRows(#"Remove nulls", each not Text.EndsWith([Attribute], "Name")),
    #"Replaced Value" = Table.ReplaceValue(#"Filter Just Sessions"," Session","",Replacer.ReplaceText,{"Attribute"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value", "Attribute", Splitter.SplitTextByEachDelimiter({" Day "}, QuoteStyle.Csv, false), {"Title", "Day"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Title", type text}, {"Day", Int64.Type}}),
    #"Final Session List" = Table.RenameColumns(#"Changed Type",{{"Value", "Session"}}),
    #"Merge Names into Sessions" = Table.NestedJoin(#"Final Session List", {"Submission ID", "Account ID", "Title"}, #"Final Name List", {"Submission ID", "Account ID", "Title"}, "NameList", JoinKind.LeftOuter),
    #"Expand Names" = Table.ExpandTableColumn(#"Merge Names into Sessions", "NameList", {"Name"}, {"Name"}),
    #"Merged Queries" = Table.NestedJoin(#"Expand Names", {"Submission ID", "Account ID", "Day", "Session"}, #"Expand Names", {"Submission ID", "Account ID", "Day", "Session"}, "Expand Names", JoinKind.LeftOuter),
    #"Aggregated Expand Names" = Table.AggregateTableColumn(#"Merged Queries", "Expand Names", {{"Title", each List.Sort(_), "List of Titles"}}),
    #"Added Custom" = Table.AddColumn(#"Aggregated Expand Names", "Name Column Header", each "Name " & Text.From(List.PositionOf([List of Titles],[Title])+1), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Title", "List of Titles"}),
    #"Pivot Titles" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Name Column Header]), "Name Column Header", "Name")
in
    #"Pivot Titles"

 

The query is in two main sections, producing:

  • "Final Name List" - a list of names for each Submission/Account
  • "Final Session List" - a list of sessions for each Submission/Account/Title (Title being Adult 1 etc)

These are then merged and unpivoted.

I also included a Day column which is an integer (1 or 2 in the sample data).

 

Does this work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@ImkeF @edhans @HotChilli 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @joooffice ,

does unpivoting the session column deliver what you're after?:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLBjoIwEIZfxXD2IC1d26OihyVrYsSDxngYtQmNSDcgJr69M4FDQWxsevjSaeef/jOHQ8AnP8E4YEKwKFJI6V3/Z7og0lVlbDGaObxDjqG0uSm058qsynTpiacZeN4fx1SWaMua8inSMofPknvkuT15BPu7kYhaCRlKpBWUJn+6P587vEFObOaG4154WIQ3Inwiyei1vneccSW2JAHnq9FNAvd8ODdrP6AYuZXUuYGvClzAw1w8FxPiM3TL2LeiIZ4JJXGpppV1X9O3KQdT9JJHUgkuB8pZvJnSaX0/PCxBiYXkIS6kX/jUWBqPrbl5on+2NpVv3FO4da3CGT6+AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Submission ID" = _t, #"Account ID" = _t, #"Adult 1 Name" = _t, #"Adult 1 Day 1 Session" = _t, #"Adult 1 Day 2 Session" = _t, #"Adult 2 Name" = _t, #"Adult 2 Day 1 Session" = _t, #"Adult 2 Day 2 Session" = _t, #"Child 1 Name" = _t, #"Child 1 Day 1 Session" = _t, #"Child 1 Day 2 Session" = _t, #"Child 2 Name" = _t, #"Child 2 Day 1 Session" = _t, #"Child 2 Day 2 Session" = _t]),
    #"Unpivoted Only Selected Columns1" = Table.Unpivot(Source, {"Child 2 Day 2 Session", "Child 2 Day 1 Session", "Child 1 Day 2 Session", "Child 1 Day 1 Session", "Adult 2 Day 2 Session", "Adult 2 Day 1 Session", "Adult 1 Day 2 Session", "Adult 1 Day 1 Session"}, "Attribute.1", "Value.1")
in
    #"Unpivoted Only Selected Columns1"

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi Imke

 

I can't get it to work. I tried doing it with only the day 1 sessions 

 

#"Unpivoted Only Selected Columns" = Table.Unpivot(#"Replaced Value", {"Adult 1 First Name", "Adult 1 Day 1 Session", "Adult 2 First Name",  "Adult 2 Day 1 Session", "Child 1 First Name", "Child 1 Day 1 Session", "Child 2 First Name",  "Child 2 Day 1 Session", "Child 3 First Name", "Child 3 Day 1 Session", "Child 4 First Name",  "Child 4 Day 1 Session"}, "Attribute", "Value"),
#"Added Index" = Table.AddIndexColumn(#"Unpivoted Only Selected Columns", "Index", 0, 1, Int64.Type),
#"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 3), Int64.Type),
#"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Submission ID", "Account ID",  "Integer-Division"}, {{"Count", each _, type table [Submission ID=nullable number, Account ID=nullable number,Attribute=text, Value=text, #"Integer-Division"=number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Column([Count],"Value")),
#"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.SplitTextByDelimiter("=", QuoteStyle.Csv), {"Custom.1", "Custom.2", "Custom.3"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Custom.1", type text}, {"Custom.2", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Custom.1", "First Name"}, {"Custom.3", "Day 1 Session"}}),
#"Removed Columns2" = Table.RemoveColumns(#"Renamed Columns",{"Integer-Division", "Count"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns2", each [Day 1 Session] <> null and [Day 1 Session] <> "")

 

That gives me a row with each person and their session choice:

 

Submission IDAccount IDDay 1 SessionName
3062552449Session AStephen
3062552449Session ACaroline

 

I then can't work out how to group it so that matching sessions for the same submission ID are on the same row. Every unpivot and group combination i've tried doesnt get the desired result

Sorry @joooffice ,

but I cannot follow.

Maybe some of my other co-superusers has another idea? Calling in:

@vanessafvg  @tex628  @OwenAuger  @mahoneypat  @TomMartens  @mwegener  @edhans 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@joooffice here's my take on it:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLBjoIwEIZfxXD2IC1d26OihyVrYsSDxngYtQmNSDcgJr69M4FDQWxsevjSaeef/jOHQ8AnP8E4YEKwKFJI6V3/Z7og0lVlbDGaObxDjqG0uSm058qsynTpiacZeN4fx1SWaMua8inSMofPknvkuT15BPu7kYhaCRlKpBWUJn+6P587vEFObOaG4154WIQ3Inwiyei1vneccSW2JAHnq9FNAvd8ODdrP6AYuZXUuYGvClzAw1w8FxPiM3TL2LeiIZ4JJXGpppV1X9O3KQdT9JJHUgkuB8pZvJnSaX0/PCxBiYXkIS6kX/jUWBqPrbl5on+2NpVv3FO4da3CGT6+AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Submission ID" = _t, #"Account ID" = _t, #"Adult 1 Name" = _t, #"Adult 1 Day 1 Session" = _t, #"Adult 1 Day 2 Session" = _t, #"Adult 2 Name" = _t, #"Adult 2 Day 1 Session" = _t, #"Adult 2 Day 2 Session" = _t, #"Child 1 Name" = _t, #"Child 1 Day 1 Session" = _t, #"Child 1 Day 2 Session" = _t, #"Child 2 Name" = _t, #"Child 2 Day 1 Session" = _t, #"Child 2 Day 2 Session" = _t]),
    #"Unpivoted All but Submission ID & Account ID" = Table.UnpivotOtherColumns(Source, {"Submission ID", "Account ID"}, "Attribute", "Value"),
    #"Remove nulls" = Table.SelectRows(#"Unpivoted All but Submission ID & Account ID", each ([Value] <> "")),
    #"Filter Just Names" = Table.SelectRows(#"Remove nulls", each Text.EndsWith([Attribute], "Name")),
    #"Renamed Columns" = Table.RenameColumns(#"Filter Just Names",{{"Attribute", "Title"}, {"Value", "Name"}}),
    #"Final Name List" = Table.TransformColumns(#"Renamed Columns", {{"Title", each Text.BeforeDelimiter(_, " Name"), type text}}),
    #"Filter Just Sessions" = Table.SelectRows(#"Remove nulls", each not Text.EndsWith([Attribute], "Name")),
    #"Replaced Value" = Table.ReplaceValue(#"Filter Just Sessions"," Session","",Replacer.ReplaceText,{"Attribute"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value", "Attribute", Splitter.SplitTextByEachDelimiter({" Day "}, QuoteStyle.Csv, false), {"Title", "Day"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Title", type text}, {"Day", Int64.Type}}),
    #"Final Session List" = Table.RenameColumns(#"Changed Type",{{"Value", "Session"}}),
    #"Merge Names into Sessions" = Table.NestedJoin(#"Final Session List", {"Submission ID", "Account ID", "Title"}, #"Final Name List", {"Submission ID", "Account ID", "Title"}, "NameList", JoinKind.LeftOuter),
    #"Expand Names" = Table.ExpandTableColumn(#"Merge Names into Sessions", "NameList", {"Name"}, {"Name"}),
    #"Merged Queries" = Table.NestedJoin(#"Expand Names", {"Submission ID", "Account ID", "Day", "Session"}, #"Expand Names", {"Submission ID", "Account ID", "Day", "Session"}, "Expand Names", JoinKind.LeftOuter),
    #"Aggregated Expand Names" = Table.AggregateTableColumn(#"Merged Queries", "Expand Names", {{"Title", each List.Sort(_), "List of Titles"}}),
    #"Added Custom" = Table.AddColumn(#"Aggregated Expand Names", "Name Column Header", each "Name " & Text.From(List.PositionOf([List of Titles],[Title])+1), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Title", "List of Titles"}),
    #"Pivot Titles" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Name Column Header]), "Name Column Header", "Name")
in
    #"Pivot Titles"

 

The query is in two main sections, producing:

  • "Final Name List" - a list of names for each Submission/Account
  • "Final Session List" - a list of sessions for each Submission/Account/Title (Title being Adult 1 etc)

These are then merged and unpivoted.

I also included a Day column which is an integer (1 or 2 in the sample data).

 

Does this work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors