Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
mtomova
Helper III
Helper III

Count number of columns in a table, excluding columns with specific column names in Power Query

Hi all,

 

I want to count the number of columns in a table, but excluding some columns with specific name.

 

This is what I have tried, but it's not working:

= Record.RemoveFields(Table.ColumnCount(#"MyTable"), {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"} )

 

 

This bit of the M code is working fine:

 

= Table.ColumnCount(#"MyTable")

 

It returns 30, this is how many columns I have in the table.

However, I want to count all columns excluding the columns with these names - "ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"

 

When I use the code I have pasted above, I get this error message:

mtomova_0-1737724154091.png

 

 

As a workaround I have done the below:

 

= Table.ColumnCount(#"MyTable") - 4

 

and it's working

 

Since I know that I want to remove 4 columns I am just performing the subtraction.

However, I am keen on learning how to do this using M code.

 

Thanks,

Maria

 

1 ACCEPTED SOLUTION
mtomova
Helper III
Helper III

Hey,

 

wow, I found a way as I kept playing with the code.

 

What I did is first, remove the columns that I don't want to count from the table:

 

= Table.RemoveColumns(#"MyTable"), {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"} )

 

This M code returns a table. Then I have enwrapped this code in a Table.ColumnCount():

 

= Table.ColumnCount(
Table.RemoveColumns(#"MyTable"), {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"} )

)

 

And I got exactly what I needed (:

 

I hope that this helps someone

View solution in original post

4 REPLIES 4
mtomova
Helper III
Helper III

Hey,

 

wow, I found a way as I kept playing with the code.

 

What I did is first, remove the columns that I don't want to count from the table:

 

= Table.RemoveColumns(#"MyTable"), {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"} )

 

This M code returns a table. Then I have enwrapped this code in a Table.ColumnCount():

 

= Table.ColumnCount(
Table.RemoveColumns(#"MyTable"), {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"} )

)

 

And I got exactly what I needed (:

 

I hope that this helps someone

dufoq3
Community Champion
Community Champion

Hi @mtomova, there are allways many ways:

= List.Count(List.Difference(Table.ColumnNames(#"Your Table Name"), {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"}))

or

= Record.FieldCount(Record.RemoveFields(#"Your Table Name"{0}, {"ColumnX",  "ColumnXP",  "ColumnAAA", "ColumnVVBVV"}))

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi,

 

thanks for providing different options!

 

Thanks,

Maria

dufoq3
Community Champion
Community Champion

Hi Maria, you're welcome 😉


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.