Forum Discussion

zuzaarbuza's avatar
zuzaarbuza
Frequent Visitor
6 years ago
Solved

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.

  • 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]
        )

     

     

     
     

    Let us know if that was helpful.

     

    Best

     

    David

2 Replies

  • Geradav's avatar
    Geradav
    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]
        )

     

     

     
     

    Let us know if that was helpful.

     

    Best

     

    David

    • zuzaarbuza's avatar
      zuzaarbuza
      Frequent Visitor

      Hi Geradav!

       

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

       

      All best!