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
Nolock
7 years agoResident Rockstar
I'm very sorry, I unterstood the task wrong at the beginning.
I've written another code which removes columns that have at least one value containing a slash.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUrUB5FJQKwUqxMNZoBwsn4KTCgZxNBRMtQ3AovEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
// get columns which contains at least one value which contains a slash
ColumnsToRemove =
List.Select(
// get a list of all columns of the table Source
Table.ColumnNames(Source),
(columnName) =>
let
// get all values of a column
ColumnValues = Table.Column(Source, 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(Source, ColumnsToRemove)
in
Resulttc5pt
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
- Nolock7 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