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

View all the Fabric Data Days sessions on demand. View schedule

Reply
Anonymous
Not applicable

DAX Help

Hi,

I have 2 tables as shown below:

firstpurchase.PNG

I am looking to calculate a measure for number of events attended by each contact before the 1st purchase made, i.e 

distinctcount(eventname) where [FirstPurchaseDate] > [Event date] at the row level context.

I tried using the "Calculate" function, but I am not able to use the dates from 2 different tables in the filter section.

 

I am new to writing script in DAX. I greatly appreciate any help.

 

Thanks

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Assuming the table on the left is Table1 and the table on the right is Table2...

This measure belongs in Table1.  You will use the ContactID from Table1 in your visual.

 

Try this:

 

# Events Before First Purchase = 
// Get the current ContactID based on the filter context
VAR Current_ContactID =
	SELECTEDVALUE(Table1[ContactID])

// Get the FirstPurchaseDate for that ContactID
VAR PurchaseDate = 
	CALCULATE(
		MAX(Table1[Purchase Date])
		,Table1[ContactID] = Current_Contact)

// Get ALL the records in Table2 for the given ContactID 
// that happened before the purchase date
VAR EventsAttended_ALL = 
	FILTER(
		Table2
		,Table2[ContactID] = Current_ContactID
			&& Table2[EventDate] < PurchaseDate
	)

//Get the unique list of Table2[EventName] based on EventsAttended_ALL
VAR EventsAttended_DISTINCT = 
	CALCULATETABLE(
		VALUES(Table2[EventName])
		,EventsAttended_ALL
	)

RETURN
COUNTROWS(EventsAttended_DISTINCT)

 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Assuming the table on the left is Table1 and the table on the right is Table2...

This measure belongs in Table1.  You will use the ContactID from Table1 in your visual.

 

Try this:

 

# Events Before First Purchase = 
// Get the current ContactID based on the filter context
VAR Current_ContactID =
	SELECTEDVALUE(Table1[ContactID])

// Get the FirstPurchaseDate for that ContactID
VAR PurchaseDate = 
	CALCULATE(
		MAX(Table1[Purchase Date])
		,Table1[ContactID] = Current_Contact)

// Get ALL the records in Table2 for the given ContactID 
// that happened before the purchase date
VAR EventsAttended_ALL = 
	FILTER(
		Table2
		,Table2[ContactID] = Current_ContactID
			&& Table2[EventDate] < PurchaseDate
	)

//Get the unique list of Table2[EventName] based on EventsAttended_ALL
VAR EventsAttended_DISTINCT = 
	CALCULATETABLE(
		VALUES(Table2[EventName])
		,EventsAttended_ALL
	)

RETURN
COUNTROWS(EventsAttended_DISTINCT)

 

AlB
Community Champion
Community Champion

Hi @Anonymous 

Try this:

1. Create a 1-many unidirectional relationship between PurchasesTable and EventsTable through ContactID.

2. Place PurchasesTable[ContactID] in the rows of a matrix visual. 

3. Create this measure and place it in values of the matrix

 

NumEvents =
CALCULATE (
    DISTINCTCOUNT ( EventsTable[EventName] ),
    FILTER (
        ALL ( EventsTable[EventDate] ),
        EventsTable[EventDate] < SELECTEDVALUE ( PurchasesTable[FirstPurchaseDate] )
    )
)

 

 

Anonymous
Not applicable

@AlB Thanks for the response.

I have tried the suggested solution and the following error is being thrown:

 

error.PNG

Any thoughts on that?

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.

Top Solution Authors
Top Kudoed Authors