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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Ramp85
Frequent Visitor

Count number of rows in another table with same ID

Hi All

 

I have a quite simple (I think) DAX question, that I hope you can help me with. Tried to find similar in the forum, without luck.

 

I hav two tables:

Table 1  
   
ProductCustomerReservationID
A1123
A5234
B5234
C7345
C9456
   
Table 2  
CustomerReservationID 
1123 
2123 
3123 
4123 
5234 
6234 
7345 
8345 
9456 

10

567 
11567 
12678 

 

There is a relation between the to tables, on Customer - Many-to-one.

 

Now:

If I have a filter on table 1 with Product = "A",

I wan't to create a meassure to count how many related customers in Table2 are on a reservation where another customer bought this product. 

So, in above example, if I filter on product "A", I can see that 6 customers are on a reservations that bought this product and thereby my desired result is "6".

If there is no filtering done on product level, I expect the result to be 11, as there is 11 customers on the reservations which have got any kind of product. (ie. reservation 234 bought two products, customer 10-12 is on reservations that are not present in table1)

 

I've been walking around, some Countrows, including a calculate with allselected, but I haven't been able to find the solution yet.

 

Looking forward to see your suggestions. Thanks in advance!

 

1 ACCEPTED SOLUTION

I created the tables exactly as described and it works perfectly with a slicer or filter on product. So there is probably more to your model than you have described above.
Or did you create a computed column instead of a measure?

View solution in original post

6 REPLIES 6
sjoerdvn
Resolver V
Resolver V

Try this:

# related customers = CALCULATE(DISTINCTCOUNT('Table 2'[Customer]), TREATAS(VALUES('Table 1'[ReservationID]), 'Table 2'[ReservationID]))

Dosn't change result when I filter on Products?

I created the tables exactly as described and it works perfectly with a slicer or filter on product. So there is probably more to your model than you have described above.
Or did you create a computed column instead of a measure?

Hi Again

 

Sry, I must have set something wrong in the first try, now it works like a charm.
Thanks a lot!

 

Rgds. Claus

Ramp85
Frequent Visitor

Hi

 

Thanks for your reply.

 

Work almost as it should. Everything is fine when choosing a single product. Only issue, is that if I choose two products. For example A & B I get 11 as a result, where I expect 8.

v-yaningy-msft
Community Support
Community Support

Hi, Ramp85

May I ask if this is the expected output you are looking for? Based on your description, I have created a measure to achieve the effect you are looking for. Following picture shows the effect of the display.

vyaningymsft_0-1701855312086.png

 

Measure:

RelatedCustomerCount =

IF (

    ISBLANK ( SELECTEDVALUE ( Table1[Product] ) ),

    COUNTROWS ( Table2 )

        - ( COUNT ( Table1[ReservationID] ) - DISTINCTCOUNT ( Table1[ReservationID] ) ),

    CALCULATE (

        COUNTROWS ( 'Table2' ),

        FILTER (

            'Table2',

            'Table2'[ReservationID] IN VALUES ( 'Table1'[ReservationID] )

        ),

        ALLSELECTED ( 'Table1' )

    )

)

If this does not work, could you please share some sample data without sensitive information and expected output.
How to provide sample data in the Power BI Forum - Microsoft Fabric Community

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors