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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. 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] )

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.


Click here to visit my LinkedIn page

Click here to schedule 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] )

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.


Click here to visit my LinkedIn page

Click here to schedule 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])

Share with Power BI Enthusiasts: 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
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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