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
powernewb
New Member

Count number of records if contains string from a reference table

Hi there,

 

How do I write a DAX function to count the number of cells in a column contain a string, from a list of strings in a reference table. 

For example, imagine that I have a items table and a refernece table:

 

Items Table:

 

iditems_listcost
1hats, jar, shoes20
2Fedora, key, Pens10
3baseball cap, Sandals30

 

 

Refernce Type Table:

Hat TypesShoe Types
hatshoes
fedorasandals
cap 

 

 

I would like to write a DAX function that takes in the strings from the Reference Type table and checks that if the string is contained in the items_list table. And if it does, then count the number of rows.

 

For example:

 

When looking to see if the strings in the Hat Types column are contained in the items_list, I can see 3 rows.

 

When looking to see if the strings in the Shoe Types column are contained in the items_list, I can see 2 rows.

 

Any help you can give is much appreciated!

 

Thank you!

 

 

 

 

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

Hi @powernewb,

 

We can take the following steps to meet your requirement.

 

1. Create a calculated table.

 

CRO = CROSSJOIN(Items,Refernce)

2. Create the measures as below.

 

Hat = IF(ISBLANK(SEARCH(MAX(CRO[Hat Types]),MAX(CRO[items_list]),1,BLANK())),BLANK(),1)
hat total = SUMX(CRO,[Hat])- CALCULATE(COUNTROWS(CRO),FILTER(CRO,MAX(CRO[Hat Types])=BLANK()))
shoes = var shoe = MAX(CRO[Shoe Types])
return
IF(ISBLANK(SEARCH(shoe,MAX(CRO[items_list]),1,BLANK())),BLANK(),1)
shot total = SUMX(CRO,[shoes])- CALCULATE(COUNTROWS(CRO),FILTER(CRO,CRO[Shoe Types]=BLANK()))

123.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

2 REPLIES 2
v-frfei-msft
Community Support
Community Support

Hi @powernewb,

 

We can take the following steps to meet your requirement.

 

1. Create a calculated table.

 

CRO = CROSSJOIN(Items,Refernce)

2. Create the measures as below.

 

Hat = IF(ISBLANK(SEARCH(MAX(CRO[Hat Types]),MAX(CRO[items_list]),1,BLANK())),BLANK(),1)
hat total = SUMX(CRO,[Hat])- CALCULATE(COUNTROWS(CRO),FILTER(CRO,MAX(CRO[Hat Types])=BLANK()))
shoes = var shoe = MAX(CRO[Shoe Types])
return
IF(ISBLANK(SEARCH(shoe,MAX(CRO[items_list]),1,BLANK())),BLANK(),1)
shot total = SUMX(CRO,[shoes])- CALCULATE(COUNTROWS(CRO),FILTER(CRO,CRO[Shoe Types]=BLANK()))

123.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Hi @powernewb,

 

Does that make sense? If so, kindly mark my answer as a solution to close the case.


Regards,
Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

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.