Forum Discussion

davidgaribaldi's avatar
6 years ago
Solved

Consolidate rows based on primary key

I'm not sure if I know the correct way to ask this so I'll try to just show it.

1Health
1Nutrition
1Orientation
2Health
2Nutrition
3Nutrition
3Orientation

 

Is there a way for me to create a column for each data point in column 2 or is there a better way to go about this? I want to consolidate so that I can end up with 1 row for each key.

 

1HealthNutritionOrientation
2HealthNurtition 
3 NutritionOrientation

 

Thanks a lot!

  • Hi davidgaribaldi 

     

    Please use the below m code. 

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJITcwpyVCK1YFw/UpLijJLMvPz4CL+RZmpeSWJcDEjVE1GGJqMsYqgGBMLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Column2] = "Health" then 1 else if [Column2] = "Nutrition" then 2 else 3),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-US")[Custom]), "Custom", "Column2")
    in
        #"Pivoted Column"

     

    Pbix attached. 

     

  • That didn't do anything for me but I did figure it out perhaps the longest way possible but it worked none the less.

     

    I grouped by primary key then created lists for each column using Table.Column. Then created an if statement to count how many nulls were in each row so I could filter by the least amount of nulls per primary key. Then I grouped by the minimum nulls and filtered out the rest.

     

    So probably way more complex than it needed to be but like I said it worked. ¯\_(ツ)_/¯

9 Replies

  • Hi,

    This M code works

    let
        Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Number", Int64.Type}, {"Value", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Value]="Health" then 1 else if [Value]="Nutrition" then 2 else 3),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-IN"), List.Distinct(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-IN")[Custom]), "Custom", "Value")
    in
        #"Pivoted Column"

    • davidgaribaldi's avatar
      davidgaribaldi
      Icon for Helper I rankHelper I

      Ashish_Mathur, I think this is close but I'm still getting duplicate primary keys.

      1Healthnullnull
      1nullNutritionnull
      2nullNutritionnull
      2nullnullOrientation

       

      rather than

      1HealthNutritionnull
      2nullNutritionOrientation
      • mwegener's avatar
        mwegener
        Icon for Most Valuable Professional rankMost Valuable Professional

        Hi davidgaribaldi ,

         

        check the aggregation at your pivot step. It should be max.

         

         

        Regards,

        Marcus

        Dortmund - Germany
        If I answered your question, please mark my post as solution, this will also help others.
        Please give Kudos for support.

  • mwegener's avatar
    mwegener
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hi davidgaribaldi ,

     

    just as an idea. Add a column of values and pivot the two columns.

    If I answered your question, please mark my post as solution, this will also help others.

    Please give Kudos for support.

  • v-diye-msft's avatar
    v-diye-msft
    Icon for Community Support rankCommunity Support

    Hi davidgaribaldi 

     

    Please use the below m code. 

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJITcwpyVCK1YFw/UpLijJLMvPz4CL+RZmpeSWJcDEjVE1GGJqMsYqgGBMLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Column2] = "Health" then 1 else if [Column2] = "Nutrition" then 2 else 3),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-US")[Custom]), "Custom", "Column2")
    in
        #"Pivoted Column"

     

    Pbix attached. 

     

  • mwegener's avatar
    mwegener
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hi davidgaribaldi,

     

    has your question been answered?

     

    Regards,

    Marcus

    Dortmund - Germany
    If I answered your question, please mark my post as solution, this will also help others.
    Please give Kudos for support.