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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Astralpro20
New Member

Joining Multiple Tables with subquery

Hi,

I need to join 3 tables with subquery to count items in each status.

 

In SQL:

SELECT d.statusCode, d.statusDesc, COUNT(d.statusCode) as Total FROM tbItems a
    INNER JOIN tbStatusHistory b on a.itemid = b.itemid
        and b.statusid = (select top 1 statusid from statusHistory x
                   where x.itemid = a.itemid and x.status_date = (select max(status_date) from statusHistory y where y.itemid = x.itemid))
    INNER JOIN tbStatus d on d.statusCode = b.statusCode
WHERE START_DATE >= @dtfrom and START_DATE <= @dtto
GROUP BY d.statusCode, d.statusDesc, d.seqno
ORDER BY d.seqno

 

SQL Results:

1.jpg

 

My Chart using HighChart. I want to do this in Power BI.

My Chart using HighChart. I want to do this in Power BIMy Chart using HighChart. I want to do this in Power BI

 

Thank you in advance. 

1 REPLY 1
kentyler
Solution Sage
Solution Sage

Looking at your SQL you would end up with 2 tables in Power BI

tblItems

and

tblStatusHistory
the value from the status lookup table with the status codes should be be merged into tlbStatusHistory before you load the data. PowerBI does not mind redundant fields and it is common practice to merge all lookup tables into the transactional tables.

It looks like you want to work with only the most recent record for an item in status history, so you should do a view in sql server that selects the max date and returns only those records. Then load the view into power bi rather than the original table. The rule is to do as much work as possible in sql server... power bi can be complicated enough the way it is.

Then you will end up with your LatestStatusHistory view as your fact table and your tblItem as your dimension table. You should have a 1 to many relationship between items and status history. 
Data models in Power BI should be star schemas, just like in data warehousing. It makes all your DAX MUCH SIMPLER.

This is a big area to talk about. If you'd like to discuss it more send me a day and time and we can do a screen share.

 

I'm a personal Power Bi Trainer I learn something every time I answer a question

The Golden Rules for Power BI

  1. Use a Calendar table. A custom Date tables is preferable to using the automatic date/time handling capabilities of Power BI. https://www.youtube.com/watch?v=FxiAYGbCfAQ
  2. Build your data model as a Star Schema. Creating a star schema in Power BI is the best practice to improve performance and more importantly, to ensure accurate results! https://www.youtube.com/watch?v=1Kilya6aUQw
  3. Use a small set up sample data when developing. When building your measures and calculated columns always use a small amount of sample data so that it will be easier to confirm that you are getting the right numbers.
  4. Store all your intermediate calculations in VARs when you’re writing measures. You can return these intermediate VARs instead of your final result  to check on your steps along the way.

 

 





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.