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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Countrows of all customer appointments using the contract start and end date from different table.

Dear all, 
My first post on the forum, so I hope I manage to get my question across. 
I'd like to calculate the number of appointments each customer makes within their contract. 

I've got two tables T_Appointments and T_Contracts, which are related through a customer table (T_Customer with unique customer numbers, not shown). 

T_Appointments 

Date_aCustomer_idType_a
15-06-2023123call
16-06-2023123visit
17-06-2023402call
19-06-2023123call
15-01-2024402call
20-01-2024683visit
12-02-2024123call
15-03-2024123call
06-04-2024123visit

 

T_Contracts (Contract details) 

Customer_idContract_idContract_startContract_end
12312501-01-201401-01-2015
12335715-06-202315-08-2023
12391501-01-202415-06-2024
40247101-01-202415-06-2024
68312001-01-201401-01-2015
68347201-01-202401-01-2025


-----------------------------------------------------------------------------------------------------------
To answer my question I tried to make a measure that would return:  

Contract_idAppointment count
1250
3573
9152
4711
1200
4721


Or alternatively, create a new column in table_a that contains the contract_id corresponding to the customer and the date. 

Date_aCustomer_idType_aContract_id
15-06-2023123call357
16-06-2023123visit357
17-06-2023402callnull



The closest I could get was calculating the number of appointments for the the last contract. 
contract_appointment_count SUMX(SUMMARIZE('T_customer','T_customer'[Customer_id],"_1",COUNTX(FILTER('T_Appointment','T_appointment'[Date_a] >= MAX(T_Contracts[Contract_start]&& 'T_Appointment','T_appointment'[Date_a] <= MAX(T_Contracts[Contract_end])),'Table_a'[customer_id])),[_1])

 
Thanks! 
Roos

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

Based on my testing, please try the following methods again:

1.Create the simple tables.

vjiewumsft_0-1718846109826.png

vjiewumsft_1-1718846121768.png

2.Create the new measure to count rows.

Appointment_Count = 
VAR _filter = SELECTCOLUMNS(FILTER(CROSSJOIN(ALLSELECTED('T_Appointments'),ALLSELECTED('T_Contracts')),'T_Contracts'[Contract_start]<='T_Appointments'[Date_a]&&'T_Contracts'[Contract_end]>='T_Appointments'[Date_a]&&'T_Appointments'[Customer_id]='T_Contracts'[Customer_id]),"_CustomID",'T_Appointments'[Customer_id],"_contactID",'T_Contracts'[Contract_id])

RETURN
COUNTX(FILTER(_filter,[_contactID] = MAX('T_Contracts'[Contract_id])),[_contactID]) + 0

3.Drag the measure into the table visual. The result is shown below.

vjiewumsft_2-1718846134601.png

 

 

 

Best Regards,

Wisdom Wu

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

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @Anonymous ,

Based on my testing, please try the following methods again:

1.Create the simple tables.

vjiewumsft_0-1718846109826.png

vjiewumsft_1-1718846121768.png

2.Create the new measure to count rows.

Appointment_Count = 
VAR _filter = SELECTCOLUMNS(FILTER(CROSSJOIN(ALLSELECTED('T_Appointments'),ALLSELECTED('T_Contracts')),'T_Contracts'[Contract_start]<='T_Appointments'[Date_a]&&'T_Contracts'[Contract_end]>='T_Appointments'[Date_a]&&'T_Appointments'[Customer_id]='T_Contracts'[Customer_id]),"_CustomID",'T_Appointments'[Customer_id],"_contactID",'T_Contracts'[Contract_id])

RETURN
COUNTX(FILTER(_filter,[_contactID] = MAX('T_Contracts'[Contract_id])),[_contactID]) + 0

3.Drag the measure into the table visual. The result is shown below.

vjiewumsft_2-1718846134601.png

 

 

 

Best Regards,

Wisdom Wu

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

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.