cancel
Showing results 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

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 Product Customer ReservationID A 1 123 A 5 234 B 5 234 C 7 345 C 9 456 Table 2 Customer ReservationID 1 123 2 123 3 123 4 123 5 234 6 234 7 345 8 345 9 456 10 567 11 567 12 678

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.

1 ACCEPTED SOLUTION
Resolver V

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?

6 REPLIES 6
Resolver V

Try this:

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

Dosn't change result when I filter on Products?

Resolver V

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?

Frequent Visitor

Hi Again

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

Rgds. Claus

Frequent Visitor

Hi

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.

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.

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!

Announcements

#### 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.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### 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
Top Kudoed Authors