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

SELECT in WHERE clause equivalent in Power BI

Hi,

 

I have a table with following columns

user_id, product.

Sample Data:

Buyer IDProduct
1Avocado
1Bread
1Cream Cheese
1Ham
2Avocado
2Bread
3Ham
3Bread
3Chicken

 

I want to recreate this SQL query in PowerBI:
SELECT product, COUNT(DISTINCT buyer_id)

FROM table

WHERE buyer_id IN (SELECT buyer_id FROM table WHERE product = @@product)

;

 

For example,
if I select "Avocado" in a filter Pane, I want to display how many users who buy avocado also bought other products. Here's the sample expected visual:

ProductTotal BuyersOverlap Buyers
Avocado22
Bread32
Cream Cheese11
Ham21
Chicken10

In this visual,
Total Buyers for "Chicken" is 1 because Buyer_id 3 bought chicken. But Overlap Buyers is 0 because even though buyer_id 3 bought chicken, they did not buy avocado.

 

I have tried multiple methods like nested FILTERS, creating a separate table but it doesn't solve my issue. Can someone please help in developing a DAX equivalent for the SQL query above?

1 ACCEPTED SOLUTION
v-chenwuz-msft
Community Support
Community Support

Hi @ltekchandani 

 

This is some thing like Association Analysis. 

In power bi desktop, you need to create a new table to get product field for slicer due to having no inactive with table visual which you used to display the result. Via the following expression:

Product = VALUES('Table'[Product])

 After that , to display Total Buyers drag the Buyer ID field into table visual and set it Count(distinct)

vchenwuzmsft_0-1636614128384.png

Or this measure :

Total Buyers = DISTINCTCOUNT('Table'[Buyer ID])

 

Then the following measure to calculate overlap buyers:

Overlap Buyers =
VAR _s_filter =
    FILTER( ALL( 'Table' ), [Product] = SELECTEDVALUE( 'Product'[Product] ) )
VAR _1 =
    DISTINCT( 'Table'[Buyer ID] )
VAR _2 =
    GROUPBY( _s_filter, [Buyer ID] )
VAR _3 =
    DISTINCT( UNION( _1, _2 ) )
RETURN
    COUNTROWS( _1 ) + COUNTROWS( _2 ) - COUNTROWS( _3 )

For the slicer visual , use new table product[product] as it's vaule. 

the result:

vchenwuzmsft_1-1636614366480.png

I put my pbix file in the attachment you can refer

 

Best Regards

Community Support Team _ chenwu zhu

 

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

 

View solution in original post

2 REPLIES 2
v-chenwuz-msft
Community Support
Community Support

Hi @ltekchandani 

 

This is some thing like Association Analysis. 

In power bi desktop, you need to create a new table to get product field for slicer due to having no inactive with table visual which you used to display the result. Via the following expression:

Product = VALUES('Table'[Product])

 After that , to display Total Buyers drag the Buyer ID field into table visual and set it Count(distinct)

vchenwuzmsft_0-1636614128384.png

Or this measure :

Total Buyers = DISTINCTCOUNT('Table'[Buyer ID])

 

Then the following measure to calculate overlap buyers:

Overlap Buyers =
VAR _s_filter =
    FILTER( ALL( 'Table' ), [Product] = SELECTEDVALUE( 'Product'[Product] ) )
VAR _1 =
    DISTINCT( 'Table'[Buyer ID] )
VAR _2 =
    GROUPBY( _s_filter, [Buyer ID] )
VAR _3 =
    DISTINCT( UNION( _1, _2 ) )
RETURN
    COUNTROWS( _1 ) + COUNTROWS( _2 ) - COUNTROWS( _3 )

For the slicer visual , use new table product[product] as it's vaule. 

the result:

vchenwuzmsft_1-1636614366480.png

I put my pbix file in the attachment you can refer

 

Best Regards

Community Support Team _ chenwu zhu

 

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

 

amitchandak
Super User
Super User

@ltekchandani , Create measure like first one , if you have independent product table, Seond if product is joined with table or used from tbale 

 

measure =
var _Tab = summzarize(filter(Table, Table[Product] in allselected(product[product])), Table[Buyer])
return
calculate(distinctcount(Table[buyer Id]), filter(Table, Table[Buyer] in _tab))

 

measure =
var _Tab = summzarize(filter(all(Table), Table[Product] in allselected(product[product])), Table[Buyer])
return
calculate(distinctcount(Table[buyer Id]), filter(all(Table), Table[Buyer] in _tab))

 

 

plot with product in visual

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.