Forum Discussion

kleetus51's avatar
kleetus51
Helper I
4 years ago
Solved

Delete all columns between two columns

I have a dataset that fluctuates in the number of columns. I need to do some transformations where I delete all the columns between the columns "Key" and "Config" columns, including the Config column. The number of columns between will fluctuate in name and number and there will be a variable number of columns after "Config". Here are some examples. Any ideas?

 

This:

KeyOrder #ReferenceConfigThicknessWidthLength
11234xyz2306823068
24567abc1.528701.52870
38910def2246822468
42345ghi1.37530681.3753068

Would change to this:

KeyThicknessWidthLength
123068
21.52870
322468
41.3753068

 

This:

KeyOrder #ConfigThicknessWidthLengthTransfer
112342306823068TRUE
245671.528701.52870FALSE
389102246822468FALSE
423451.37530681.3753068TRUE

Would change to this:

KeyThicknessWidthLengthTransfer
123068TRUE
21.52870FALSE
322468FALSE
41.3753068TRUE
  • Hi, kleetus51 ;

    You could compete it in power query.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TU5JEoAwCPtLzx2nBbr4lk4P7voD9fUCOuoBkgwkUIrxxhoPSAz7cXIHdDELcqHjxqraoppCTAxdP4irCZCTu5kYxMValpFpbr0Mx2mWIdCbCvSlkl4k8S/rplmYwvOC8v8b9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, #"Order #" = _t, Reference = _t, Config = _t, Thickness = _t, Width = _t, Length = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"Order #", Int64.Type}, {"Reference", type text}, {"Config", type number}, {"Thickness", type number}, {"Width", Int64.Type}, {"Length", Int64.Type}}),
    a = Table.ColumnNames(#"Changed Type"),
    start=List.PositionOf(a,"Key"),
    end=List.PositionOf(a,"Config"),
    #"Demoted Headers" = Table.DemoteHeaders(#"Changed Type"),
    T1=Table.ToColumns( #"Demoted Headers"),
    select1=List.Range(T1, start,1),
    select2=List.Range(T1, end),
    result=Table.FromColumns(List.Union({select1,select2})),
    #"Promoted Headers" = Table.PromoteHeaders(result, [PromoteAllScalars=true])
    in
        #"Promoted Headers"

    The final output is shown below:

     


    Best Regards,
    Community Support Team_ Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

    • kleetus51's avatar
      kleetus51
      Helper I

      The number and names of the columns that need to be deleted will vary. I need a solution that says "delete all columns between "Key" and "Config", regardless of their name and number of columns between them.

  • v-yalanwu-msft's avatar
    v-yalanwu-msft
    Community Support

    Hi, kleetus51 ;

    You could compete it in power query.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TU5JEoAwCPtLzx2nBbr4lk4P7voD9fUCOuoBkgwkUIrxxhoPSAz7cXIHdDELcqHjxqraoppCTAxdP4irCZCTu5kYxMValpFpbr0Mx2mWIdCbCvSlkl4k8S/rplmYwvOC8v8b9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, #"Order #" = _t, Reference = _t, Config = _t, Thickness = _t, Width = _t, Length = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"Order #", Int64.Type}, {"Reference", type text}, {"Config", type number}, {"Thickness", type number}, {"Width", Int64.Type}, {"Length", Int64.Type}}),
    a = Table.ColumnNames(#"Changed Type"),
    start=List.PositionOf(a,"Key"),
    end=List.PositionOf(a,"Config"),
    #"Demoted Headers" = Table.DemoteHeaders(#"Changed Type"),
    T1=Table.ToColumns( #"Demoted Headers"),
    select1=List.Range(T1, start,1),
    select2=List.Range(T1, end),
    result=Table.FromColumns(List.Union({select1,select2})),
    #"Promoted Headers" = Table.PromoteHeaders(result, [PromoteAllScalars=true])
    in
        #"Promoted Headers"

    The final output is shown below:

     


    Best Regards,
    Community Support Team_ Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.