Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Group by and Concatenate Rows for Multiple Columns with Power Query

I have a table where my data is split by an ID, just like the example below. ID Var1 Var2 Var3 1    A A    A    1    B    B    B    1    C    C    C    2    D    D    ...
  • AntrikshSharma's avatar
    3 years ago

    Anonymous You can use this:

    let
        Source = Table.FromRows (
            Json.Document (
                Binary.Decompress (
                    Binary.FromText (
                        "i45WMlTSUXKE41gdiIgTHMNEnOEYJGIEZLnAMUzEFY5jYwE=",
                        BinaryEncoding.Base64
                    ),
                    Compression.Deflate
                )
            ),
            let
                _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
            in
                type table [ ID = _t, Var1 = _t, Var2 = _t, Var3 = _t ]
        ),
        ChangedType = Table.TransformColumnTypes (
            Source,
            {
                { "ID", Int64.Type },
                { "Var1", type text },
                { "Var2", type text },
                { "Var3", type text }
            }
        ),
        Group = Table.Group (
            ChangedType,
            { "ID" },
            {
                {
                    "Transformation",
                    ( CurrentGroup ) =>
                        let
                            DataCols    = Table.RemoveColumns ( CurrentGroup, "ID" ),
                            ColumnNames = Table.ColumnNames ( DataCols ),
                            ColumnsList = Table.ToColumns ( DataCols ),
                            Combine     = List.Transform ( ColumnsList, each Text.Combine ( _, ", " ) ),
                            Result      = Table.FromRows ( { Combine }, ColumnNames )
                        in
                            Result,
                    type table [
                        ID = nullable number,
                        Var1 = nullable text,
                        Var2 = nullable text,
                        Var3 = nullable text
                    ]
                }
            }
        ),
        ExpandedTransformation = Table.ExpandTableColumn (
            Group,
            "Transformation",
            { "Var1", "Var2", "Var3" },
            { "Var1", "Var2", "Var3" }
        )
    in
        ExpandedTransformation