cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
PaulG572 Occasional Visitor
Occasional Visitor

Count rows based on dates from 2 separate tables

Hi community,

 

I am struggling to create a calculated column that counts activities per account between dates from different tables and was wondering if anyone can help? The table schemas are as below with a 1 to many relationship between tblAccounts(AccountID) and tblActivities(AccountID).

 

tblAccounts

AccountID BIGINT PK

OpenDate DATE

DaysOpen INT <- Calculated column DATEDIFF ( 'tblAccounts'[OpenDate], TODAY (), DAY )

 

tblActivities

ActivityID BIGINT PK

AccountID BIGINT FK

ActivityTypeID INT

ActivityDate DATE

 

What I am looking to achieve is if DaysOpen >= 30 then count activities between OpenDate and OpenDate + 30 days. I have tried variations on the below but to no avail.

 

IF ( 'tblAccounts'[DaysOpen] >= 30 , CALCULATE ( COUNTROWS('tblActivities') , DATESBETWEEN('tblActivities'[ActivityDate],'tblAccounts'[OpenDate],DATEADD('tblAccounts'[OpenDate],30,DAY)) , 0 )

 

Any help will be greatly appreciated.

 

Many thanks

Paul

 

 

 

1 REPLY 1
Super User
Super User

Re: Count rows based on dates from 2 separate tables

[# Activities (30D)] = -- column in tblAccounts
var __daysDiff = 30
var __daysOpen = tblAccounts[DaysOpen]
var __account = tblAccounts[AccountID]
var __openDate = tblAccounts[OpenDate]
var __endDate = __openDate + __daysDiff
var __activityCount =
	COUNTROWS(
		FILTER (
			tblActivities,
			tblActivities[AccountID] = __account
			&& tblActivities[Date] >= __openDate
			&& tblActivities[Date] <= __endDate 
		)
	)
RETURN
	if(
		__daysOpen >= __daysDiff,
		__activityCount
	)

Best

D.

Helpful resources

Announcements
October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 148 members 1,543 guests
Please welcome our newest community members: