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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
farinabrn
Regular Visitor

Distinct between 2 tables

Hi everyone,

 

I am start to learning PBI and on my work i have a problem, Can you help me?

 

I'm trying to build a dashboard with a stacked bar chart where will be shown a percentage of stores with missing products per week_number, in other words, i need to distinct the ID of PRODUCT relation with PRODUCT_ID grouped in SALES relation, this relation will be the NOT SOLD product. Following below an example:

 

 graf.png

 

 

 

 

 

 

 

 

 

 

 

A SALES table contains info of each product sold every day, with the columns: product_code, sale_quantity, volume, date that are grouped by the fields: year, week_number, region, company, store and PRODUCT_ID. Then Product table has all product registers.

 

I would to do a calculated table trying group the SALES table using SUMMARIZE like that:

SALES_GRP = SUMMARIZE(SALES; SALES[YEAR]; SALES[WEEK]; SALES[REGION]; SALES[COMPANY]; SALES[STORE])

and make a relation with SALES (I try the SUBSTITUTEWITHINDEX but didn't work) to catch a table of one column containing all week products to use EXCEPT and ALL in DAX some like that:

 

DISTINCTCOUNT (
   EXCEPT(
      ALL( PRODUCT[ID] ),
      "PRODUTCTS SOLD IN A WEEK PER STORE.."
   )
)

 

I have wrote this sql query trying to find a way to convert it to DAX but i'm noob lol

 

SELECT v.YEAR, v.WEEK, v.REGION, v.COMPANY, v.STORE
((SELECT COUNT(1) FROM PRODUCT) - COUNT(DISTINCT p.ID)) AS prod
FROM SALES v
LEFT JOIN PRODUCT p ON p.ID = v.PRODUCT_ID
GROUP BY v.YEAR, v.WEEK, v.REGION, v.COMPANY, v.STORE

1 ACCEPTED SOLUTION
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @farinabrn,


I have wrote this sql query trying to find a way to convert it to DAX but i'm noob lol

 

SELECT v.YEAR, v.WEEK, v.REGION, v.COMPANY, v.STORE
((SELECT COUNT(1) FROM PRODUCT) - COUNT(DISTINCT p.ID)) AS prod
FROM SALES v
LEFT JOIN PRODUCT p ON p.ID = v.PRODUCT_ID
GROUP BY v.YEAR, v.WEEK, v.REGION, v.COMPANY, v.STORE


Instead of converting the sql query to DAX, you should be able to directly use the sql query in SQL statement option when GetData to a new table to your model. Smiley Happy

 

sqls.PNG

 

Regards

View solution in original post

1 REPLY 1
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @farinabrn,


I have wrote this sql query trying to find a way to convert it to DAX but i'm noob lol

 

SELECT v.YEAR, v.WEEK, v.REGION, v.COMPANY, v.STORE
((SELECT COUNT(1) FROM PRODUCT) - COUNT(DISTINCT p.ID)) AS prod
FROM SALES v
LEFT JOIN PRODUCT p ON p.ID = v.PRODUCT_ID
GROUP BY v.YEAR, v.WEEK, v.REGION, v.COMPANY, v.STORE


Instead of converting the sql query to DAX, you should be able to directly use the sql query in SQL statement option when GetData to a new table to your model. Smiley Happy

 

sqls.PNG

 

Regards

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.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

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