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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Look up value of earlier date in same table

Hi,

I have a table similar to the below where i am trying to calculate for each row where activity type is "Order" the earliest (based on Date) Activity Type for each customer id.

 

Any ideas on how i could proceed?

 

Activity IdActivity TypeDateCustomer IdDesired result
1Call1/1/20201n/a
2Order3/1/20201Call
3Email2/1/20202n/a
4Call 4/1/20202n/a
5Order6/1/20202Call
6Email4/1/20203n/a
7Order6/1/20203Email

 

 

 

 

 

1 ACCEPTED SOLUTION

Of course, that makes perfect sense. 

 

In that case the DAX is even simpler:

 

Earlier Activity = IF(Activity[Activity Type]="Order", MINX(FILTER(Activity,EARLIER(Activity[Customer Id])=Activity[Customer Id]),Activity[Activity Type]), "n/a")
 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 


If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

9 REPLIES 9
amitchandak
Super User
Super User

@Anonymous , Both as new columns

Last Activity = maxx(filter(Table, [Customer Id]=earlier([Customer Id]) && [Activity ]<earlier([Activity ])),[Activity ])
Last Activity Type = maxx(filter(Table, [Customer Id]=earlier([Customer Id]) && [Activity ]=earlier([Last Activity])),[Activity Type])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

With slight change from @amitchandak this is the final dax.

Two columns

 

Last Activity Date = maxx(filter(Table, [Customer Id]=earlier([Customer Id]) && [Activity Date]<earlier([Activity Date])),[Activity Date]) #This gives you the closest actvity date to each activity 

Last Activity Type = maxx(filter(Table, [Customer Id]=earlier([Customer Id]) && [Activity ]=earlier([Last Activity])),[Activity Type]) #This gives you the closest activity type to each order

Anonymous
Not applicable

It worked, used in the first column Date instead of activity, FILTER(... && Date<earlier(Date),Date).

Thank you so much.

AllisonKennedy
Super User
Super User

@Anonymous  This should do the trick: 

 

Earlier Activity = IF(MAXX(FILTER(Activity,EARLIER(Activity[Customer Id])=Activity[Customer Id]),Activity[Activity Id])=Activity[Activity Id], MINX(FILTER(Activity,EARLIER(Activity[Customer Id])=Activity[Customer Id]),Activity[Activity Type]), "n/a")
 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 


If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

AllisonKennedy
Super User
Super User

Why n/a? Are you only wanting a result for the latest activity of each customer?

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Anonymous
Not applicable

sorry that was misleading, n/a because i need to calculate the above only when the activity type is "order" since i want to establish which type of activity has caused the order. 

Of course, that makes perfect sense. 

 

In that case the DAX is even simpler:

 

Earlier Activity = IF(Activity[Activity Type]="Order", MINX(FILTER(Activity,EARLIER(Activity[Customer Id])=Activity[Customer Id]),Activity[Activity Type]), "n/a")
 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 


If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Anonymous
Not applicable

First of all thank you for supporting and trying to help, this is definitely much appreciated.

I will keep as solved anyway your second answer becuase it might help people that want to see what is the first/min date or activity.

 

there was two problems in my case,

 

I was sort of looking for the max preceeding the order activity

Also it was taking the absolute min and if more than one order per customer it would only take one value.

 

Thanks anyway i wish you a great day.

F.

Your original post said 'earliest' so I took that to mean the min(), but glad you got it working.

 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 

 

If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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.