Forum Discussion

psvdr's avatar
psvdr
Frequent Visitor
2 years ago
Solved

collect data from a column that changes position

Dear All,   Asking for your kind help. I'd like to collect values from a column (named strawberry) that is missaligned (first, it is in the 2nd column, than it is in the 3rd column and than in the ...
  • dufoq3's avatar
    2 years ago

    Hi psvdr, another solution here:

     

    Result

     

    v1 (group transformation based on List.PositionOf)

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WykstV9JRyixJzS0G0hmJZalAKrGoKLMsNUUpVidaKbGgIAckVlxSlFielFpUVAnkKIAxSNoEyDJDETEEsoxQRIyBLEMUEaJtzS9KzEvHYj2yRYZwESOoiDEFFhWkJhah2ofsBVMwjW67CVzEGCpqDncPLICMlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
        GroupedRows = Table.Group(Source, {"Column1"}, {{"All", each 
            [ a = List.Transform(Table.ToColumns(_), (x)=> List.Contains(x, "strawberry")),
              b = List.PositionOf(a, true),
              c = Table.AddColumn(_, "strawberry", (x)=> if x[Column1] = "new" then null else Record.ToList(x){b}, type text)
            ][c], type table}},
        GroupKind.Local, (s,c)=> Byte.From(c[Column1] = "new") ),
        CombinedAll = Table.Combine(GroupedRows[All])
    in
        CombinedAll

     

     

    v2 (group transformation based on List.TransformMany)

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WykstV9JRyixJzS0G0hmJZalAKrGoKLMsNUUpVidaKbGgIAckVlxSlFielFpUVAnkKIAxSNoEyDJDETEEsoxQRIyBLEMUEaJtzS9KzEvHYj2yRYZwESOoiDEFFhWkJhah2ofsBVMwjW67CVzEGCpqDncPLICMlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
        AddedIndex = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
        Ad_GroupHelper = Table.AddColumn(AddedIndex, "GroupHelper", each if [Column1] = "new" then [Index] else null, Int64.Type),
        FilledDown = Table.FillDown(Ad_GroupHelper,{"GroupHelper"}),
        GroupedRows = Table.Group(FilledDown, {"GroupHelper"}, {{"All", each 
            [ a = Table.ToColumns(_),
              b = List.TransformMany(a,
                    (y)=> {List.Contains(y, "strawberry")},
                    (x,y)=> List.Select(x, (w)=> y) ),
              c = {null} & List.Skip(List.Combine(b)),
              d = Table.FromColumns(a & {c})
            ][d], type table}}),
        CombinedAll = Table.Combine(GroupedRows[All]),
        RemovedColumns = Table.RemoveColumns(CombinedAll,{"Column5", "Column6"})
    in
        RemovedColumns