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
andreiasanz
Frequent Visitor

IN clause SQL to DAX

Good afternoon,

 

I'm having trouble passing the following sql statement to DAX, my problem is the IN clause.

Anyone can help? 

 

SELECT *
FROM TABLE1
WHERE COLUMN1 IN (
          select COLUMN1
          from TABLE2
          where COLUMN2 IN(
                    select COLUMN2
                    from TABLE3
                    where COLUMN5 = 'XPTO'))

 

Regards

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @andreiasanz

 

You can use TREATAS or IN to perform the function of the SQL IN operator.

I would start from the innermost SELECT and turn each SELECT into a CALCULATETABLE. For columns that are used as filters, VALUES(...) is sufficient to get the distinct values of the column.

 

Here are two versions:

 

 

Result =
VAR Table3_Column2 =
    CALCULATETABLE ( VALUES ( TABLE3[COLUMN2] ),
TABLE3[COLUMN5] = "XPTO"
) VAR Table2_Column1 = CALCULATETABLE ( VALUES ( TABLE2[COLUMN1] ),
TREATAS ( Table3_Column2, TABLE2[COLUMN2] ) ) RETURN CALCULATETABLE (
TABLE1,
TREATAS ( Table2_Column1, TABLE1[COLUMN1] )
)
Result =
VAR Table3_Column2 =
    CALCULATETABLE ( VALUES ( TABLE3[COLUMN2] ),
TABLE3[COLUMN5] = "XPTO"
) VAR Table2_Column1 = CALCULATETABLE (
VALUES ( TABLE2[COLUMN1] ),
TABLE2[COLUMN2] IN Table3_Column2
) RETURN CALCULATETABLE (
TABLE1,
TABLE1[COLUMN1] IN Table2_Column1
)

Regards,

Owen

 


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

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @andreiasanz

 

You can use TREATAS or IN to perform the function of the SQL IN operator.

I would start from the innermost SELECT and turn each SELECT into a CALCULATETABLE. For columns that are used as filters, VALUES(...) is sufficient to get the distinct values of the column.

 

Here are two versions:

 

 

Result =
VAR Table3_Column2 =
    CALCULATETABLE ( VALUES ( TABLE3[COLUMN2] ),
TABLE3[COLUMN5] = "XPTO"
) VAR Table2_Column1 = CALCULATETABLE ( VALUES ( TABLE2[COLUMN1] ),
TREATAS ( Table3_Column2, TABLE2[COLUMN2] ) ) RETURN CALCULATETABLE (
TABLE1,
TREATAS ( Table2_Column1, TABLE1[COLUMN1] )
)
Result =
VAR Table3_Column2 =
    CALCULATETABLE ( VALUES ( TABLE3[COLUMN2] ),
TABLE3[COLUMN5] = "XPTO"
) VAR Table2_Column1 = CALCULATETABLE (
VALUES ( TABLE2[COLUMN1] ),
TABLE2[COLUMN2] IN Table3_Column2
) RETURN CALCULATETABLE (
TABLE1,
TABLE1[COLUMN1] IN Table2_Column1
)

Regards,

Owen

 


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

Hi Owen.

 

Thanks for your help.

The first version was works very well.

 

 

Regards,

Andreia

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.