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

Showing data like a Matrix using T-SQL

Hello everyone, 

I am new to Power BI, I hope you can help me with this question.

 

I have a data model like the following one (where I have two fact tables and one dimension table,  )

 
 

Data modelData model

 

imagen.png

Using a matrix I have a report like above.  How could I perform the equivalent of that  using T-SQL?

 

 

I've tried to do it but only managed to get one side

imagen.png

 

Thanks in advance

1 ACCEPTED SOLUTION

I see, the tables are at different grain.

 

Try this SQL

 

SELECT
IsNull(a1.SalesTerritoryKey, a2.SalesTerritoryKey) as SalesTerritoryKey
, a1.ReturnQuantity
, a2.OrderQuantity
FROM (
SELECT tlk.SalesTerritoryKey
, SUM(s.OrderQuantiry) OrderQuantity
FROM AW_Territories_Lookup tlk
INNER JOIN AW_Sales s on tlk.SalesTerritoryKey = s.TerritoryKey
group by tlk.SalesTerritoryKey
) a2
LEFT JOIN (
SELECT tlk.SalesTerritoryKey
, SUM(r.ReturnQuantity) ReturnQuantity
FROM AW_Territories_Lookup tlk
INNER JOIN AW_Returns r on tlk.SalesTerritoryKey = r.TerritoryKey
GROUP BY tlk.SalesTerritoryKey
) a1 on a1.SalesTerritoryKey = a2.SalesTerritoryKey
Order by SalesTerritoryKey

View solution in original post

8 REPLIES 8
amitchandak
Super User
Super User

@arturopaz , what is the issue you are facing. A left join is with +0 or using Option, Show Item with no data.

Can you explain the issue you are facing. Are qty wrong?

Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data?

@amitchandak  what I am looking for is to show the data as shown in the Matrix but using T-SQL within MSSQL. (I share sample data)  Thanks in advance.

 

arturopaz_1-1604627391562.png

 

 

AW_Returns  (Fact_table)

AW_Returns.png

 

AW_Sales (Fact_table)

AW_Sales.png

 

AW_Territories_Lookup (Dimension table)

AW_Territories_Lookup.png

 

I see, the tables are at different grain.

 

Try this SQL

 

SELECT
IsNull(a1.SalesTerritoryKey, a2.SalesTerritoryKey) as SalesTerritoryKey
, a1.ReturnQuantity
, a2.OrderQuantity
FROM (
SELECT tlk.SalesTerritoryKey
, SUM(s.OrderQuantiry) OrderQuantity
FROM AW_Territories_Lookup tlk
INNER JOIN AW_Sales s on tlk.SalesTerritoryKey = s.TerritoryKey
group by tlk.SalesTerritoryKey
) a2
LEFT JOIN (
SELECT tlk.SalesTerritoryKey
, SUM(r.ReturnQuantity) ReturnQuantity
FROM AW_Territories_Lookup tlk
INNER JOIN AW_Returns r on tlk.SalesTerritoryKey = r.TerritoryKey
GROUP BY tlk.SalesTerritoryKey
) a1 on a1.SalesTerritoryKey = a2.SalesTerritoryKey
Order by SalesTerritoryKey

@sevenhillsThanks you very much, that is what I was looking for, I wanted to replicate what a Matrix did internally in Power BI (or Pivot Table on Excel).

amitchandak
Super User
Super User

@arturopaz , One Option is to add +0 to both you measures

The second Option to use Show items with No Data

ShowItemwithoutdata.JPG

sevenhills
Super User
Super User

SELECT tlk.SalesTerritoryKey
, SUM(r.ReturnQuantity) ReturnQuantity
, SUM(s.OrderQuantiry) OrderQuantity
FROM AW_Territories_Lookup tlk
LEFT JOIN AW_Returns r on tlk.SalesTerritoryKey = r.TerritoryKey
LEFT JOIN AW_Sales s on tlk.SalesTerritoryKey = s.TerritoryKey
GROUP BY tlk.SalesTerritoryKey
Order by tlk.SalesTerritoryKey

 


With rollup -- if you want totals

@sevenhills Thanks for your answer, but running the query I get this:

arturopaz_0-1604627365731.png

 


I would like it to show like this:

arturopaz_1-1604627391562.png

 

camargos88
Community Champion
Community Champion

@arturopaz ,

 

If you remove the Order Quantity column, you only get the matching rows. To show also the nonmatching you need to select the option "Show Items with no data" clicking on the arrow of SalesTerritoryKey and checking the option "Show Items with no data".



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



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.