Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Lookup between two dates

Hi All,

 

I have two tables/queries (snippet below) and what I am trying to do is match the driver from the table/query 1 to table/query 2 based on the following criteria - Car (Table2) matches Car (Table1) and Report Date (Table2) falls inbetween Commence date (Table1) and End Date (Table1)

 

Table/Query1

 

CarDriverCommence DateEnd Date
NissanWill01/01/2018 15:0601/04/2018 10:00
NissanCharlie01/04/2018 10:0002/05/2018 09:00
FiatPeter25/03/2018 00:0031/04/2018 11:54
MazdaJan03/02/2018 13:4304/05/2018 09:36
MazdaJan04/05/2018 09:3621/05/2018 11:00
MazdaBen21/05/2018 11:0009/06/2018 13:43

 

Table/Query2

 

CarReport Date
Nissan31/04/2018 11:33
Fiat25/03/2018 00:00
Mazda07/05/2018 13:24

 

Therefore, for the above you would finish with the below

 

 

CarReport DateDriver
Nissan31/04/2018 11:33Charlie
Fiat25/03/2018 00:00Peter
Mazda07/05/2018 13:24Ben

 

Does anyone have any ideas?

 

Many Thanks,

 

Jack

  • Are you looking to do it with a calculated table?

     

     

    Table =
    ADDCOLUMNS (
        Table2,
        "Driver",
        VAR tCar = Table2[Car]
        VAR Dte = Table2[Report Date]
        RETURN
            CALCULATE (
                VALUES ( Table1[Driver] ),
                Table1[Car] = tCar,
                Table1[Commence Date] <= dte,
                Table1[End Date] >= Dte
            )
    )

1 Reply

  • Are you looking to do it with a calculated table?

     

     

    Table =
    ADDCOLUMNS (
        Table2,
        "Driver",
        VAR tCar = Table2[Car]
        VAR Dte = Table2[Report Date]
        RETURN
            CALCULATE (
                VALUES ( Table1[Driver] ),
                Table1[Car] = tCar,
                Table1[Commence Date] <= dte,
                Table1[End Date] >= Dte
            )
    )