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 the nearest record from another table

I have 2 tables and i would like to match the nearest record from another table by comparing table1's access_time and table2's permit_time and its category.  thanks for the help

 

table1:

itemcategoryaccess_time
1A2022-11-23 08:12:00
1B2022-11-23 09:12:00
2C2022-11-22 13:12:00
3D2022-11-22 14:12:00

 

table2:

itemcategorypermit_timevalue
1A2022-11-23 09:12:0011
1A2022-11-23 08:30:0012
1B2022-11-22 08:30:0013
2C2022-11-22 13:12:0021
3D2022-11-22 14:12:0031
3D2022-11-22 14:30:0032

 

expected result:

itemcategoryaccess_timepermit timevalue
1A2022-11-23 08:12:002022-11-23 08:12:0012
1B2022-11-23 09:12:002022-11-22 08:30:0013
2C2022-11-22 13:12:002022-11-22 13:12:0021
3D2022-11-22 14:12:002022-11-22 14:12:0031
2 REPLIES 2
FreemanZ
Super User
Super User

hi @clteh9 

 

you may also try with measures, by plotting a table visual with all table1 columns and two measures like:

permit_time2 = 
VAR _access_time=MAX(Table1[access_time])
VAR _gapmin =
    MINX(
        Table2,
        ABS(Table2[permit_time]-_access_time)
    )
VAR result =
    MAXX(
        FILTER(
           Table2,
            ABS(Table2[permit_time]-_access_time)=_gapmin
        ),
       Table2[permit_time]
    )
RETURN
    result

and

value2 = 
VAR _access_time=MAX(Table1[access_time])
VAR _gapmin =
    MINX(
        Table2,
        ABS(Table2[permit_time]-_access_time)
    )
VAR result =
    MAXX(
        FILTER(
           Table2,
            ABS(Table2[permit_time]-_access_time)=_gapmin
        ),
       Table2[value]
    )
RETURN
    result

it worked like:

FreemanZ_2-1681132922520.png

 

FreemanZ
Super User
Super User

hi @clteh9 

Supposing your tables are related like:

FreemanZ_0-1681132200662.png

 

try to add two columns one after another like:

permit_time = 
VAR _table=RELATEDTABLE(Table2)
VAR _gapmin =
    MINX(
        _table,
        ABS(Table2[permit_time]-Table1[access_time])
    )
VAR result = 
    MAXX(
        FILTER(
            _table,
            ABS(Table2[permit_time]-Table1[access_time])=_gapmin
        ),
    Table2[permit_time] 
    )
RETURN 
    result

and

value = 
VAR _table=RELATEDTABLE(Table2)
VAR result = 
    MAXX(
        FILTER(
            _table,
            Table2[permit_time]=Table1[permit_time]
        ),
    Table2[value] 
    )
RETURN
    result

 

it worked like:

FreemanZ_1-1681132270648.png

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.