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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
amineregask
New Member

Count number of text cells containing string, for each string in a table column

Hi everyone,

 

I would need your help on the following case. I have 2 tables:

  • Table 1: contains a column with a list of countries
  • Table 2: contains a column with text elements

I would like to know, for each country in table 1, the number of text elements in table 2 that contain at least one occurrence of that country.

In the example below, the desired output will be:

  • Mexico: 2 occurrences (appears in 3rd and 4th items in table 2, even if it appears more than once in the 3rd item)
  • Japan: 1 occurrence (1st item in table 2)
  • Singapore: 1 occurrence (3rd item in table 2)
  • Rwanda and Chile: 0 occurrence

amineregask_1-1626249220798.png

 

Thank you in advance for your help, much appreciated!

1 ACCEPTED SOLUTION

Hi @amineregask 

 

You need to remove that "each" after "Custom", before (X)=>

= Table.AddColumn(#"Changed Type1", "Custom",  (x)=> List.Count(List.Select(Table2[Text], each Text.Contains(_,x[Country]))))

 

@watkinnc the way seems better, but there is a typo, you can try like this

Table.AddColumn(#"Changed Type1", "Custom", each List.Count(List.FindText(Table2[Text],[Country])))

View solution in original post

6 REPLIES 6
watkinnc
Super User
Super User

Actually it looks like my typo is that it should read in Counts, not in counts...

 

let

Country2 = Table2[Text],

NewTable = Table1,

Counts = Table.AddColumn(NewTable, "Total", each List.Count(List.FindText(Country2,  [Text]))

in Counts

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
watkinnc
Super User
Super User

let

Country2 = Table2[Text],

NewTable = Table1,

Counts = Table.AddColumn(NewTable, "Total", each List.Count(List.FindText(Country2,  [Text]))

in counts

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
Vera_33
Resident Rockstar
Resident Rockstar

Hi @amineregask 

 

If your Table2 and Text column are different names, please change it accordingly

 

Vera_33_0-1626251031826.png

(x)=> List.Count(List.Select(Table2[Text], each Text.Contains(_,x[Country])))

 

Hi @Vera_33 thanks a lot for your quick reply!

When I create the custom column, I get "function" values in it. Did I miss any step?

amineregask_0-1626257171357.png

Thank you!

Hi @amineregask 

 

You need to remove that "each" after "Custom", before (X)=>

= Table.AddColumn(#"Changed Type1", "Custom",  (x)=> List.Count(List.Select(Table2[Text], each Text.Contains(_,x[Country]))))

 

@watkinnc the way seems better, but there is a typo, you can try like this

Table.AddColumn(#"Changed Type1", "Custom", each List.Count(List.FindText(Table2[Text],[Country])))

Hi @Vera_33 @watkinnc 

 

Both solutions work, many thanks!!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors