Forum Discussion

adamvainauskas's avatar
adamvainauskas
New Member
7 years ago
Solved

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
1444PaulSmith[email protected]address001
2555John Doe[email protected]address002
3333JaneSmith[email protected]address003
4111TonyJohnson[email protected]address004
5222DerekJones[email protected]address005

 

Table 2

 

ID NumFirst nameLast nameEmailAddressQualificationQual acquireQual expired
1PaulSmith[email protected]address001type1 Level 14/08/201531/12/2018
1PaulSmith[email protected]address001type2 Level 28/06/201631/12/2019
1PaulSmith[email protected]address001type3 level 313/04/201731/12/2020
2John Doe[email protected]address002type4 level 116/02/201831/12/2021
2John Doe[email protected]address002type 2 level 322/12/201831/12/2021
3JaneSmith[email protected]address003type 1 level 427/10/201431/12/2019
3JaneSmith[email protected]address003type 3 level 26/07/201731/12/2020
3JaneSmith[email protected]address003type 2 level 131/08/201831/12/2021
4TonyJohnson[email protected]address004type 2 level 411/05/201331/12/2018
5DerekJones[email protected]address005type 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
1444PaulSmith[email protected]address001type1 Level 14/08/201531/12/2018type2 Level 28/06/201631/12/2019type3 level 313/04/201731/12/2020   
2555John Doe[email protected]address002  type 2 level 322/12/201831/12/2021   type4 level 116/02/2018########
3333JaneSmith[email protected]address003type 1 level 427/10/201431/12/2019type 2 level 131/08/201831/12/2021type 3 level 26/07/201731/12/2020   
4111TonyJohnson[email protected]address004  type 2 level 411/05/201331/12/2018      
5222DerekJones[email protected]address005type 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. 

  • 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])
              )

  • 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

2 Replies

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft 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

  • PattemManohar's avatar
    PattemManohar
    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])
              )