Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Create a new column based on distinct Columns

My basic idea is to bring up a line chart by grouping by multiple columns. Below I have given a simple table.

 

Time_minsResult_typeStability_timeAnalysis_typeAvg - Dissolved Uncorrected
10Individual manual56.15714286
5Individual manual46.91190476
10Individual fully automated72.45590551
5Individual fully automated58.72992126
10Individual4 weekfully automated88.65853659
5Individual4 weekfully automated83.52439024
8Individual semi-automated79.18888889

 

 

Now I need to create a index column based on distinct values of Stability_time and Analysis_type, just like below.

 

 

Time_minsResult_typeStability_timeAnalysis_typeAvg - Dissolved UncorrectedCustom
10Individual manual56.157142861
5Individual manual46.911904761
10Individual fully automated72.455905512
5Individual fully automated58.729921262
10Individual4 weekfully automated88.658536593
5Individual4 weekfully automated83.524390243
8Individual semi-automated79.188888894

 

Please help me here. Thanks in Advance.

  • Anonymous 

    Paste below code ina blank query in the ADvanced Editor and follow steps:

     





    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY/BCoMwDIZfRXp2oYlJ2zzCnkE8FOxApu4w3djbr+hprBrI4Q//ny9pW4PW1OY698Nr6Nc4ZlHlnuK8C3GA4pEpONPVrZFTNztQRLXsd3d5+W0dx08V1+UxxSX1eeIJWEStCB5T/mMSwJMqIR3huHqndC+GQwAnQRonWmaeZRsQ4kYt8ZYNpXufaRouP18qYNgqE7sv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time_mins = _t, Result_type = _t, Stability_time = _t, Analysis_type = _t, #"Avg - Dissolved Uncorrected" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time_mins", Int64.Type}, {"Avg - Dissolved Uncorrected", type number}}),
        #"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({[Stability_time], [Analysis_type]}, "|"), type text),
        #"Grouped Rows" = Table.Group(#"Inserted Merged Column", {"Merged"}, {{"Count", each _, type table [Time_mins=nullable number, Result_type=nullable text, Stability_time=nullable text, Analysis_type=nullable text, #"Avg - Dissolved Uncorrected"=nullable number, Merged=text]}}),
        #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1, Int64.Type),
        #"Removed Columns" = Table.RemoveColumns(#"Added Index",{"Merged"}),
        #"Expanded Count" = Table.ExpandTableColumn(#"Removed Columns", "Count", {"Time_mins", "Result_type", "Stability_time", "Analysis_type", "Avg - Dissolved Uncorrected"}, {"Time_mins", "Result_type", "Stability_time", "Analysis_type", "Avg - Dissolved Uncorrected"})
    in
        #"Expanded Count"

    ________________________

    Did I answer your question? Mark this post as a solution, this will help others!.

    Click on the Thumbs-Up icon if you like this reply 🙂

    YouTube, LinkedIn

     

4 Replies

  • vanessafvg's avatar
    vanessafvg
    Icon for Community Champion rankCommunity Champion

    in power query you can duplicate the data set, then do a group by on the relevant columns, and then add an identity column. like this, see attached.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for the reply, But I need it exactly as the table I mentioned above. From the example you gave the table again got shrunk on grouping by rows, which will not be helpful for my manipulations.

  • Anonymous 

    Paste below code ina blank query in the ADvanced Editor and follow steps:

     





    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY/BCoMwDIZfRXp2oYlJ2zzCnkE8FOxApu4w3djbr+hprBrI4Q//ny9pW4PW1OY698Nr6Nc4ZlHlnuK8C3GA4pEpONPVrZFTNztQRLXsd3d5+W0dx08V1+UxxSX1eeIJWEStCB5T/mMSwJMqIR3huHqndC+GQwAnQRonWmaeZRsQ4kYt8ZYNpXufaRouP18qYNgqE7sv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time_mins = _t, Result_type = _t, Stability_time = _t, Analysis_type = _t, #"Avg - Dissolved Uncorrected" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time_mins", Int64.Type}, {"Avg - Dissolved Uncorrected", type number}}),
        #"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({[Stability_time], [Analysis_type]}, "|"), type text),
        #"Grouped Rows" = Table.Group(#"Inserted Merged Column", {"Merged"}, {{"Count", each _, type table [Time_mins=nullable number, Result_type=nullable text, Stability_time=nullable text, Analysis_type=nullable text, #"Avg - Dissolved Uncorrected"=nullable number, Merged=text]}}),
        #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1, Int64.Type),
        #"Removed Columns" = Table.RemoveColumns(#"Added Index",{"Merged"}),
        #"Expanded Count" = Table.ExpandTableColumn(#"Removed Columns", "Count", {"Time_mins", "Result_type", "Stability_time", "Analysis_type", "Avg - Dissolved Uncorrected"}, {"Time_mins", "Result_type", "Stability_time", "Analysis_type", "Avg - Dissolved Uncorrected"})
    in
        #"Expanded Count"

    ________________________

    Did I answer your question? Mark this post as a solution, this will help others!.

    Click on the Thumbs-Up icon if you like this reply 🙂

    YouTube, LinkedIn

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for your help, Works exactly as expected. ðŸ˜Š