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
clteh9
Helper I
Helper I

match record with nearest datetime

Hi, 

 

I have 2 tables and would like to match the record with the nearest date. 

 

table 1:

clteh9_0-1669872830810.png

 

table 2:

clteh9_1-1669872844305.png

 

expected result:

clteh9_2-1669872869140.png

 

thanks

1 ACCEPTED SOLUTION
ryan_mayu
Super User
Super User

@clteh9 

pls try this 

Column = 
VAR _l=maxx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table'[end time]>='Table 2'[last_dt]),'Table 2'[last_dt])
VAR _n=minx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table'[end time]<'Table 2'[last_dt]),'Table 2'[last_dt])
VAR _d1='Table'[end time]-_l
VAR _d2=abs(_n-'Table'[end time])
VAR _compare=min(_d1,_d2)
return  if(_compare=_d1,maxx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table 2'[last_dt]=_l),'Table 2'[value]),maxx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table 2'[last_dt]=_n),'Table 2'[value]))

1.PNG

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

1 REPLY 1
ryan_mayu
Super User
Super User

@clteh9 

pls try this 

Column = 
VAR _l=maxx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table'[end time]>='Table 2'[last_dt]),'Table 2'[last_dt])
VAR _n=minx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table'[end time]<'Table 2'[last_dt]),'Table 2'[last_dt])
VAR _d1='Table'[end time]-_l
VAR _d2=abs(_n-'Table'[end time])
VAR _compare=min(_d1,_d2)
return  if(_compare=_d1,maxx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table 2'[last_dt]=_l),'Table 2'[value]),maxx(FILTER('Table 2','Table'[item]='Table 2'[item]&&'Table 2'[last_dt]=_n),'Table 2'[value]))

1.PNG

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.