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
adamvainauskas
New Member

Combine date from two tables and split rows over columns

I need to combine two sets of data (two tables) that have some identical columns. Each row has an ID Num which is assigned to an individual. Each individual may hold a number of qualifications. There are 4 types of qualification and up to 4 levels in each qualification.  The second table contains the data relating to the individual's qualification(s), with each row representing a qualification held by the individual. I want to merge these tables so that each individual only has 1 row and there a number of columns relating to each type of qualification. There are also two additional columns for the date the qualification is acquired and expires.

 

Table 1 and Table 2 below set out an example of the data and Table 3 shows how I want the data to turn out.

 

Table 1

ID NumID2First nameLast nameEmailAddress
1444PaulSmithpaulsmith@test.comaddress001
2555John Doeemail2@test.comaddress002
3333JaneSmithemail3@test.comaddress003
4111TonyJohnsonemail3@test.comaddress004
5222DerekJonesemail4@test.comaddress005

 

Table 2

 

ID NumFirst nameLast nameEmailAddressQualificationQual acquireQual expired
1PaulSmithpaulsmith@test.comaddress001type1 Level 14/08/201531/12/2018
1PaulSmithpaulsmith@test.comaddress001type2 Level 28/06/201631/12/2019
1PaulSmithpaulsmith@test.comaddress001type3 level 313/04/201731/12/2020
2John Doeemail2@test.comaddress002type4 level 116/02/201831/12/2021
2John Doeemail2@test.comaddress002type 2 level 322/12/201831/12/2021
3JaneSmithemail3@test.comaddress003type 1 level 427/10/201431/12/2019
3JaneSmithemail3@test.comaddress003type 3 level 26/07/201731/12/2020
3JaneSmithemail3@test.comaddress003type 2 level 131/08/201831/12/2021
4TonyJohnsonemail3@test.comaddress004type 2 level 411/05/201331/12/2018
5DerekJonesemail4@test.comaddress005type 1 level 416/03/201231/12/2018

  

Table 3

 

ID NumID2 NumFirst NameLast NameEmailAddressQual type 1Qual type 1 acquireQual type 1 acquireQual type 2Qual type 2 acquireQual type 2 acquireQual type 3Qual type 3 acquireQual type 3 acquireQual type 4Qual type 4 acquireQual type 4 acquire
1444PaulSmithpaulsmith@test.comaddress001type1 Level 14/08/201531/12/2018type2 Level 28/06/201631/12/2019type3 level 313/04/201731/12/2020   
2555John Doeemail2@test.comaddress002  type 2 level 322/12/201831/12/2021   type4 level 116/02/2018########
3333JaneSmithemail3@test.comaddress003type 1 level 427/10/201431/12/2019type 2 level 131/08/201831/12/2021type 3 level 26/07/201731/12/2020   
4111TonyJohnsonemail3@test.comaddress004  type 2 level 411/05/201331/12/2018      
5222DerekJonesemail4@test.comaddress005type 1 level 416/03/201231/12/2018         


I have been able to merge the tables but not able to then merge rows with identical ID Nums. Further, I cannot work out how I can have the query look through the qualification column and work out which column the qualification should be in the merged table. 

 

Thank you for any assistance in advanced. 

2 ACCEPTED SOLUTIONS
PattemManohar
Community Champion
Community Champion

@adamvainauskas If you are sure that the number of qualifications will not be more than 4, then you can achieve this by using below DAX as "New Table" (Ofcourse, you can achieve this through Power Query as well with multiple steps of pivot, unpivot and merge...) But it will be a straight forward in DAX assuming the max qualifications will not exceed 4 without having any additional transformations.

 

We are here adding additional fields to the existing table (Table 1 in your case) and result into a new table.

 

Test113Out = 

ADDCOLUMNS(Test113PQ1,
          "Qual Type 1",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 1"),"Qual",[Qualification]),
          "Qual Type 1 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 1"),"QualAcquire",[QualAcquired]),
          "Qual Type 1 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 1"),"QualExpire",[QualExpired]),

          "Qual Type 2",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 2"),"Qual",[Qualification]),
          "Qual Type 2 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 2"),"QualAcquire",[QualAcquired]),
          "Qual Type 2 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 2"),"QualExpire",[QualExpired]),

          "Qual Type 3",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 3"),"Qual",[Qualification]),
          "Qual Type 3 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 3"),"QualAcquire",[QualAcquired]),
          "Qual Type 3 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 3"),"QualExpire",[QualExpired]),

          "Qual Type 4",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 4"),"Qual",[Qualification]),
          "Qual Type 4 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 4"),"QualAcquire",[QualAcquired]),
          "Qual Type 4 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 4"),"QualExpire",[QualExpired])
          )

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




View solution in original post

v-jiascu-msft
Employee
Employee

Hi @adamvainauskas,

 

Here is the solution of Power Query (M). Please download the demo in the attachment. Please note: many steps can be done through the UI of Query Editor.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZPLTsMwEEV/xcq6ws82YQdSQKKqEAh2URdWO6gWeREniP49M6kDURtYtKuZayvn2jfjLIseUvbYFdEsuneNb1lpC0Cxsr/9XWFdjvV2u23Ae+yeO5u7N7exravKoJndfHSugUHCV41qG61nWSRx8cl2BHkpXLvDWqP01N+04NurTUVHsAcHIeiDdl+DZCv4hJyRNlwkXAk5x15LLhWJ5DK+CnyFOuFiQcjFmH99GV+zvOdr1FJzYYgZjwyU6A3If1ntSoY1rShDoNDVJFwFuAlwMpMLLkIgI7g8H87U6OhK/cR9Qqf9pS1hlE2P15N4PeBlwBvCx1wKwpvT6M/ED8HTdTCa+I/cz6SrUfJIPMzlRDZ0n9eq3Icf4PvH8o+BOTagBYkGczLQp4NPbyGFBt57hxL8wDeT/PlU+jQ6mpDqiL/+Bg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t, #"(blank).7" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}, {"(blank).5", type text}, {"(blank).6", type text}, {"(blank).7", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID Num", Int64.Type}, {"First name", type text}, {"Last name", type text}, {"Email", type text}, {"Address", type text}, {"Qualification", type text}, {"Qual acquire", type text}, {"Qual expired", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 0, 1),
    #"Added Custom1" = Table.AddColumn(#"Added Index", "Custom", each let currentID = [ID Num], currentIndex = [Index] in List.Accumulate(Table.SelectRows(#"Added Index", each [ID Num] = currentID and  [Index] <=currentIndex)[Index],  0, (state, current) =>  state + 1)),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Added Custom1", {"Qual expired", "Qual acquire", "Qualification"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom.1", each [Attribute] & " " & Text.From([Custom])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Custom", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom.1]), "Custom.1", "Value")
in
    #"Pivoted Column"

 

Best Regards,
Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-jiascu-msft
Employee
Employee

Hi @adamvainauskas,

 

Here is the solution of Power Query (M). Please download the demo in the attachment. Please note: many steps can be done through the UI of Query Editor.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZPLTsMwEEV/xcq6ws82YQdSQKKqEAh2URdWO6gWeREniP49M6kDURtYtKuZayvn2jfjLIseUvbYFdEsuneNb1lpC0Cxsr/9XWFdjvV2u23Ae+yeO5u7N7exravKoJndfHSugUHCV41qG61nWSRx8cl2BHkpXLvDWqP01N+04NurTUVHsAcHIeiDdl+DZCv4hJyRNlwkXAk5x15LLhWJ5DK+CnyFOuFiQcjFmH99GV+zvOdr1FJzYYgZjwyU6A3If1ntSoY1rShDoNDVJFwFuAlwMpMLLkIgI7g8H87U6OhK/cR9Qqf9pS1hlE2P15N4PeBlwBvCx1wKwpvT6M/ED8HTdTCa+I/cz6SrUfJIPMzlRDZ0n9eq3Icf4PvH8o+BOTagBYkGczLQp4NPbyGFBt57hxL8wDeT/PlU+jQ6mpDqiL/+Bg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t, #"(blank).6" = _t, #"(blank).7" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}, {"(blank).5", type text}, {"(blank).6", type text}, {"(blank).7", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID Num", Int64.Type}, {"First name", type text}, {"Last name", type text}, {"Email", type text}, {"Address", type text}, {"Qualification", type text}, {"Qual acquire", type text}, {"Qual expired", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 0, 1),
    #"Added Custom1" = Table.AddColumn(#"Added Index", "Custom", each let currentID = [ID Num], currentIndex = [Index] in List.Accumulate(Table.SelectRows(#"Added Index", each [ID Num] = currentID and  [Index] <=currentIndex)[Index],  0, (state, current) =>  state + 1)),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Added Custom1", {"Qual expired", "Qual acquire", "Qualification"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Only Selected Columns", "Custom.1", each [Attribute] & " " & Text.From([Custom])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Custom", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom.1]), "Custom.1", "Value")
in
    #"Pivoted Column"

 

Best Regards,
Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
PattemManohar
Community Champion
Community Champion

@adamvainauskas If you are sure that the number of qualifications will not be more than 4, then you can achieve this by using below DAX as "New Table" (Ofcourse, you can achieve this through Power Query as well with multiple steps of pivot, unpivot and merge...) But it will be a straight forward in DAX assuming the max qualifications will not exceed 4 without having any additional transformations.

 

We are here adding additional fields to the existing table (Table 1 in your case) and result into a new table.

 

Test113Out = 

ADDCOLUMNS(Test113PQ1,
          "Qual Type 1",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 1"),"Qual",[Qualification]),
          "Qual Type 1 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 1"),"QualAcquire",[QualAcquired]),
          "Qual Type 1 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 1"),"QualExpire",[QualExpired]),

          "Qual Type 2",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 2"),"Qual",[Qualification]),
          "Qual Type 2 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 2"),"QualAcquire",[QualAcquired]),
          "Qual Type 2 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 2"),"QualExpire",[QualExpired]),

          "Qual Type 3",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 3"),"Qual",[Qualification]),
          "Qual Type 3 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 3"),"QualAcquire",[QualAcquired]),
          "Qual Type 3 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 3"),"QualExpire",[QualExpired]),

          "Qual Type 4",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 4"),"Qual",[Qualification]),
          "Qual Type 4 Acquire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 4"),"QualAcquire",[QualAcquired]),
          "Qual Type 4 Expire",SELECTCOLUMNS(FILTER(Test113PQ2,Test113PQ1[IDNum]=Test113PQ2[IDNum] && LEFT(Test113PQ2[Qualification],6) = "type 4"),"QualExpire",[QualExpired])
          )

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




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.