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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
zuzaarbuza
Frequent Visitor

Filtering value based on next closest date

Hi,

 

I have a huge table with transport data. 

Route - is column with initial route

Desired output route - is column I would like to calculate which is next route taken by driver based on:

     1. Filtering by registration plate (has to be exact same).

     2. Dates - filter only loads taken within 2 days of previous unloading date.

 

RouteLoading dateUnloading dateRegistration plateDesired output dateDesired output Route
Chicago - Ontario07-11-201908-11-20191ABCno load takenno load taken
Atlanta - New York14-11-201915-11-20191ABC15-11-2019New York - Chicago
New York - Chicago15-11-201918-11-20191ABC18-11-2019Chicago - Ontario
Chicago - Ontario18-11-201919-11-20191ABC19-11-2019Los Angeles - New York
Los Angeles - New York19-11-201920-11-20191ABC  

 

I tried below formula but it shows incorrect value:

 

Desired output date = CALCULATE(FIRSTNONBLANK(Table1[Loading date];1);FILTER(ALL(Table1);Table1[Loading date]<=[Unloading date]+2))

 

 

 

I would very much appreciate your help - I tried multiple lookup values and first non blank but have huge troubles with the syntax.

1 ACCEPTED SOLUTION
Geradav
Responsive Resident
Responsive Resident

Hi @zuzaarbuza 

 

See if the following DAX statement for calculated column works for:

 

 

Next route = 
VAR RegistrationPlate = RouteTable[Registration plate]
VAR UnloadingDate = RouteTable[Unloading date]
RETURN
    MAXX (
        
        TOPN ( 1,
            FILTER (
                RouteTable,
                RouteTable[Registration plate] = RegistrationPlate
                    && RouteTable[Unloading date] > UnloadingDate
                    && RouteTable[Loading date]
                        <= DATE ( YEAR ( UnloadingDate ), MONTH ( UnloadingDate ), DAY ( UnloadingDate ) + 2 )
            ),
            RouteTable[Loading date], ASC
        ), RouteTable[Route]
    )

 

 

Annotation 2020-04-28 113142.jpg

 
 

Let us know if that was helpful.

 

Best

 

David

View solution in original post

2 REPLIES 2
Geradav
Responsive Resident
Responsive Resident

Hi @zuzaarbuza 

 

See if the following DAX statement for calculated column works for:

 

 

Next route = 
VAR RegistrationPlate = RouteTable[Registration plate]
VAR UnloadingDate = RouteTable[Unloading date]
RETURN
    MAXX (
        
        TOPN ( 1,
            FILTER (
                RouteTable,
                RouteTable[Registration plate] = RegistrationPlate
                    && RouteTable[Unloading date] > UnloadingDate
                    && RouteTable[Loading date]
                        <= DATE ( YEAR ( UnloadingDate ), MONTH ( UnloadingDate ), DAY ( UnloadingDate ) + 2 )
            ),
            RouteTable[Loading date], ASC
        ), RouteTable[Route]
    )

 

 

Annotation 2020-04-28 113142.jpg

 
 

Let us know if that was helpful.

 

Best

 

David

Hi @Geradav!

 

It worked just perfect, thank you so much! I will dig into how does it work

 

All best!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.