powerbidaxpowerquery

Find and isolate the values found in a text column - DAX


I have a two dummy tables that look like:

Narrative

User

There is a 1- * relationship between them

I have created a measure so I can find what Users are mentioned in the Narrative, however the measure keeps returning null values. I tried using the FIND function and CONTAINSSTRING functions but it does not return values from the User column.

MatchedUsers = 
  VAR NarrativeText = MAX('Narrative'[Narrative])
  RETURN 
    CALCULATE(
      CONCATENATEX(
        FILTER(
          VALUES('User'[User]),
          NOT(ISBLANK(NarrativeText)) && 
          CONTAINSSTRING(NarrativeText, 'User'[User])
        ),
      'User'[User], ", "
    )
  )

Id like it to return the names from the user column that are mentioned in the Narrative column

Narrative User
The tree is green
Tim's tree is green
Tim, Sarah, Anne, Fred Sarah, Anne
Jack, Sarah Jack, Sarah
It is sunny outside

Solution

  • Try this measure:

    MatchedUsers = 
        VAR usersTable = ALL(User[User])  //Get all users
        VAR n = MAX(Narrative[Narrative]) //Gets the current Narrative
        
        VAR matches = CALCULATE(CONCATENATEX(
            FILTER(
                usersTable,
                CONTAINSSTRING(n, [User]) //Finds each user in the Narrative
            ),
            [User], ", ") //Creates comma delimited list
        )
        RETURN matches