Forum Discussion
Attribute Index issue
Hi, this is a follow up post on my last post here. I thought it was solved; however, another issue came up.
I am trying to take a table where the first 7 columns have static data and then the following columns (attributes) have dynamic values; i.e, some rows may have information in these attribute columns and some do not. I am trying to take something like this (not exact, but similar)
| Name(static) | Email (static) | Team Name 1 | Proj 1 Allocation | Proj Assett
| Team Name 2 | Proj 2 allocation | Proj Assett | Team Name 3 | etc..... |
| John Doe | [email protected] | Test Team 1 | 2% | Assett 1 | Team 2 | 3% | |||
| Sam Smith | [email protected] | Test Team 4 | 3% | Assett 2 | Test Team 2 | 20% | Assett | Test Team 3 |
And turn it into something like this
| Name(static) | Email (static) | Team Name 1 | Proj 1 Allocation | Proj Assett |
| John Doe | [email protected] | Test Team 1 | 2% | Assett 1 |
| John Doe | [email protected] | Team 2 | 3% | |
| Sam Smith | [email protected] | Test Team 4 | 3% | Assett 2 |
| Sam Smith | [email protected] | Test Team 2 | 20% | Assett |
| Sam Smith | [email protected] | Test Team 3 |
The issue I'm having is that for one record with one value in each attribute, it's splitting out all the attributes seperately into individual attribute rows. What I need is for it to merge all the attributes for that single record, but create multiple rows for that record if they have say two values for one attribute, does that make sense?
ex:
-original record of user with only values for one team
-End result with current issue of this user/record
-Want this result
Current M Query
let
Source = Excel.Workbook(File.Contents("C:\Users\30109290\OneDrive - Bright Horizons\Test Template.xlsx"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Name", type text}, {"BHID", Int64.Type}, {"BHEmail", type text}, {"Role", type text}, {"Regular Finance Report Approver Name", type text}, {"Approver Email", type text}, {"% available for projects(rounded)", type any}, {"ADO Team Name 1", type text}, {"Proj.1 Allocation", type number}, {"PROJECT ASSET 1", type text}, {"PMO Project Name 1", type text}, {"Finance WD Project ID 1", Int64.Type}, {"ADO Team Name 2", type text}, {"Proj.2 Allocation", type any}, {"PROJECT ASSET 2", type text}, {"PMO Project Name 2", type text}, {"Finance WD Project ID 2", Int64.Type}, {"ADO Team Name 3", type text}, {"Proj.3 Allocation", type number}, {"PROJECT ASSET 3", type text}, {"PMO Project Name 3", type text}, {"Finance WD Project ID 3", Int64.Type}, {"ADO Team Name 4", type text}, {"Proj. 4 Allocation", type text}, {"PROJECT ASSET 4", type text}, {"PMO Project Name 4", type text}, {"Finance WD Project ID 4", Int64.Type}, {"ADO Team Name 5", type any}, {"Proj 5 alloc %", type any}, {"PROJECT ASSET 5", type any}, {"PMO Project Name 5", type any}, {"Finance WD Project ID 5", type any}, {"ADO Team Name 6", type any}, {"Proj. 6 Allocation", type any}, {"PROJECT ASSET 6", type any}, {"PMO Project Name 6", type any}, {"Finance WD Project ID 6", type any}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "BHID","BHEmail","Role","Regular Finance Report Approver Name","Approver Email","% available for projects(rounded)"},"Attribute", "Value"),
Custom1 = Table.TransformColumns(#"Unpivoted Other Columns",{"Attribute",each Text.Remove(_,{"0".."9","."})}),
#"Cleaned Text" = Table.TransformColumns(Custom1,{{"Attribute", Text.Clean, type text}}),
#"Grouped Rows" = Table.Group(#"Cleaned Text", {"Attribute"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
#"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Name", "BHID","BHEmail","Role","Regular Finance Report Approver Name","Approver Email","% available for projects(rounded)", "Value", "Index"}, {"Name", "BHID","BHEmail","Role","Regular Finance Report Approver Name","Approver Email","% available for projects(rounded)", "Value", "Index"}),
#"Pivoted Column" = Table.Pivot(#"Expanded Count", List.Distinct(#"Expanded Count"[Attribute]), "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
#"Removed Columns"
- Anonymous2 years ago
Hi Bristix22 ,
I suggest you to try code as below to transform your data in Power Query Editor.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFNwyU9V0lHKSslPdUjPTczM0UvOzwUKhKQWlyiEpCbmKhgCeUaqQMKxuDi1pETBUAEsDZQxAjKMQTIgEaVYnWilYKBocG5mSQZQoLgYxMBhqIkCTC/UVCMUaRDPyAAhDbPRLzE3VcFYKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Name(static)" = _t, #"Email (static)" = _t, #"Team Name 1" = _t, #"Proj 1 Allocation" = _t, #"Proj 1 Assett" = _t, #"Team Name 2" = _t, #"Proj 2 Allocation" = _t, #"Proj 2 Assett" = _t, #"Team Name 3" = _t]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Name(static)", "Email (static)"}, "Attribute", "Value"), #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "ColumnName", each if Text.Contains([Attribute], "Team Name") then "Team Name" else if Text.Contains([Attribute], "Assett") then "Proj Assett" else if Text.Contains([Attribute], "Allocation") then "Proj Allocation" else ""), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Name(static)", "Email (static)", "ColumnName", "Value"}), #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Name(static)", Order.Ascending}, {"Email (static)", Order.Ascending}, {"ColumnName", Order.Ascending}}), #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Name(static)", "Email (static)", "ColumnName"}, {{"Rows", each _, type table [#"Name(static)"=nullable text, #"Email (static)"=nullable text, ColumnName=text, Value=text]}}), #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Rows],"Index",1)), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Value", "Index"}, {"Value", "Index"}), #"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom",{"Rows"}), #"Reordered Columns1" = Table.ReorderColumns(#"Removed Columns1",{"Name(static)", "Email (static)", "Index", "ColumnName", "Value"}), #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Index", Int64.Type}}), #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[ColumnName]), "ColumnName", "Value"), #"Reordered Columns2" = Table.ReorderColumns(#"Pivoted Column",{"Name(static)", "Email (static)", "Team Name", "Index", "Proj Allocation", "Proj Assett"}), #"Removed Columns2" = Table.RemoveColumns(#"Reordered Columns2",{"Index"}), #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"Proj Allocation", Percentage.Type}, {"Proj Assett", type text}, {"Team Name", type text}}) in #"Changed Type1"Sample:
Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
1 Reply
- AnonymousNot applicable
Hi Bristix22 ,
I suggest you to try code as below to transform your data in Power Query Editor.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFNwyU9V0lHKSslPdUjPTczM0UvOzwUKhKQWlyiEpCbmKhgCeUaqQMKxuDi1pETBUAEsDZQxAjKMQTIgEaVYnWilYKBocG5mSQZQoLgYxMBhqIkCTC/UVCMUaRDPyAAhDbPRLzE3VcFYKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Name(static)" = _t, #"Email (static)" = _t, #"Team Name 1" = _t, #"Proj 1 Allocation" = _t, #"Proj 1 Assett" = _t, #"Team Name 2" = _t, #"Proj 2 Allocation" = _t, #"Proj 2 Assett" = _t, #"Team Name 3" = _t]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Name(static)", "Email (static)"}, "Attribute", "Value"), #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "ColumnName", each if Text.Contains([Attribute], "Team Name") then "Team Name" else if Text.Contains([Attribute], "Assett") then "Proj Assett" else if Text.Contains([Attribute], "Allocation") then "Proj Allocation" else ""), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Name(static)", "Email (static)", "ColumnName", "Value"}), #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Name(static)", Order.Ascending}, {"Email (static)", Order.Ascending}, {"ColumnName", Order.Ascending}}), #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Name(static)", "Email (static)", "ColumnName"}, {{"Rows", each _, type table [#"Name(static)"=nullable text, #"Email (static)"=nullable text, ColumnName=text, Value=text]}}), #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Rows],"Index",1)), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Value", "Index"}, {"Value", "Index"}), #"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom",{"Rows"}), #"Reordered Columns1" = Table.ReorderColumns(#"Removed Columns1",{"Name(static)", "Email (static)", "Index", "ColumnName", "Value"}), #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Index", Int64.Type}}), #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[ColumnName]), "ColumnName", "Value"), #"Reordered Columns2" = Table.ReorderColumns(#"Pivoted Column",{"Name(static)", "Email (static)", "Team Name", "Index", "Proj Allocation", "Proj Assett"}), #"Removed Columns2" = Table.RemoveColumns(#"Reordered Columns2",{"Index"}), #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"Proj Allocation", Percentage.Type}, {"Proj Assett", type text}, {"Team Name", type text}}) in #"Changed Type1"Sample:
Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.