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
CrownWorker
Helper I
Helper I

Dax - Keyword/Category replacement.

 

Ok here is my problem,  I have been using Access with Excel to this point to build my reports and am transitioning to PowerBI and very little understanding of Measures and DAX. 

 

the SQL I am using is as follows:

 

UPDATE MainTable, CategoryTable

SET MainCat = Category
WHERE TRBL_DESC Like "*" +CategoryTable.Keyword + "*";

 

MainTable - the table being used in PowerBI where I have a TRBL_DESC "trouble description" column and have created a blank column "MainCat" to populate from the SQL above.

 

CategoryTable - has 2 columns "Keyword" and "Category"

 

What the SQL does is pull in both tables, then look to see if TRBL_DESC field in the MainTable contains any of the keywords within its string from the CategoryTable Keyword fields then replaces the MainCat field in the MainTable with the Category field in the CategoryTable. 

 

I would like to do this all in PowerBi, and I dont want to have a long string because sometimes my keywords are in the 1000's and I need to be able to easily reorder them.

 

In short I need the Dax Equivalent for the SQL.

 

Thank you.

1 ACCEPTED SOLUTION

Hi @CrownWorker -

 

I believe you are getting the error on the second set because you need to ensure that your keyword list has a unique set of values. i.e. you can't have the same keyword listed in your keyword table more than once.

 

The formula I gave you above returns the category in alphabetical order. So if there are multiple matches, the item that comes first alphabetically is what appears in the category formula.

 

Sounds like you want to override this alphabetic sorting and return the category by your own ranking - where you define the order of categories to return in the case of multiple matches. Here's how:

 

Step 1: Add a column to your keyword table and rank your keyword table like so:

 

rankTable.png

 

Now change your matchfound formula to something resembling the following:

 

rankedCategory = 

LOOKUPVALUE(
    'Keyword Table'[Narrative Contains],'Keyword Table'[Rank],
     CALCULATE(
        FIRSTNONBLANK('Keyword Table'[Rank],1),
        filter(
VALUES('Keyword Table'[Narrative Contains]), SEARCH('Keyword Table'[Narrative Contains],'Transaction list'[Narrative],1,0)<>0 ) ) )

Which should now create the following:

 

rankTableresults.png

 

 

This should allow you to assign a ranking in order to dictate which categories should be returned in the case of multiple matches.

 

 

 

View solution in original post

7 REPLIES 7
v-jiascu-msft
Employee
Employee

Hi @CrownWorker,

 

Import these two tables first. Then add a calculated column with this formula.

MainCat =
LOOKUPVALUE (
    CategoryTable[Category],
    CategoryTable[Keyword], MainTable[Trbl_Desc]
)

Note: the records (rows) in "CategoryTable" should be unique.Dax - KeywordCategory replacement..jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

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

Thank you for the attempt, the only problem with your solution is that it needs an exact match and I am looking for partial/similar match

Hi @CrownWorker

 

I had a similar problem and just figured it out. Here's what I did:

 

I had a transaction table with various narratives. I wanted to search each description narrative to see if it contains a keyword from a keyword list If it did, return the corresponding category paired with the keyword. So like this:

 

Transaction list:

transaction list.png

 

Keyword List:

 

original lookuptable.png

 

The first thing I did was set up a column to determine if there's a match, and if so - what keyword identified the match. I created a calculated column with this formula:

MatchFound = 
FIRSTNONBLANK(
FILTER(
values('Keyword Table'[Narrative Contains]),
search('Keyword Table'[Narrative Contains],'Transaction list'[Narrative],1,0))
,1)

 

Which adds this column to the table - that identifies the keyword that was found in the narrative string. 

 

match 1.png

 

I then used a lookupvalue formula - to return the corresponding category like so:

 

category = 
LOOKUPVALUE(
'Keyword Table'[Category],'Keyword Table'[Narrative Contains],'Transaction list'[MatchFound]
)

Which adds this category column. Voila!

 

final match table 2.png

 

Hope that helps!

 

 

 

 

 

Matchfound.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

The first part worked, I would like to understand how it has ordered my list of keywords so that I can order them properly to capture the information in the best possible way to represent the service.

 

The second part isnt working properly though giving the error indicated in the graphic below

 

 

Tables.jpgCatError.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Hi @CrownWorker -

 

I believe you are getting the error on the second set because you need to ensure that your keyword list has a unique set of values. i.e. you can't have the same keyword listed in your keyword table more than once.

 

The formula I gave you above returns the category in alphabetical order. So if there are multiple matches, the item that comes first alphabetically is what appears in the category formula.

 

Sounds like you want to override this alphabetic sorting and return the category by your own ranking - where you define the order of categories to return in the case of multiple matches. Here's how:

 

Step 1: Add a column to your keyword table and rank your keyword table like so:

 

rankTable.png

 

Now change your matchfound formula to something resembling the following:

 

rankedCategory = 

LOOKUPVALUE(
    'Keyword Table'[Narrative Contains],'Keyword Table'[Rank],
     CALCULATE(
        FIRSTNONBLANK('Keyword Table'[Rank],1),
        filter(
VALUES('Keyword Table'[Narrative Contains]), SEARCH('Keyword Table'[Narrative Contains],'Transaction list'[Narrative],1,0)<>0 ) ) )

Which should now create the following:

 

rankTableresults.png

 

 

This should allow you to assign a ranking in order to dictate which categories should be returned in the case of multiple matches.

 

 

 

 

Hey Robbieinoz,

 

You are amazing. That solved every issue I had. I wish there was a way I could show my appreciation better then a few lines of text on a screen.  I'll try to send some goodwill and karma your way.

 

Thanx again.

No worries happy to help 🙂

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.