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
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
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.