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
GSTI08
Frequent Visitor

DAX function to search for multiple values in strings and count the number of times each value occur

Hoping someone can help, I am new to DAX.

 

I am trying to count the number of times specific texts occurs.

 

I have a table/Column with multiple values in it. 

Regions
Europe, ME, UKI
UKI
South America, Africa
North America, UKI, Africa, Europe
Africa

 

I want to be able to count how many times a specfic value occurs so the output I am looking for in a visual would be

 

RegionCount
Africa3
South America1
ME1
North America1
UKI3
Europe2

 

I would prefer to do this in DAX rather than tables or query editor if this is possible.  

 

I did try to do this with a nested IF, but that sin't working and just brings me back true or false. 

4 CalcRegion =
IF(CALCULATE(COUNTROWS(Accounts),FILTER(Accounts,CONTAINSSTRING(Accounts[Primary Connectivity Regions],"Europe")))>0,1,0)
+IF(CALCULATE(COUNTROWS(Accounts),FILTER(Accounts,CONTAINSSTRING(Accounts[Primary Connectivity Regions],"Far East")))>0,1,0)
+IF(CALCULATE(COUNTROWS(Accounts),FILTER(Accounts,CONTAINSSTRING(Accounts[Primary Connectivity Regions],"UKI")))>0,1,0)
 
Any help or pointers in the right direction would be really appreciated.
 
1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @GSTI08 ,

 

It is suggested to create another region table by DAX or just enter data.

Region =
DATATABLE (
    "Region", STRING,
    {
        { "Africa" },
        { "South America" },
        { "ME" },
        { "North America" },
        { "UKI" },
        { "Europe" }
    }
)

 

Then, create measures like what @Greg_Deckler provided.

4 CalcRegion = 
VAR __SearchTerms =
    ADDCOLUMNS (
        Regions,
        "Count",
            COUNTROWS (
                FILTER (
                    'Accounts',
                    FIND ( [Region], 'Accounts'[Primary Connectivity Regions],, 0 ) > 0
                )
            )
    )
RETURN
    SUMX ( __SearchTerms, [Count] )

region.PNG

 

 

Best Regards,

Icey

 

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

6 REPLIES 6
GSTI08
Frequent Visitor

Thank you very much everyone for you help, particulary Greg and Icey, 

 

This is working perfectly!  

 

Really appreciate everyone's effort. 

 

Icey
Community Support
Community Support

Hi @GSTI08 ,

 

It is suggested to create another region table by DAX or just enter data.

Region =
DATATABLE (
    "Region", STRING,
    {
        { "Africa" },
        { "South America" },
        { "ME" },
        { "North America" },
        { "UKI" },
        { "Europe" }
    }
)

 

Then, create measures like what @Greg_Deckler provided.

4 CalcRegion = 
VAR __SearchTerms =
    ADDCOLUMNS (
        Regions,
        "Count",
            COUNTROWS (
                FILTER (
                    'Accounts',
                    FIND ( [Region], 'Accounts'[Primary Connectivity Regions],, 0 ) > 0
                )
            )
    )
RETURN
    SUMX ( __SearchTerms, [Count] )

region.PNG

 

 

Best Regards,

Icey

 

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

Greg_Deckler
Super User
Super User

@GSTI08 Your DAX formula could be greatly simplified:

4 CalcRegion =
  VAR __SearchTerms = 
    ADDCOLUMNS(
      { "Africa", "South America", "ME", "North America", "UKI", "Europe" },
      "Count", COUNTROWS(FILTER('Accounts',FIND([Value],'Accounts'[Primary Connectivity Regions],,0)>0))
RETURN
  SUMX(__SearchTerms,[Count])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg, 

 

That looks much better, thank you.  Although I am getting a "The Syntax for 'RETURN' is incorrect, but I can't see why, it looks fine.  Any ideas?

 

Thanks

@GSTI08 


Add a closing bracket ")" to @Greg_Deckler 's formula before the RETURN as below.

 

4 CalcRegion =
  VAR __SearchTerms = 
    ADDCOLUMNS(
      { "Africa", "South America", "ME", "North America", "UKI", "Europe" },
      "Count", COUNTROWS(FILTER('Accounts',FIND([Value],'Accounts'[Primary Connectivity Regions],,0)>0))
    )
RETURN
  SUMX(__SearchTerms,[Count])

 

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn




Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

VijayP
Super User
Super User

@GSTI08 

 

Its working for me 

Let me share the file here https://drive.google.com/file/d/1QUkqA8W4WjJfBRHOmwPXwGbV5noLbZcC/view?usp=sharing

Vijay Perepa

If this is the solution you are looking for mark this as solution and share your Kudoes

 




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


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.