Forum Discussion
tc5pt
7 years agoRegular Visitor
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...
- 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
tc5pt
7 years agoRegular Visitor
Thank you. I will try this out. Do I need to be replacing certain words in there, like "source"?
I am a total noob so I apologize if it's obvious.
Thank you Nolock
Nolock
7 years agoResident Rockstar
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