Forum Discussion
Dynamically remov columns in power query
- 4 years ago
Hello, this is the full Function which i have been using in order to reach the wanted behavior :
(table as table, searchText as text) as list =>
let
ColumnNameList = Table.ColumnNames(table),
CleanColumns = List.Transform(ColumnNameList,(x)=> Text.BeforeDelimiter(x,"_")),
ColPositionOne = List.PositionOf(CleanColumns, searchText),
ColPositionTwo = List.PositionOf(List.Range(CleanColumns,ColPositionOne+1),searchText),
ColsToRemove = List.Range(ColumnNameList,ColPositionOne,ColPositionTwo+2),
ResultList = {ColPositionOne,ColPositionTwo}
in
ColsToRemoveI am calling this Function when i want ot delete the columns which are placed BETWEEN the Column named "searchText" - which means in my case this column named "searchText" has to appear twice within my table!
For sure, we could exten the function with searchText1 and searchText2 in order to dynamically delete columns placed between those 2 columns which would be even easier because in this case i would not need to create "CleanColumns" since PowerQuery is automatically renaming the second column named "searchText" into searchText_ColID since two columns must not have the same Header Name.
Finally as i said the function is called by
= Table.RemoveColumns(#"Promoted Headers", fxGetColumnsToRemove(#"Promoted Headers","ColumnName"))
Hope this helps somebody else.
Hello, this is the full Function which i have been using in order to reach the wanted behavior :
(table as table, searchText as text) as list =>
let
ColumnNameList = Table.ColumnNames(table),
CleanColumns = List.Transform(ColumnNameList,(x)=> Text.BeforeDelimiter(x,"_")),
ColPositionOne = List.PositionOf(CleanColumns, searchText),
ColPositionTwo = List.PositionOf(List.Range(CleanColumns,ColPositionOne+1),searchText),
ColsToRemove = List.Range(ColumnNameList,ColPositionOne,ColPositionTwo+2),
ResultList = {ColPositionOne,ColPositionTwo}
in
ColsToRemove
I am calling this Function when i want ot delete the columns which are placed BETWEEN the Column named "searchText" - which means in my case this column named "searchText" has to appear twice within my table!
For sure, we could exten the function with searchText1 and searchText2 in order to dynamically delete columns placed between those 2 columns which would be even easier because in this case i would not need to create "CleanColumns" since PowerQuery is automatically renaming the second column named "searchText" into searchText_ColID since two columns must not have the same Header Name.
Finally as i said the function is called by
= Table.RemoveColumns(#"Promoted Headers", fxGetColumnsToRemove(#"Promoted Headers","ColumnName"))
Hope this helps somebody else.