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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
awitt
Helper III
Helper III

Nearest Date from Other Table

I have two tables, one is an orders table with an order date and the other is a semester table with a semester start date. I am wanting to evaluate how close the order date is to the semester start date but I need to figure what the closest semester start date is. 

 

So in the example below, the yellow column would be calculated in the Orders Table. The first order has an order date of 1/1/2022 and the closest start date is 1/6/2022 so there you go. I would also be ok if this result wasn't the date, bit the SemesterID.  PBIX file here

awitt_0-1648009734276.png

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column.

 

Picture1.png

 

Desired result CC = 
VAR currentschoolid = OrderTable[SchoolID]
VAR currentorderdate = OrderTable[OrderDate]
VAR semesterdatedifftable =
    ADDCOLUMNS (
        FILTER ( SemesterIDTable, SemesterIDTable[SchoolID] = currentschoolid ),
        "@orderdate", currentorderdate
    )
VAR datediffcolumn =
    ADDCOLUMNS (
        semesterdatedifftable,
        "@diff", ABS ( DATEDIFF ( [@orderdate], SemesterIDTable[SemesterStartDate], DAY ) )
    )
VAR datediffmin =
    MINX ( datediffcolumn, [@diff] )
VAR filtermin =
    FILTER ( datediffcolumn, [@diff] = datediffmin )
RETURN
    MAXX ( filtermin, SemesterIDTable[SemesterStartDate] )

 

Desired result semesterID CC = 
VAR currentschoolid = OrderTable[SchoolID]
VAR currentorderdate = OrderTable[OrderDate]
VAR semesterdatedifftable =
    ADDCOLUMNS (
        FILTER ( SemesterIDTable, SemesterIDTable[SchoolID] = currentschoolid ),
        "@orderdate", currentorderdate
    )
VAR datediffcolumn =
    ADDCOLUMNS (
        semesterdatedifftable,
        "@diff", ABS ( DATEDIFF ( [@orderdate], SemesterIDTable[SemesterStartDate], DAY ) )
    )
VAR datediffmin =
    MINX ( datediffcolumn, [@diff] )
VAR filtermin =
    FILTER ( datediffcolumn, [@diff] = datediffmin )
RETURN
    MAXX ( filtermin, SemesterIDTable[SemesterID] )

 

    Microsoft MVP
 

 

   


      If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


   


     
        LinkedInVisit my LinkedIn page
     

   


   


     
        Outlook BookingSchedule a short Teams meeting to discuss your question

     

   


 


View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column.

 

Picture1.png

 

Desired result CC = 
VAR currentschoolid = OrderTable[SchoolID]
VAR currentorderdate = OrderTable[OrderDate]
VAR semesterdatedifftable =
    ADDCOLUMNS (
        FILTER ( SemesterIDTable, SemesterIDTable[SchoolID] = currentschoolid ),
        "@orderdate", currentorderdate
    )
VAR datediffcolumn =
    ADDCOLUMNS (
        semesterdatedifftable,
        "@diff", ABS ( DATEDIFF ( [@orderdate], SemesterIDTable[SemesterStartDate], DAY ) )
    )
VAR datediffmin =
    MINX ( datediffcolumn, [@diff] )
VAR filtermin =
    FILTER ( datediffcolumn, [@diff] = datediffmin )
RETURN
    MAXX ( filtermin, SemesterIDTable[SemesterStartDate] )

 

Desired result semesterID CC = 
VAR currentschoolid = OrderTable[SchoolID]
VAR currentorderdate = OrderTable[OrderDate]
VAR semesterdatedifftable =
    ADDCOLUMNS (
        FILTER ( SemesterIDTable, SemesterIDTable[SchoolID] = currentschoolid ),
        "@orderdate", currentorderdate
    )
VAR datediffcolumn =
    ADDCOLUMNS (
        semesterdatedifftable,
        "@diff", ABS ( DATEDIFF ( [@orderdate], SemesterIDTable[SemesterStartDate], DAY ) )
    )
VAR datediffmin =
    MINX ( datediffcolumn, [@diff] )
VAR filtermin =
    FILTER ( datediffcolumn, [@diff] = datediffmin )
RETURN
    MAXX ( filtermin, SemesterIDTable[SemesterID] )

 

    Microsoft MVP
 

 

   


      If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


   


     
        LinkedInVisit my LinkedIn page
     

   


   


     
        Outlook BookingSchedule a short Teams meeting to discuss your question

     

   


 


amitchandak
Super User
Super User

@awitt , a new column in table1

 

minx(filter(Table2, Table2[semesterDate] >= Table1[OrderDate] && Table2[schoolID] = Table1[SchoolID]), Table2[semesterDate])

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

So this worked when the orderdate was before the start date. But in a case where something was ordered the day after start, this column is picking up not that start day the day before but rather the next start date which might be months away. 

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

May 2025 Monthly Update

Fabric Community Update - May 2025

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