Forum Discussion

tc5pt's avatar
tc5pt
Regular Visitor
7 years ago
Solved

Remove columns containing a certain value

I have various columns that have a date listed in one row, therefore they contain a "/". None of the other columns I need contain a backslash in any of the rows. I would like to delete the columns co...
  • Nolock's avatar
    Nolock
    7 years ago

    Hi tc5pt,

    I've tried to integrate it into the code you mentioned earlier. Just replace all occurrences of "Source" with #"Promoted Headers" and it should work in your Excel file.

     

    let
        Source = Excel.Workbook(File.Contents("C: Source"), null, true),
        #"1_Sheet" = Source{[Item="1",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(#"1_Sheet", [PromoteAllScalars=true]),
    
    // get columns which contains any slash among values
        ColumnsToRemove = 
            List.Select(
                // get a list of all columns
                Table.ColumnNames(#"Promoted Headers"),
                (columnName) =>
                    let
                        // get all values of a columns
                        ColumnValues = Table.Column(#"Promoted Headers", columnName),
                        // go through values and stop when you find the first occurence of a text containing a slash
                        // if there is a value with a slash, return true else false
                        ContainsSlash = List.AnyTrue(List.Transform(ColumnValues, each Text.Contains(_, "/")))
                    in
                        ContainsSlash
            ),
        // remove columns
        Result = Table.RemoveColumns(#"Promoted Headers", ColumnsToRemove)
    in
        Result