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

Calculated column to check if a value exists in another table

I have two tables that are joined with a many-many relationship on Item Number.
One of them (Table1) is a reference table - it contains like 40 or so Item Number entries of a certain type (a few of which are duplicates with different other values associated, which is the only reason it is many-many)

The other (Table2) is a record of returns, with Item Number as one of the columns.

 

What I'd like to do is create a calculated column in Table2 which checks to see if that row's Item Number is represented in Table1.

 

Product Type X = 
if( 'Table2'[ITEM] IN related('Table1'[Item Number]), 1, 0 )

 

However, I get this error message: "The column 'Table1[Item Number]' either doesn't exist or doesn't have a relationship to any table available in the current context."
Despite the fact that they do have a relationship in the model

swisdom_0-1638993755900.png

Any tips?

8 REPLIES 8
Jeanxyz
Post Prodigy
Post Prodigy

Related() only works in 1-to-many relationship, the calculated column has to be created on the many side of a relationship. That's why it won't work in your case. 

 

As I turnaround, I would create a new lookup table using table 3=values('Table1'[Item Number] ) and link it to table 2.  You can then write related formula 

 

Product Type X =
if( 'Table2'[ITEM] = related('Table3'[Item Number]), 1, 0 )

 

bcdobbs
Super User
Super User

Related won't work becuase it returns a single value. Due to the nature of your many to many relationship it could return multiple rows.


You need to count the rows using RELATEDTABLE. However you also need to pass the filter back to Table1 so in example that follows CROSSFILTER temporily lets filter go both ways.

Exists In Table 1 = 

VAR RelatedRows = 
    CALCULATE (
        COUNTROWS ( RELATEDTABLE(Table1) ),
        CROSSFILTER ( Table1[ID], Table2[ID], Both )
    )

RETURN IF ( RelatedRows > 0 , 1, 0 )

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

Same idea but I prefer this version.

Exists In Table 1 = 
    CALCULATE (
        INT ( NOT ISEMPTY( ( Table1 ) ) ),
        TREATAS ( { Table2[ID] }, Table1[ID] )
    )

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

swisdom_1-1638996112516.pngswisdom_2-1638996137089.png
swisdom_0-1638996089687.png

I'm getting errors using both of those formulas.

You just need to replace Table1[ID] and Table2[ID] with relevant table / column names from your model.



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

Yes of course, but it doesn't find Table1 without the RELATED function.

Have a look at my example. I think I've matched your model as far as I can see: Demo File 

 

If your table names have spaces make sure they're between ' ' Eg 'Table 1'[ITEM]



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

I think this might be the problem.
DAX function "RELATED" does not work between DirectQuery and Import tables.
I have mixed sources.

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.