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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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])

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.