Forum Discussion
Remove columns containing a certain value
- 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
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.
- tc5pt7 years agoRegular 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.
- Nolock7 years agoResident 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.
- tc5pt7 years agoRegular Visitor
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]) in #"Promoted Headers"This is what is currently in my advanced editor. It also Changed Type by default but I deleted that step because there were over 100 columns and it created a huge block of code.
How would I inject the code you gave?