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
zoemostert
Helper I
Helper I

Count, Where and Join

I've been trying a lot of things to fix my problem and searched the entire website, but couldn't figure out how to fix it. 

 

So I kinda know SQL and I believe that my query is very simple in SQL, though I still don't know how to do it in DAX. 

 

I have two tables (Stock and Sold Items) wich are related trough anohter table with ItemID's. I want to see thow many radio's have been sold in the last 1,5 year (Date in Sold Items table). There are 4 types of Categorys (in Stock table) that have radio's. I guess the code in SQL would be something like:

 

SELECT COUNT (Order_ID) AS Radio_Count

FROM so Sold_Items

JOIN i Item_ID 

ON so.Item_ID=i.Item_ID

JOIN s Stock

ON i.Item_ID=s.Item_ID

WHERE s.Category = "cat1"

OR "cat2"

OR "cat3"

OR "cat4"

 

Eventually I would like to see something like this:voorbeeld.jpg

 

 

Please help me!? 

1 ACCEPTED SOLUTION
v-sihou-msft
Employee
Employee

@zoemostert

 

I assume you already build the relationships between Sold_Items, Item_ID and Stock tables on "XXX_ID" column. Then you just need to create a measure to get the count calculation, and specify correct filter context to achieve where clause in DAX.

 

=
CALCULATE (
    COUNT ( Sold_Items[Order_ID] ),
    FILTER (
        Sold_Items,
        RELATED ( Stock[Category] ) = "cat1"
            || RELATED ( Stock[Category] ) = "cat2"
            || RELATED ( Stock[Category] ) = "cat3"
    )
)

For more details, please refer to article below:

 

From SQL to DAX: Filtering Data

 

Regards,

View solution in original post

3 REPLIES 3
v-sihou-msft
Employee
Employee

@zoemostert

 

I assume you already build the relationships between Sold_Items, Item_ID and Stock tables on "XXX_ID" column. Then you just need to create a measure to get the count calculation, and specify correct filter context to achieve where clause in DAX.

 

=
CALCULATE (
    COUNT ( Sold_Items[Order_ID] ),
    FILTER (
        Sold_Items,
        RELATED ( Stock[Category] ) = "cat1"
            || RELATED ( Stock[Category] ) = "cat2"
            || RELATED ( Stock[Category] ) = "cat3"
    )
)

For more details, please refer to article below:

 

From SQL to DAX: Filtering Data

 

Regards,

Thank u! That worked perfectly 🙂

Greg_Deckler
Super User
Super User

Import your 3 tables and build the relationships in the relationship editor.

 

Then, create a column or measure that goes something like this:

 

Measure = CALCULATE(COUNT([Column]),RELATED(Table))

Really tough to be more specific without sample data.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors