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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
einfraulein
Regular Visitor

Count rows if column contains specific text, but column have multiple text in cells

Hello dear community,

 

i hope you're doing well. 

 

My problem:  i have a table "Countries" with two columns "Alpha-2-code" and " Country".

Table "Countries": 

Alpha-2 codeEnglish short name (upper/lower case)
ADAndorra
AEUnited Arab Emirates
AFAfghanistan
AGAntigua and Barbuda
AIAnguilla
ALAlbania
AMArmenia
AOAngola
AQAntarctica
ARArgentina
ASAmerican Samoa
ATAustria
AUAustralia
AWAruba
AXAland Islands !Åland Islands
AZAzerbaijan
BABosnia and Herzegovina
BBBarbados
BDBangladesh
BEBelgium
BFBurkina Faso
BGBulgaria
BHBahrain
BIBurundi
BJBenin
BLSaint Barthelemy !Saint Barthélemy
BMBermuda
BNBrunei Darussalam
BOBolivia (Plurinational State of)
BQBonaire, Sint Eustatius and Saba
BRBrazil
BSBahamas
BTBhutan
BVBouvet Island
BWBotswana
BYBelarus
BZBelize
CACanada
CCCocos (Keeling) Islands
CDCongo (Democratic Republic of the)
CFCentral African Republic
CGCongo
CHSwitzerland
CICote d'Ivoire !Côte d'Ivoire
CKCook Islands
CLChile
CMCameroon
CNChina
COColombia
CRCosta Rica
CUCuba
CVCabo Verde
CWCuracao !Curaçao
CXChristmas Island
CYCyprus
CZ

Czech Republic

And i have a table " AMT-Query" with a lot of columns, BUT the column "MarketInfo" is the "Problem".

einfraulein_1-1670939052747.png

As you can see, there are sometimes several coutry codes in one cell.
I am looking for a way that based on the table "Countries", I can calculate the number (not distinct) of rows if the column "MarketInfo" contains this selected "Alpha-2-code".

 

What I would like to see:

Alpha-2 codeEnglish short name (upper/lower case)Total sum if "Alpha-2-code" is found in cell from "MarketInfo"
ADAndorra45 (bc found in 45 rows in column "MarketInfo")
AEUnited Arab Emirates 
AFAfghanistan 
AGAntigua and Barbuda 
AIAnguilla 
ALAlbania 
AMArmenia 
AOAngola 
AQAntarctica 
ARArgentina 
ASAmerican Samoa 
ATAustria 
AUAustralia 
AWAruba 
AXAland Islands !Åland Islands 
AZAzerbaijan 
BABosnia and Herzegovina 
BBBarbados 
BDBangladesh 
BEBelgium 
BFBurkina Faso 
BGBulgaria 


I have tried to split the MarketInfo column into max 10 columns, so that I have the individual Country codes in the 10 columns and then calculate the count when the country is found from "Alpha-2-code" in the columns "MarketInfo 1 - 10".

I tried with lookup, search, contains, but I'm quite new to PBI and need to learn a lot more about the formula structure.

 

Calculation explanation:

Data from column "MarketInfo"Calculation for formula
FR= 1 for "FR"
SA= 1 for "SA"
AL AL = this should be count as 1 for Country "AL", bc its doubled
AR= 1 for "AR"
AR = 1 for "AR"
AR MX= 1 for AR, 1 for MX
AR MX = 1 for AR, 1 for MX
AT 
AT  
AT AT  
AZ GE  
BE 
BE  
BE DE FR  
BE DE FR NL  
BE FR LU NL  
BE FR UK NL  
BE LU  
BE LU NL  
BE NL LU 
BG 
BG  
BG GR RO  
BG GR RO RO  
BG HR RO 
BG HR RO  
BG HR RS 
BG HR RS  
BG HR RS ME 
BG HU 
BG RO HR 
BH IQ JO KW LB OM QA SA AE  
BJ BF TD DJ GA GN CM CG MG ML MR NE CF SN TG CI 

 

Thanks a lot and i hope that my explanation is understandable.

Sarah

1 ACCEPTED SOLUTION
v-yueyunzh-msft
Community Support
Community Support

Hi , @einfraulein 

According to your description,you want to create a new column in the "Countries" table to count the number of occurrences in AMT-Query[MarketInfo], and only once in a row if it appears consecutively. Right?

Here are the steps you can refer  to :
(1)This is my test data:

vyueyunzhmsft_0-1670984300725.png

(2)We can add a "blank query" in Power Query Editor:

let
    Source = Table.TransformColumns(#"AMT-Query",{"MarketInfo",(x)=>List.Distinct(Text.Split(x," "))     }),
    #"Expanded MarketInfo" = Table.ExpandListColumn(Source, "MarketInfo")
in
    #"Expanded MarketInfo"

(3)Then we can add a custom column in "Countries" Table :

(x)=>  Table.RowCount(Table.SelectRows(Query1,(y)=>y[MarketInfo]=x[#"Alpha-2 code"]  ))

vyueyunzhmsft_1-1670984390476.png

(4)Then we can meet your need:

vyueyunzhmsft_2-1670984406017.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

In the Query Editor, split that column by rows (not columns).


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yueyunzh-msft
Community Support
Community Support

Hi , @einfraulein 

According to your description,you want to create a new column in the "Countries" table to count the number of occurrences in AMT-Query[MarketInfo], and only once in a row if it appears consecutively. Right?

Here are the steps you can refer  to :
(1)This is my test data:

vyueyunzhmsft_0-1670984300725.png

(2)We can add a "blank query" in Power Query Editor:

let
    Source = Table.TransformColumns(#"AMT-Query",{"MarketInfo",(x)=>List.Distinct(Text.Split(x," "))     }),
    #"Expanded MarketInfo" = Table.ExpandListColumn(Source, "MarketInfo")
in
    #"Expanded MarketInfo"

(3)Then we can add a custom column in "Countries" Table :

(x)=>  Table.RowCount(Table.SelectRows(Query1,(y)=>y[MarketInfo]=x[#"Alpha-2 code"]  ))

vyueyunzhmsft_1-1670984390476.png

(4)Then we can meet your need:

vyueyunzhmsft_2-1670984406017.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Helpful resources

Announcements
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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