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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Oceanbagel
Frequent Visitor

Creating a flag based on text values from 2 different tables

I have two tables that are related based on ID. This is a many to many relationship.

 

TableA

ID

City

101Fremont
101

Fremont

102

Lakeside

103Auburn
104

Colburn

105Ephrata
106Columbus
107Eugene

 

TableB

IDNarrative
101misc narrative text
102misc narrative text
103misc narrative text
103misc narrative text including the word danger
104misc narrative text
105misc narrative text including the word help
106misc narrative text
107misc narrative text including the word critical

 

I need to create a flag column in TableA (or in a new table) that returns a true/false based on criteria found in either table:

If TableA[City] is Fremont or Columbus or if TableB[Narrative] contains danger, help, or critical return true. Otherwise return false.

 

This is obviously sample data and my true application involves several criteria from both columns that I need to return true. I'm able to apply the City and Narrative logic separately within their respective tables but bringing them together is where I'm stuck.

1 ACCEPTED SOLUTION
AmiraBedh
Resident Rockstar
Resident Rockstar

You're looking for a way to combine criteria from two tables to generate a flag column. In this context, you can make use of DAX in Power BI to create such a flag.

The basic process can be split into:

1. Create relationships between the tables based on ID.
2. Use the RELATED function (or RELATEDTABLE function if there are multiple related rows) to pull data from TableB into TableA.
3. Create the flag based on the criteria.

 

Flag Column =
VAR CurrentCity = TableA[City]
VAR RelatedNarrative = CALCULATE(CONCATENATEX(TableB, TableB[Narrative], ", "), ALL(TableB[ID]))

RETURN
IF(
(CurrentCity IN {"Fremont", "Columbus"} ||
SEARCH("danger", RelatedNarrative, 1, 0) > 0 ||
SEARCH("help", RelatedNarrative, 1, 0) > 0 ||
SEARCH("critical", RelatedNarrative, 1, 0) > 0),
TRUE(),
FALSE()
)

This is how I am explaining what I have done so far :

1. `VAR CurrentCity`: This gets the city from the current row in TableA.
2. `VAR RelatedNarrative`: This calculates a concatenated narrative from all the rows in TableB related to the current ID in TableA. We use CONCATENATEX to concatenate all related narratives, and we reset the filter context on the ID column to make sure we get all related rows.
3. `RETURN`: We then use an IF statement to evaluate our conditions:
- If the city is either "Fremont" or "Columbus".
- If the related narrative contains the word "danger", "help", or "critical".

The flag is then set to TRUE if any of the conditions are met, otherwise it's set to FALSE.

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

5 REPLIES 5
parry2k
Super User
Super User

@Oceanbagel if you have more keywords to search I would recommend adding a column in PQ in TableB for the matching keywords with flag 1 and 0 so that you don't have to check all the keywords in the DAX, it will make things super clean and simpler, and it is super easy to add a column in PQ.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

AmiraBedh
Resident Rockstar
Resident Rockstar

You're looking for a way to combine criteria from two tables to generate a flag column. In this context, you can make use of DAX in Power BI to create such a flag.

The basic process can be split into:

1. Create relationships between the tables based on ID.
2. Use the RELATED function (or RELATEDTABLE function if there are multiple related rows) to pull data from TableB into TableA.
3. Create the flag based on the criteria.

 

Flag Column =
VAR CurrentCity = TableA[City]
VAR RelatedNarrative = CALCULATE(CONCATENATEX(TableB, TableB[Narrative], ", "), ALL(TableB[ID]))

RETURN
IF(
(CurrentCity IN {"Fremont", "Columbus"} ||
SEARCH("danger", RelatedNarrative, 1, 0) > 0 ||
SEARCH("help", RelatedNarrative, 1, 0) > 0 ||
SEARCH("critical", RelatedNarrative, 1, 0) > 0),
TRUE(),
FALSE()
)

This is how I am explaining what I have done so far :

1. `VAR CurrentCity`: This gets the city from the current row in TableA.
2. `VAR RelatedNarrative`: This calculates a concatenated narrative from all the rows in TableB related to the current ID in TableA. We use CONCATENATEX to concatenate all related narratives, and we reset the filter context on the ID column to make sure we get all related rows.
3. `RETURN`: We then use an IF statement to evaluate our conditions:
- If the city is either "Fremont" or "Columbus".
- If the related narrative contains the word "danger", "help", or "critical".

The flag is then set to TRUE if any of the conditions are met, otherwise it's set to FALSE.

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

This worked. Thank you very much!

How would you filter out row of narrative based on a similar search to prevent them from getting to the Return step?

If you want to filter out certain rows from `TableB` before evaluating them in the `RETURN` step, you can modify the formula in the `RelatedNarrative` variable.

Let's assume you want to filter out any rows in `TableB` where the narrative contains the word "exclude" (you can adjust the criteria as needed):

Flag Column =
VAR CurrentCity = TableA[City]
VAR RelatedNarrative =
CALCULATE(
CONCATENATEX(
FILTER(
TableB,
SEARCH("exclude", TableB[Narrative], 1, 0) = 0
),
TableB[Narrative],
", "
),
ALL(TableB[ID])
)

RETURN
IF(
(CurrentCity IN {"Fremont", "Columbus"} ||
SEARCH("danger", RelatedNarrative, 1, 0) > 0 ||
SEARCH("help", RelatedNarrative, 1, 0) > 0 ||
SEARCH("critical", RelatedNarrative, 1, 0) > 0),
TRUE(),
FALSE()
)

In the `RelatedNarrative` variable, the `FILTER` function filters out rows from `TableB` where the narrative contains the word "exclude". Then, the remaining narratives are concatenated into a single string.

So, by the time we get to the `RETURN` step, any narratives containing "exclude" have already been removed and won't be evaluated in the flag-setting logic.


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696
parry2k
Super User
Super User

@Oceanbagel try following as column in TableA

 

Match Found = 
VAR __TableB = 
ADDCOLUMNS ( 
    RELATEDTABLE ( TableB ), 
    "@Matched", INT ( 
        CONTAINSSTRING ( TableB[Narrative], "Danger" ) || 
        CONTAINSSTRING ( TableB[Narrative], "Help" ) || 
        CONTAINSSTRING ( TableB[Narrative], "Critical" ) 
    ) 
) 
VAR __MatchFound = SUMX ( __TableB, [@Matched] )
RETURN
TableA[City] IN { "Columbus", "Fremont" } && NOT ISBLANK ( __MatchFound )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.