Forum Discussion

VRMENDESadq's avatar
VRMENDESadq
Frequent Visitor
1 year ago
Solved

Return all values ​​appearing in a given column

HI, I need to create a custom column that shows all the values ​​in the "Origem" column according to the value in the "Produto" column, for example: 

 

ProdutoOrigemCustom Colunm
1AA,B
1BA,B
2AA,B,C
2BA,B,C
2CA,B,C
3AA,B
3BA,B
4AA

 

Anybody can help me?

 

Tks.

  • Anonymous's avatar
    Anonymous
    1 year ago

    Firstly, I duplicated the Table and named it Table2.

     

    In Table, this is the M-Code to transform the column to the custom column that you want:

    = Table.Group(Source, {"Produto"}, {{"Custom Column", each Text.Combine([Origem], ";"), type text}})

    This is the result:

    Then I use merge function in PQ to add custom column to Table2 to get the final result.

     

  • Ahmedx's avatar
    Ahmedx
    1 year ago

    pls try again

    let
    
    
    lst = List.Buffer( Table.ToList( from,(x)=>x)),
    f= (w)=>  Text.Combine( List.Transform(List.PositionOf( List.Transform(lst,(z)=>z{0}),w,Occurrence.All), (x)=> lst{x}{1}?),", "),
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWBsJzALCO4mBGKmDOYZQyXNYbLmkDEYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Produto = _t, Origem = _t]),
        from = Table.TransformColumnTypes(Source,{{"Produto", Int64.Type}, {"Origem", type text}}),
        #"Added Custom1" = Table.AddColumn(from, "Custom", each f([Produto]))
    in
        #"Added Custom1"

11 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    VRMENDESadq Try:

    Column =
      VAR __Produto = [Produto]
      VAR __Table = FILTER( 'Table', [Produto] = __Produto )
      VAR __Return = CONCATENATEX( __Table, [Origem], "," )
    RETURN
      __Return
    
    
    Measure =
      VAR __Produto = MAX([Produto])
      VAR __Table = FILTER( ALLSELECTED('Table'), [Produto] = __Produto )
      VAR __Return = CONCATENATEX( __Table, [Origem], "," )
    RETURN
      __Return
    
  • Anonymous's avatar
    Anonymous
    Not applicable

    Firstly, I duplicated the Table and named it Table2.

     

    In Table, this is the M-Code to transform the column to the custom column that you want:

    = Table.Group(Source, {"Produto"}, {{"Custom Column", each Text.Combine([Origem], ";"), type text}})

    This is the result:

    Then I use merge function in PQ to add custom column to Table2 to get the final result.

     

    • VRMENDESadq's avatar
      VRMENDESadq
      Frequent Visitor

      Hi Anonymous , this step doesn't work: 

      Table.Group(Source, {"Produto"}, {{"Custom Column", each Text.Combine([Origem], ";"), type text}})

      when I add the custom colunm and put this code, PQ returns: Expression.Error: The name 'Source' was not recognized. Make sure it is spelled correctly.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi,

        The first parameter is the table name. Here, it refers to the name of the previous step. For me, my previous step is called "Source". You can check yours on the right side of your screen. The steps are cut off from my screenshots.

  • pls try code in M

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWBsJzALCO4mBGKmDOYZQyXNYbLmkDEYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Produto = _t, Origem = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Produto", Int64.Type}, {"Origem", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Combine( Table.SelectRows( #"Changed Type",(x)=>x[Produto]=[Produto])[Origem],","))
    in
        #"Added Custom"
    • VRMENDESadq's avatar
      VRMENDESadq
      Frequent Visitor

      Hi Ahmedx 

      Its work, but in example just have a little piece of data in colunm Produto e Origem, do you know any code that I can apply  this logic in a column that has a lot of data?

       

      Thanks.

      • Ahmedx's avatar
        Ahmedx
        Super User

        pls try again

        let
        
        
        lst = List.Buffer( Table.ToList( from,(x)=>x)),
        f= (w)=>  Text.Combine( List.Transform(List.PositionOf( List.Transform(lst,(z)=>z{0}),w,Occurrence.All), (x)=> lst{x}{1}?),", "),
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWBsJzALCO4mBGKmDOYZQyXNYbLmkDEYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Produto = _t, Origem = _t]),
            from = Table.TransformColumnTypes(Source,{{"Produto", Int64.Type}, {"Origem", type text}}),
            #"Added Custom1" = Table.AddColumn(from, "Custom", each f([Produto]))
        in
            #"Added Custom1"