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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Griffin_BI
Frequent Visitor

Finding Intersection between two columns

I have two tables:

 

Table1:

 

IDLetter
1a
2b
3c
4d
5e
6f
7g
8h
9i

 

And a virtual table, Table 2

 

ID

1
2

 

How do I find the intersection (i.e. all rows in Table1 that has an ID in Table2). There are a large number of rows, so the DAX function must be efficient. 

 

Thanks!

6 REPLIES 6
Griffin_BI
Frequent Visitor

Thanks Owen, but I couldn't use this as a solution.

 

TREATAS() requires the name number of columns in the table I believe.

 

Can I not use CONTAINS?

 

The ultimate goal is to filter a table based on a random sample (Table2). Two parallel calculations have to be done from the same random sample so I'm getting the sample as a concatenated list and then building a virtual table using that list inorder to filter another virtual table.

That's right, TREATAS requires the same number of columns as column references provided.

 

If Table2 contains more than just the ID column, then just use SELECTCOLUMNS to select the ID column:

CALCULATETABLE (
    Table1,
    TREATAS (
        SELECTCOLUMNS ( Table2, "ID", Table2[ID] ),
        Table1[ID]
    )
)

It is possible to use CONTAINS with FILTER, but performance is likely to be worse.

https://www.sqlbi.com/articles/propagate-filters-using-treatas-in-dax/

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Not sure what I'm doing wrong here. I get an error saying that a multiple values were supplied when a single value was expected?
 
VAR _indextbl = GENERATESERIES(1, PATHLENGTH([Sample List]))

VAR _Table2 =

GENERATE(_indextbl, ROW("@ID", PATHITEM(_indextbl, [Value], 0)))
 
VAR _SampleTbl =

CALCULATETABLE(
ALLSELECTED('Table1'),
TREATAS(
SELECTCOLUMNS(Table2, "ID", [@ID]),
'Table1'[ID]
)
)

VAR _Xbar =

AVERAGEX(
SUMMARIZE(
_SampleTbl,
'Calendar'[Month],
"@X", AVERAGE(LengthOfTime)
),
[@X])

RETURN

_XBar

One problem I can see is that the first argument of PATHITEM is currently _indextbl, but it should be a vertical bar delimited string, presumably [Sample List].


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

It is for creating a new table.

 

 Picture1.png

 

Intersect table =
CALCULATETABLE ( Table1, INTERSECT ( VALUES ( Table1[ID] ), Table2 ) )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

OwenAuger
Super User
Super User

Hi there,

Based on what you've posted, I would suggest using TREATAS to apply Table2 as a filter (I'm assuming Table2 has a single column).

 

For example, if you need an expression that returns the rows of Table1

CALCULATETABLE (
    Table1,
    TREATAS ( Table2, Table1[ID] )
)

 What is the ultimate goal, i.e. where would the intersecting rows of Table1 be used? 

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.