• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

How to remove columns from list|Power Query|Excel Forum|My Online Training Hub

You are here: Home / How to remove columns from list|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryHow to remove columns from list
sp_PrintTopic sp_TopicIcon
How to remove columns from list
Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
1
June 4, 2021 - 8:50 pm
sp_Permalink sp_Print sp_EditHistory

Hi,

i have 2 tables:

TableName|ColumnName

Table1 Col1
Table1 Col2
Table2 Col1
Table2 Col2
Table2 Col3

From here i am creating Table with column lists for each table:

TableName|List

Table1 [Table]
Table2 [Table]

And now i have query (which will be the function so i need dynamic variable for tableName) like here:

let
TableName = "Table1",
Source = TableName,
ColumnsToRemove = Table.Column(TableName, "List"){0} //not working --> i need just take the list from TableList to get all columns which i have to keep in Table1 - in this case Col1 and Col2
// table.RemoveColumns(Source,ColumnsToRemove)
in
ColumnsToRemove

So i want to refer to TableList and get List of columns which i have to keep, all other i want to delete. 
So in this example Table1 has one additional column which is called "AdditionalOne".
I do not want to have this column in result. 

How can i do this? 

Best,
Jacek

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
June 4, 2021 - 9:10 pm
sp_Permalink sp_Print

Instead of removing columns, why not provide the list of those you need to keep?

KeepColumns = Table.SelectColumns(TableName, ListOfColumnsToKeep)

or:
KeepColumns = Table.SelectColumns(TableName, List.RemoveItems(Table.ColumnNames(TableName),{"Col1ToRemove","Col2ToRemove"})

(to remove some columns from the existing list of columns)

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
3
June 4, 2021 - 11:57 pm
sp_Permalink sp_Print

Thank you it is nice.

But how to take the list of columns from TableList to keep ?
How to build funtion in order to make this dynamically? 

In other words: how to get column names from TableList based on selected table (filter TableList?), how to create ListOfColumnsToKeep variable dynamically?

Best,
Jacek

sp_AnswersTopicAnswer
Answers Post
Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
4
June 5, 2021 - 12:23 am
sp_Permalink sp_Print sp_EditHistory

Ok i added function fnGetColumnsToKeep:

(TableName)=>
let
#"Filter" = Table.SelectRows(TableList, each ([TableName] = TableName))
in
#"Filter"

and tried with:

let
TableName = "Table1",
ColumnsToKeep = Table.Column(fnGetColumnsToKeep(TableName), "List"){0},
LastShape = Table.SelectColumns(TableName, ColumnsToKeep)
in
LastShape

but it throws error:

Screenshot_178.pngImage Enlarger

Why? 

Best, 
Jacek

sp_PlupAttachments Attachments
  • sp_PlupImage Screenshot_178.png (67 KB)
Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
5
June 7, 2021 - 5:17 pm
sp_Permalink sp_Print

Anyone?

Best,
Jacek

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
June 7, 2021 - 6:04 pm
sp_Permalink sp_Print

= Table.SelectColumns(TableName, ColumnsToKeep)

TableName is not a table, it's the NAME of a table. The function needs a table, not a name.

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
7
June 7, 2021 - 6:37 pm
sp_Permalink sp_Print

Hi Catalin,

ok but function fnGetColumnsToKeep is working while invoking function. 

Ok so what should i do?

let
TableName = "Table1",
ColumnsToKeep = Table.Column(fnGetColumnsToKeep(TableName), "List"){0},
LastShape = Table.SelectColumns(TableName, ColumnsToKeep)
in
LastShape

How can i get this Table1 as not name but as Table? 
So how to from string evaluate Table?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
8
June 7, 2021 - 7:27 pm
sp_Permalink sp_Print

TableName="Table1" returns a text.

Source= Excel.CurrentWorkbook(){[Name=TableName]}[Content] returns a table where the table name is provided by the parameter TableName.

Your LastShape step has 2 problems:

LastShape= Table.SelectColumns(TableName, ColumnsToKeep) : Both parameters used are wrong: TableName is not a table, ColumnsToKeep is nowhere in your code, where is this coming from? ColumnsToKeep is supposed to be a list...

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
9
June 7, 2021 - 8:46 pm
sp_Permalink sp_Print

Hi Catalin,

thank you. 

i see the issue. 

I have this query with ColumnsToKeep in function but it is creating table which is not the list. 
Code:

let
TableName = "Table1",
Source = TableList,
#"Grouped Rows" = Table.Group(Source, {"TableName"}, {{"List", each _, type table [TableName=nullable text, List=table]}}),
#"Filter" = Table.SelectRows(#"Grouped Rows", each ([TableName] = TableName))
in
#"Filter"

i am getting table instead of List. How to create it?

Screenshot_181.pngImage Enlarger

 

Best,
Jacek

sp_PlupAttachments Attachments
  • sp_PlupImage Screenshot_181.png (149 KB)
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
10
June 7, 2021 - 11:09 pm
sp_Permalink sp_Print

As mentioned in your previous topics, a table column is a list.

You have to refer to a single column from a table to get a list of items in that column.

If you just put the column name at the end of a step, the result of that step is a list, no longer a table:
#"Filter" = Table.SelectRows(#"Grouped Rows", each ([TableName] = TableName))[ColumnName]

Make sure in that column you have values, not other objects like tables, the image you attached shows you have tables in the List column, if you refer to that column, you'll get a list of tables, not a list of strings...

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
11
June 8, 2021 - 12:27 am
sp_Permalink sp_Print sp_EditHistory

Thank you very much.

Ok i got it but still struggling with changing table to list type 🙂

let
TableName = "Table1",
Source = TableList,
#"Grouped Rows" = Table.Group(Source, {"TableName"}, {{"ColumnsToKeep", each _, type table [TableName=nullable text, List=table]}}),
#"List" = Table.AddColumn(#"Grouped Rows", "List", each Table.Column([ColumnsToKeep],"ColumnName")),
#"Filter" = Table.SelectRows(#"Added Custom", each ([TableName] = TableName))[Table]
in
#"Filter"

I am getting error while creating #"List" step... Literally the result in table is just "Error" for each row for the column.
Why? What i am doing wrongly? 

Please see workbook in attachment. 
Best,
Jacek

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
12
June 8, 2021 - 1:15 am
sp_Permalink sp_Print

= Table.AddColumn(#"Grouped Rows", "List", each Table.Column([ColumnsToKeep],"ColumnName"))

Where is the text in red coming from?

The table from [ColumnsToKeep] does NOT have a column with that name.

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
13
June 8, 2021 - 6:09 pm
sp_Permalink sp_Print

Hmm i was trying to add column and create list from Table.

The syntax is:

Table.Column(table as table, column as text) as list i have no idea what should be there.

I tried with:
 #"List" = Table.AddColumn(#"Grouped Rows", "List", each [ColumnsToKeep]),

but it is creating Table instead of list.
Can you please help?

Jacek

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
14
June 8, 2021 - 6:10 pm
sp_Permalink sp_Print

I also tried:

let
TableName = "Table1",
Source = TableList,
#"Grouped Rows" = Table.Group(Source, {"TableName"}, {{"ColumnsToKeep", each _, type table [TableName=nullable text, List=table]}}),
#"List" = Table.AddColumn(#"Grouped Rows", "List", each Table.Column(#"Grouped Rows",[ColumnsToKeep])),
#"Filter" = Table.SelectRows(#"Added Custom", each ([TableName] = TableName))[Table]
in
#"Filter"

Best,
Jacek

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
15
June 8, 2021 - 7:07 pm
sp_Permalink sp_Print

You are chasing your own tail and I have no idea what you're trying to do.
Which is the table you are trying to remove columns from?

#"Grouped Rows" step does not make any sense, because the Source is TableList, which is a grouped table. Why grouping again a grouped table, does not make any sense.

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
16
June 8, 2021 - 8:07 pm
sp_Permalink sp_Print

ok,

thanks.

I have TableList query like here:

let
Source = Excel.CurrentWorkbook(){[Name="TableList"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TableName", type text}, {"ColumnName", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"TableName"}, {{"Table", each _, type table [TableName=nullable text, ColumnName=nullable text]}}),
#"List" = Table.AddColumn(#"Grouped Rows", "List", each Table.Column([Table],"ColumnName"))
in
List

as you can see i am getting a List of columns for each table here

Next i have fnGetColumnsToKeep function where i am just filtering Table to get proper list:

(TableName)=>
let
Source = TableList,
#"Filter" = Table.SelectRows(Source, each ([TableName] = TableName))
in
#"Filter"

And i finally did it!:

let
Source = Table1,
TableName = "Table1",
ColumnsToKeep = Table.Column(fnGetColumnsToKeep(TableName), "List"){0},
LastShape = Table.SelectColumns(Source, ColumnsToKeep)
in
LastShape

the problem was with the source which i didnt have. I had only STRING "Table1".

Just a question, it is possbile to make the source and use string? 

Something like TableSourceName = Evaluate("Table1") and get table from string?

Best,
Jacek

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
17
June 8, 2021 - 8:19 pm
sp_Permalink sp_Print

See post 8 above, I already gave you the answer:

TableName="Table1"
Source= Excel.CurrentWorkbook(){[Name=TableName]}[Content] --> this returns a table based on the table name.

But this is a basic thing, you get this code from ANY query from a table.

Avatar
jaryszek
Member
Members
Level 0
Forum Posts: 183
Member Since:
February 1, 2019
sp_UserOfflineSmall Offline
18
June 8, 2021 - 8:22 pm
sp_Permalink sp_Print

Thank you very much for help Catalin,

Best !
Jacek

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Kim Knox, Nada Perovic
Guest(s) 9
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Bhuwan Devkota
Kathryn Patton
Maria Conatser
Forum Stats:
Groups: 3
Forums: 24
Topics: 6222
Posts: 27293

 

Member Stats:
Guest Posters: 49
Members: 31916
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.