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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
darianle
Regular Visitor

Find the nearest date from another table based on ID

https://www.dropbox.com/s/9es5uzaxjtrjm9l/SampleDATA.pbix?dl=0

 

 

Hello,


I'm looking for the closest date in (Table1) to the value (in Table2) based on the ID. 

[the date should be greater than and not equal to]

 

I'm using the dax below, but the results are finding the closest value and ignoring the ID.

 

TIME2 =
    VAR enter_ = Table2[MessageDate]
    RETURN
        TOPN(
            1,
            CALCULATETABLE(
                DISTINCT(Table1[MessageDate2]),
                ALLEXCEPT(Table1, Table1[ID]),
                Table1[MessageDate2] > enter_
            ),
         Table1[MessageDate2] - enter_, asc)
         

 

please help, 

 

Expected Results:

darianle_1-1687348689635.png

 

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @darianle ,

 

Please try:

Column =
VAR _a =
    MINX (
        FILTER (
            'Table1',
            [ID] = EARLIER ( Table2[ID] )
                && DATEDIFF ( [MessageDate], [MessageDate2], MINUTE ) > 0
        ),
        DATEDIFF ( [MessageDate], [MessageDate2], MINUTE )
    )
RETURN
    MINX (
        FILTER (
            'Table1',
            [ID] = EARLIER ( Table2[ID] )
                && DATEDIFF ( [MessageDate], [MessageDate2], MINUTE ) = _a
        ),
        [MessageDate2]
    )

Final output:

vjianbolimsft_0-1687487987586.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
darianle
Regular Visitor

works perfectly. thanks

v-jianboli-msft
Community Support
Community Support

Hi @darianle ,

 

Please try:

Column =
VAR _a =
    MINX (
        FILTER (
            'Table1',
            [ID] = EARLIER ( Table2[ID] )
                && DATEDIFF ( [MessageDate], [MessageDate2], MINUTE ) > 0
        ),
        DATEDIFF ( [MessageDate], [MessageDate2], MINUTE )
    )
RETURN
    MINX (
        FILTER (
            'Table1',
            [ID] = EARLIER ( Table2[ID] )
                && DATEDIFF ( [MessageDate], [MessageDate2], MINUTE ) = _a
        ),
        [MessageDate2]
    )

Final output:

vjianbolimsft_0-1687487987586.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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