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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
krigovi
New Member

Need to Get Table.ColumnCount if column contains specific value

Hello Team,

 

I have 10 Excel sheets, where i am trying to get column count if Column contains value "Different".

 

I have tried few Formaulas In power Query but nothing worked 

 

Table.ColumnCount( Table.SelectRows([Sheets],each Text.Contains([Column Name],"DIFFERENT")))

 

I have created column with all Column name using formula : Table.ColumnNames([Sheets]),

and sheets is my table 

 

krigovi_0-1737348236386.png

 

can some one help me to get the Query right ?

 

 

13 REPLIES 13
rohit1991
Super User
Super User

hi @krigovi , Use this concise formula in Power Query:

List.Count(List.Select(Table.ColumnNames([Sheets]), (col) => List.Contains(Table.Column([Sheets], col), "Different")))

Hello @rohit1991 

 

Thankyou for your Insight.

 

I like to get the count of column which contains "Different" in the row, not in the Column Name

krigovi_0-1737362295409.png

Here i have Value Different in the row so i lkike to count that column

danextian
Super User
Super User

Hi @krigovi 

 

Please try this as a custom column:

let 
//creates a list containing the header names of a table
ColumnNames = Table.ColumnNames([Sheets]),
//check if any of the list items contain the word DIFFERENT, case-sensitive
ContainsAWord = List.Select(ColumnNames, each Text.Contains(_, "DIFFERENT")),
//count the items in the filtered list
ListCount = List.Count(ContainsAWord)
in
ListCount

 










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
rajendraongole1
Super User
Super User

Hi @krigovi  - Use Table.SelectRows to filter rows in each sheet where any column contains "Different".

This will create a new column, "Count Columns with 'Different'," that contains the count of columns with the value "DIFFERENT" for each table in your dataset.

attached mcode FYR





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

Proud to be a Super User!





Hello @rajendraongole1 

 

Thankyou for your Valuable Solution,

krigovi_0-1737355619837.png

I tried it and it provides Value 0. 

 

krigovi_1-1737355650799.png

 

 

krigovi_2-1737355682936.png

 

I need Count of column which contains value "DIFFERENT"  can you share little more insight ?

Hi @krigovi - can you please check below code use the following formula in the Custom Column editor.





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

Proud to be a Super User!





bhanu_gautam
Super User
Super User

@krigovi , Try using 

 

In Power Query, go to Home > Advanced Editor.
Create a new function to check if a column contains the value "Different".

let
CheckColumnForValue = (table as table, value as text) as logical =>
let
columns = Table.ColumnNames(table),
containsValue = List.AnyTrue(List.Transform(columns, each List.Contains(Table.Column(table, _), value)))
in
containsValue
in
CheckColumnForValue

 

 

Go back to your main query where you have all the sheets.
Add a custom column to apply the function to each sheet.

 

let
Source = Excel.Workbook(File.Contents("YourFilePath.xlsx"), null, true),
Sheets = Source{[Item="Sheet1",Kind="Sheet"]}[Data], // Repeat this for each sheet
AddCustom = Table.AddColumn(Sheets, "ContainsDifferent", each CheckColumnForValue([Data], "Different")),
Filtered = Table.SelectRows(AddCustom, each [ContainsDifferent] = true),
ColumnCount = Table.RowCount(Filtered)
in
ColumnCount




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






shafiz_p
Super User
Super User

Hi @krigovi  Try this line of M code : 

AddCountColumn = Table.AddColumn(Source, "DifferentCount", each List.Count(List.Select([Column Name], each Text.Contains(_, "Different"))))

 

Change 'Source' with the last step name. Where Column name representing list with column name of those tables. If not, then try this code to create a column name list column:

AddColumnNamesList = Table.AddColumn(#"Filtered Rows", "ColumnNamesList", each Table.ColumnNames([Sheets])),

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

Best Regards,
Shahariar Hafiz

Hello @shafiz_p 

 

Thankyou for your valuable Solution,

 

Unfortunately i am getting 0 As result.

krigovi_0-1737356708263.png

 

krigovi_1-1737356722590.png

 

If your list contains table column names and your column name contains "DIFFERENT", then this formula should work. You see that it return 0, that means formula working but not finding any word "DIFFERENT". Please check each list to make sure, column name exist with the desired word. Also, check header as @danextian  suggest.

 

Hope this helps!!

Hello @shafiz_p 

 

Thankyou for your Feedabck,

Yes I can understand that, I need Count of Column which contains "DIFFERENT" in the row, not in Column name

krigovi_0-1737363220948.png

Your Insight would be verymuch Appriciated...

Hi @krigovi  You have provided wrong information and desired output. However, to count rows which contain specific word, try below steps:

AddCountColumn = Table.AddColumn(PreviousSteps, "DifferentCount", each try if Table.HasColumns([Sheets], "Column Name") then Table.RowCount(Table.SelectRows([Sheets], each Text.Contains([Column Name], "Different"))) else 0 otherwise 0)

 

Change column name accordingly.

 

[Sheets] column will contain your table and the column name from which you want to count rows which contains specific word, for your case "DIFFERENT".

 

shafiz_p_0-1737368776849.png

For my case, I have used Customer Name column from table. Each table contain this column, if not then will return 0.

 

Hope this will solve your problem!!

Hi @krigovi 

Have you checked if Column Name column actually contains the word you're looking for. I am thinking, you used Table.ColumnNames on tables without promoting the first row as the headers first - if this is the case, the column names will be just Column1, Column2, etc.  Please ensure they're promoted first.

Table.ColumnNames(Table.PromoteHeaders([Data Column], [PromoteAllScalars = true]))

 










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.