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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DanVelus
New Member

Nested DAX query with filter conditions

Hi there,

I am fairly new to DAX, I would really appreciate your assistance, please.

I am trying to filter out the number of customers that belong to both zones. The table looks like this:

Table: Customers
Customer_id     Zone_id

1                         A

2                         A

3                         A

3                         B

4                         A

4                         B


Expected Result:

(2 customers belong to both zones A & B)

Equivalent SQL would look something like this:

SELECT COUNT(DISTINCT(Customer_id) 

FROM customers
WHERE

 Zone_id = A

AND Customer_id IN

           (SELECT Customer_id

            FROM Customers
            WHERE Zone_id = B)

 

Could you please help me with an equivalent DAX for this? I tried various options with no luck.


Thank you so much in advance.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

hi @DanVelus ,

 

I try to understand the model you are talking about and hope to get the result you want in the end.

Please create two new table as follows firstly:

01.png

02.png

Then, create a measure:

03.png

Result should look like this:

04.png

You can also create a new measure as follows:

05.png

Result should look like this:

04.png

Hope it helps!

 

Best regards,

Community Support Team  CGao

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

hi @DanVelus ,

 

I try to understand the model you are talking about and hope to get the result you want in the end.

Please create two new table as follows firstly:

01.png

02.png

Then, create a measure:

03.png

Result should look like this:

04.png

You can also create a new measure as follows:

05.png

Result should look like this:

04.png

Hope it helps!

 

Best regards,

Community Support Team  CGao

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

 

smpa01
Super User
Super User

@DanVelus  Can you please provide some sample data where it returns BLANK, so that I can debug if required

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
wdx223_Daniel
Super User
Super User

New Measure=COUNTROWS(FILTER(VALUES('Table'[Customer_id]),CALCULATE(COUNTROWS(FILTER({"A","B"},[Value] IN VALUES('Table'[Zone_id]))))=2))

smpa01
Super User
Super User

@DanVelus  equivalent DAX Measure is this

 

Measure =
VAR _a =
    CALCULATE (
        MAX ( 'Table'[Customer_id] ),
        FILTER ( VALUES ( 'Table'[Zone_id] ), 'Table'[Zone_id] = "A" )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[Customer_id] ),
        FILTER ( 'Table', 'Table'[Customer_id] = _a && 'Table'[Zone_id] = "B" )
    )

 

 

smpa01_0-1639445993770.png

 

a more dynamic approach would be following

Measure2 =
VAR _select = { "A", "B" }
VAR _count1 =
    COUNTX ( _select, [Value] )
VAR _count2 =
    CALCULATE (
        COUNT ( 'Table'[Customer_id] ),
        'Table'[Zone_id] IN _select,
        ALLEXCEPT ( 'Table', 'Table'[Customer_id] )
    )
RETURN
    IF ( _count1 = _count2, MAX ( 'Table'[Customer_id] ) )

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hey, @smpa01 Thank you so much for both of your responses. I tried all your solutions, I only getting BLANK. I am not getting the expected answer. Here are the some of things I tried:

Table name is 'groups'; Columns: user_id & zone_id
Users_inBothGroup =
VAR _select = {123, 456}
VAR _count1 =
COUNTX (_select, [Value])
VAR _count2 =
CALCULATE(
COUNT(groups[user_id]),
groups[zone_id] IN _select,
ALLEXCEPT(groups, groups[user_id])

)
RETURN
IF( _count1 = _count2, MAX(groups[user_id]) )

The Result: BLANK


VAR _a =
CALCULATE(
MAX(groups[user_id]),
FILTER(VALUES(groups[zone_id]), groups[zone_id] = 123)
)
RETURN
CALCULATE(
MAX(groups[user_id]),
FILTER(groups, groups[user_id] = _a && groups[zone_id] = 456)
)

Result: BLANK

I am not sure what I am doing wrong.

Please help

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.