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.
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 ID | Account ID | Adult 1 Name | Adult 1 Day 1 Session | Adult 1 Day 2 Session | Adult 2 Name | Adult 2 Day 1 Session | Adult 2 Day 2 Session | Child 1 Name | Child 1 Day 1 Session | Child 1 Day 2 Session | Child 2 Name | Child 2 Day 1 Session | Child 2 Day 2 Session |
306 | 2552449 | Stephen | Session A | Session X | Caroline | Session A | Session X | Asher | Session A | Session X | Shane | Session A | Session X |
305 | 2552737 | Elaine | Session A | Session Y | Bob | Session A | Session X | ||||||
304 | 2552818 | Marilyn | Session B | Session R | John | Session C | Session R | ||||||
303 | 2553086 | Peter | Session B | Session T | Jackie | Session T | |||||||
302 | 2552925 | Julian | Session C | Session R | David | Session C | Session R | Jessica | Session Y | ||||
301 | 5988889 | Shaun | Session C | ||||||||||
299 | 3489538 | David | Session D | Session T | Jane | Session D | Session T | ||||||
298 | 5831111 | Ian | Session B | Session X | Tim | Session B | Session X | Louise | Session A | Session X | Sam | Session X |
Expected output
Day 1 Session | Name 1 | Name 2 | Name 3 | Name 4 | ||
306 | 2552449 | Session A | Stephen | Caroline | Asher | Shane |
306 | 2552449 | Session X | Stephen | Caroline | Asher | Shane |
305 | 2552737 | Session A | Elaine | Bob | ||
305 | 2552737 | Session Y | Elaine | |||
305 | 2552737 | Session X | Bob | |||
304 | 2552818 | Session B | Marilyn | |||
304 | 2552818 | Session C | John | |||
304 | 2552818 | Session R | Marilyn | John | ||
303 | 2553086 | Session B | Peter | |||
303 | 2553086 | Session T | Peter | Jackie | ||
302 | 2552925 | Session C | Julian | David | ||
302 | 2552925 | Session R | Julian | David | ||
302 | 2552925 | Session Y | Jessica | |||
301 | 5988889 | Session C | Shaun | |||
299 | 3489538 | Session C | David | Jane | ||
299 | 3489538 | Session T | David | Jane | ||
298 | 5831111 | Session B | Ian | Tim | ||
298 | 5831111 | Session A | Louise | |||
298 | 5831111 | Session X | Ian | Tim | Louise | 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
Solved! Go to 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:
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
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 ID | Account ID | Day 1 Session | Name |
306 | 2552449 | Session A | Stephen |
306 | 2552449 | Session A | Caroline |
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:
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
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.