Forum Discussion

AnthonyJoseph's avatar
AnthonyJoseph
Resolver III
3 years ago
Solved

Combine text ( without duplicating) based on another column

Hello Community,

 

I require some help in adding "Years in scope" column from power query.(in the screenshot below) Basically, I need a column that contains all the concatenated distinct year field values by client field. 

 

For example: 

1) Client 1 has data for the years 2020, 2021 and 2022 hence the concatenated column (Years in scope) has value "2020,2021,2022". (delimited with comma).

2) Client 2 has data for the years 2021 and 2022 hence the concatenated column has "2021,2022".

Hope this acheivable and would really appreciate any response or support.

 

Thanks,

AnthonyJoseph

  • Thank you ThxAlot . I was able to get the output however, is it possible to have them sorted to be in ascending order  For example: I am getting values like 2022,2020,2021 is there a way where I can get it in ascending order like 2020,2021,2022 other than by sorting the table by year?
    Thanks,

    AnthonyJoseph

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi AnthonyJoseph ,

     

    Make a little change to ThxAlot's code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDICUoYGBkqxOnAhCIUhZIwqZACkTKBCRgizTFFEQGrNgCKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Client = _t, Year = _t, Revenue = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client", Int64.Type}, {"Year", Int64.Type}, {"Revenue", Int64.Type}}),
        Grouped = Table.Group(Source, "Client", {"grp", each Table.AddColumn(_, "Yrs in scope", (r) => Text.Combine(List.Sort(List.Distinct([Year])),","))}),
        Table = Table.Combine(Grouped[grp])
    in
        Table

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

3 Replies

  •  

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDICUoYGBkqxOnAhCIUhZIwqZACkTKBCRgizTFFEQGrNgCKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Client = _t, Year = _t, Revenue = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client", Int64.Type}, {"Year", Int64.Type}, {"Revenue", Int64.Type}}),
        Grouped = Table.Group(Source, "Client", {"grp", each Table.AddColumn(_, "Yrs in scope", (r) => Text.Combine(List.Distinct([Year]),","))}),
        Table = Table.Combine(Grouped[grp])
    in
        Table

     

     

     

    A showcase of powerful Excel formulas,

    • AnthonyJoseph's avatar
      AnthonyJoseph
      Resolver III

      Thank you ThxAlot . I was able to get the output however, is it possible to have them sorted to be in ascending order  For example: I am getting values like 2022,2020,2021 is there a way where I can get it in ascending order like 2020,2021,2022 other than by sorting the table by year?
      Thanks,

      AnthonyJoseph

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi AnthonyJoseph ,

         

        Make a little change to ThxAlot's code:

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDICUoYGBkqxOnAhCIUhZIwqZACkTKBCRgizTFFEQGrNgCKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Client = _t, Year = _t, Revenue = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client", Int64.Type}, {"Year", Int64.Type}, {"Revenue", Int64.Type}}),
            Grouped = Table.Group(Source, "Client", {"grp", each Table.AddColumn(_, "Yrs in scope", (r) => Text.Combine(List.Sort(List.Distinct([Year])),","))}),
            Table = Table.Combine(Grouped[grp])
        in
            Table

        Best Regards,
        Gao

        Community Support Team

         

        If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

        How to get your questions answered quickly --  How to provide sample data in the Power BI Forum