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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Count rows containing one of many strings (not exactly)

Context:

Table1 = 'Companies' (list of companies)

Table2 = 'Specialties' (list of areas of expertise of those companies)

Relationship = 1:M (one company has multiple specialties)

 

Company_id  Company_name  
1Microsoft
2Google
3Amazon

 

Specialty_id  Specialty_name  Company_id  
1Cloud1
2Data1
3Database2
4System Architecture  2
5Security2

 

I want to create a column in the table 'Companies' that counts the amount of specialties of that company containing one of many strings. In other words, the column 'Specialty_name' has to be filtered so that only the rows remain that contains one of the strings (not exact). 

 

List of strings = {"Data", "Cloud", ...}

 

So the output should be:

Company_id  Company_name  Count_Specialties  
1Microsoft2
2Google1
3Amazon0

 

As you see, the new column should also count "Database" even though that exact string is not in the list (only "Data").

 

So far, I managed to create a DAX but it's very long and inefficient: 

Count_Specialties= CALCULATE(COUNTROWS(FILTER('specialties', CONTAINSSTRING('specialties'[specialty_name], "Cloud") || CONTAINSSTRING('specialties'[specialty_name], "Data") + 0
The list contains 20+ strings, so the DAX keeps adding strings via || (and at the end I add '+ 0' to avoid null values). 
 
I wonder if there is another way that can produce the same column more efficiently. 
 
Thanks in advance!
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

You need to put all the conditions into the formula. There doesn't seem to be a simpler way. 

Or you can put the conditions into a column. Then count the column.

Column = if(CONTAINSSTRING('specialties'[specialty_name  ], "Cloud") || CONTAINSSTRING('specialties'[specialty_name  ], "Data"),1,0)
Measure = CALCULATE(COUNT(Specialties[Column])+0,FILTER(ALL('Table'),'Table'[Company_id  ]=SELECTEDVALUE('Table'[Company_id  ])))

vpollymsft_0-1648792115125.png

If I have misunderstood your meaning, please provide your pbix file without privacy infromation and desired output.

 

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @Anonymous ,

You need to put all the conditions into the formula. There doesn't seem to be a simpler way. 

Or you can put the conditions into a column. Then count the column.

Column = if(CONTAINSSTRING('specialties'[specialty_name  ], "Cloud") || CONTAINSSTRING('specialties'[specialty_name  ], "Data"),1,0)
Measure = CALCULATE(COUNT(Specialties[Column])+0,FILTER(ALL('Table'),'Table'[Company_id  ]=SELECTEDVALUE('Table'[Company_id  ])))

vpollymsft_0-1648792115125.png

If I have misunderstood your meaning, please provide your pbix file without privacy infromation and desired output.

 

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Whitewater100
Solution Sage
Solution Sage

Hi:

You can do this via the data model by either a measure or a new table. Please see two tables (different ways of calculating the same result) and a measure doing the same thing. Please see link here:

https://drive.google.com/file/d/1EcZBdUDVUC0cOb3CSM1QdnblfbvZQzxL/view?usp=sharing 

 

Whitewater100_0-1648573350606.png

 

Anonymous
Not applicable

Hey, thanks a lot for your response!

 

Unfortunately don't think I was clear enough in the description. I have a big list of relevant Specialties on which I want to filter, but the amount of Specialties linked to the companies is much higher. So it's not enough to just count the specialties. I know the example I gave was misleading because all of the specialties needed to be counted.

 

I will edit the post, let me know if it still isn't clear!

 

Hopefully you can still help 🙂

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.