We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now
Issue: Data needs to be moved from one database to another. Unfortunately I do not have access to either database directly, I only have access to what I can export. Database export gives me contacts that are related to an ID, however each contact is on a different row. Database we are importing to requires all contacts to be on one row.
We need to flatten data in a way that looks for similar user_id's, and if they match, move that data a new column up to, but no more than 4 times and keep all null data.
Obstacles: In the database we are exporting from, it is possible to have as many contacts assigned to a user id as you want. The database we are importing to has a max of 4 contacts per user_id.
The ultimate goal is to automate the process when a new export is detected.
exported Dataset
student_user_id,ec_firstname,ec_lastname,ec_phone,ec_email
4768086,John,Doe,5555555555,email@email.com
4768086,John,Doe,5555555555,email@email.com
4768308,John,Doe,5555555555,email@email.com
4768308,John,Doe,5555555555,email@email.com
4768308,John,Doe,5555555555,email@email.com
4858034,John,Doe,5555555555,email@email.com
4858034,John,Doe,5555555555,email@email.com
4858034,John,Doe,5555555555,email@email.com
4858034,John,Doe,5555555555,email@email.com
4925492,John,Doe,5555555555,email@email.com
4925492,John,Doe,5555555555,email@email.com
4925492,John,Doe,5555555555,email@email.com
4925492,John,Doe,5555555555,email@email.com
4925492,John,Doe,5555555555,email@email.com
Required Dataset to Import
student_user_id,ec_firstname,ec_lastname,ec_phone,ec_email,ec2_firstname,ec2_lastname,ec2_phone,ec2_email,ec3_firstname,ec3_lastname,ec3_phone,ec3_email,ec4_firstname,ec4_lastname,ec4_phone,ec4_email
4768086,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,,,,,,,,
4768308,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,,,,
4858034,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com
4925492,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com,John,Doe,5555555555,email@email.com
I figured Power Query is the best option, but if there is a better one out there, let me know. Most of the options I found when doing a search did not match what I needed, nor do I have the requisite Excel knowledge to modify an existing solution.
Solved! Go to Solution.
Hi @MD_1385 ,
I converted this:
to this (not all columns showing because it is WIDE
This is the code - 3 steps including the source.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjE3szCwMFPSUfLKz8gDUi75qUDSFA6AnNTcxMwcBzCpl5yfqxSrQ5k+YwOLwa/PwtTCwNhk+OqzNDIF4lF9yPpiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [student_user_id = _t, ec_firstname = _t, ec_lastname = _t, ec_phone = _t, ec_email = _t]),
#"Grouped Rows" =
Table.Group(
Source,
{"student_user_id"},
{
{"All Rows", each
Table.PromoteHeaders(
Table.Transpose(
Table.CombineColumns(
Table.TransformColumnTypes(
Table.UnpivotOtherColumns(
Table.AddIndexColumn(_, "Index", 0),
{"Index"},
"Attribute",
"Value"),
{
{"Index", type text}
},
"en-US"),
{"Attribute", "Index"},
Combiner.CombineTextByDelimiter("", QuoteStyle.None),
"New Columns"
)
)
)
}
}
),
FinalStep = Table.Combine(#"Grouped Rows"[All Rows])
in
FinalStep
I did everything in a nested table grouping based on the student user id.
You can convert the nulls to commas or whatever at this point.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingHi @MD_1385 ,
I converted this:
to this (not all columns showing because it is WIDE
This is the code - 3 steps including the source.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjE3szCwMFPSUfLKz8gDUi75qUDSFA6AnNTcxMwcBzCpl5yfqxSrQ5k+YwOLwa/PwtTCwNhk+OqzNDIF4lF9yPpiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [student_user_id = _t, ec_firstname = _t, ec_lastname = _t, ec_phone = _t, ec_email = _t]),
#"Grouped Rows" =
Table.Group(
Source,
{"student_user_id"},
{
{"All Rows", each
Table.PromoteHeaders(
Table.Transpose(
Table.CombineColumns(
Table.TransformColumnTypes(
Table.UnpivotOtherColumns(
Table.AddIndexColumn(_, "Index", 0),
{"Index"},
"Attribute",
"Value"),
{
{"Index", type text}
},
"en-US"),
{"Attribute", "Index"},
Combiner.CombineTextByDelimiter("", QuoteStyle.None),
"New Columns"
)
)
)
}
}
),
FinalStep = Table.Combine(#"Grouped Rows"[All Rows])
in
FinalStep
I did everything in a nested table grouping based on the student user id.
You can convert the nulls to commas or whatever at this point.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingExcellent @MD_1385 - glad I was able to help
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingIf you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 5 | |
| 3 | |
| 3 | |
| 2 | |
| 2 |
| User | Count |
|---|---|
| 11 | |
| 10 | |
| 7 | |
| 6 | |
| 5 |