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 containing this backslash.

 

I saw a thread that propose the following formula but am having difficulty applying it to my query. It tells me "PreviousStepName not recognized". I replaced it with the name of the previous step in my query, but that didn't work. Am I supposed to be filling in values for "ColumnNames" and other parts of this formula as well? Or is this formula not accurate for what I am trying to achieve?

 

= let columnsToRemove = List.Select(Table.ColumnNames(PreviousStepName), each Text.StartsWith(_, "Var") or Text.Contains(_, "_prep")) in Table.RemoveColumns(PreviousStepName, columnsToRemove)

 

I also found a thread that suggested transposing my data and then deleting rows, but this was not a good solution for my data.

 

Thank you

  • 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

11 Replies

  • Nolock's avatar
    Nolock
    Resident Rockstar

    Hi tc5pt,

    I'm just guessing: You have to replace all occurancies of PreviousStepName. In your case you have to replace it twice.

     

    let 
        PreviousStepName = <THE NAME OF PREVIOUS STEP>,
        columnsToRemove = List.Select(Table.ColumnNames(PreviousStepName), each Text.StartsWith(_, "Var") or Text.Contains(_, "_prep")) 
    in 
        Table.RemoveColumns(PreviousStepName, columnsToRemove)

    But I suppose there can also be another problem because your code starts with "=". Please post the whole query to see that you use it correctly.

    • tc5pt's avatar
      tc5pt
      Regular Visitor

      Thanks for your response Nolock 

       

      Where exactly do I enter this code? In the Advanced Editor? When I do that it gives me either Token Comma or Token Eof expected. I am not sure how to input this code.

      • Nolock's avatar
        Nolock
        Resident Rockstar

        Hi tc5pt,

        jep, in the Advanced Editor. Please post your PowerQuery query (without data) and I will help you to integrate the code.

        The error message means that there is a comma missing or maybe one in the end of the code before the last IN or something similar.