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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
shanemc1
Frequent Visitor

Convert SQL to Dax (Count Distinct using Join)

I have the following table structure in my database.

 

Enquiry Table which stores enquiries

EnquiryVenue Table which stores venues that are attached to an enquiry

Venue Table which stores venue table

VenuePremium table which stores venues that are on our premium system.

 

I want to count the number of Enquiries that have a venue attached to it that is in the premium table.

 

The following sql captures this data 

select count(distinct fkEnquiries)
from enquiries_venues ev
inner join VenuePremium vp
on ev.fkVenue = vp.VenuePremiumId

 In Power BI I am doing the following:

CALCULATE(DISTINCTCOUNT('Enquiries_Venues'[fkEnquiries]),SUMMARIZE('VenuePremium', VenuePremium[VenueId]) )

This is giving me a much larger count than expected ?

 

Any suggestions on how this should work?

 

Thanks in advance

1 ACCEPTED SOLUTION

HI @Anonymous ,

 

Yes it seems to be running a left join because when I do a left join in SQL I am presented with the same number.

 

I have tried using a NaturalInnerJoin and it worked perfect thanks for your help.

 

Shane

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @shanemc1,

 

I think LEFT JOIN is taking place when you are using SUMMARIZE function in dax.

Can you re-run that SQL query with left join and see the count?

I think you can modify your dax query to use NATURALINNERJOIN(enquiries_venues,VenuePremium

 

Thanks,
Amit

HI @Anonymous ,

 

Yes it seems to be running a left join because when I do a left join in SQL I am presented with the same number.

 

I have tried using a NaturalInnerJoin and it worked perfect thanks for your help.

 

Shane

Stachu
Community Champion
Community Champion

There is an active direct join between these tables, correct?

try this code:

CALCULATE(DISTINCTCOUNT('Enquiries_Venues'[fkEnquiries]),  VenuePremium[VenueId]) <> BLANK())


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi @Stachu ,

 

I tried this and it just returned true. The  tables are not directly related in power bi but the venue id in both do correspond to each other 

Stachu
Community Champion
Community Champion

ah, if the tables were not joined then my code will not work for sure


@Anonymous has provided you with working solution, so that's the most important 🙂

 

here is a good more in depth aritcle on using joins in DAX and the implications
https://www.sqlbi.com/articles/from-sql-to-dax-joining-tables/



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors