cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

remove all columns in a set starting with "Column"

In Power Query I would like to remove all columns that do'nt have a header. So I tried to find out how to remove all columns starting with "Column" in the column name. I would like to do this dynamicly because my source files may a different ammount of collumns without headers. 
Can this be done in Power Query M script?

1 ACCEPTED SOLUTION
Greg_Deckler
Super User IV
Super User IV

Well, in theory you could use Table.ColumnNames to get a list of column names, do some text parsing with Text functions, essentially loop through each value in the Text list coming from ColumnNames and build up a new set that only includes the word Column in them and then feed in the resulting list into Table.RemoveColumns function.

 

First and last parts are easy. The tricky part is the middle part. I would use Text.Split to split it out to a list because there are a lot of list functions that you could use to potentially do what you want.

 

https://msdn.microsoft.com/en-us/library/mt211003.aspx

 


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
redzstarkiss
Regular Visitor

Usually I would transpose the columns.

Then under the 1st column, i will remove the 'null' values.

Then re-transpose the columns again before promoting headers.

jasonmaskell
Advocate II
Advocate II

This is how I solved it.

 

  1. Getting the table column names with Table.ColumnNames.
  2. Using List.FindText to find the desired string within those columns (e.g. "Column"), and return that as a list. 
  3. Returning a list with the difference between all the columns, and the desired columns using List.Difference.
  4. Using Table.RemoveColumns to remove columns not found in our list of desired columns.

Note: this returns all columns that contain the input string (e.g. "Column"), not just those starting with that string.

 

let
    Source = #"Query Containing Table",
    #"Column Names" = Table.ColumnNames(Source),
    #"Columns Containing String" = List.FindText(#"Column Names", "Column"),
    #"List Difference" = List.Difference(#"Column Names", #"Columns Containing String"),
    #"Remove Columns" = Table.RemoveColumns(Source, #"List Difference")
in
    #"Remove Columns"

 

Good solution! Alternatively, this code is simpler:

= Table.RemoveColumns(#"Last step",List.FindText(Table.ColumnNames(#"Last step"), "Column"))

 

Greg_Deckler
Super User IV
Super User IV

Well, in theory you could use Table.ColumnNames to get a list of column names, do some text parsing with Text functions, essentially loop through each value in the Text list coming from ColumnNames and build up a new set that only includes the word Column in them and then feed in the resulting list into Table.RemoveColumns function.

 

First and last parts are easy. The tricky part is the middle part. I would use Text.Split to split it out to a list because there are a lot of list functions that you could use to potentially do what you want.

 

https://msdn.microsoft.com/en-us/library/mt211003.aspx

 


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Helpful resources

Announcements
MBAS on Demand

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

July 2021 Update 768x460.png

Check it out!

Click here to read more about the July 2021 Updates

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Urdu Hindi D365 Bootcamp 768x460.png

Urdu Hindi D365 Bootcamp

Dont miss our very own April Dunnam’s The Developer Guide to the Galaxy! Find out what the Power Platform has to offer for the traditional developer.

Top Solution Authors
Top Kudoed Authors