Forum Discussion

Mythicos's avatar
Mythicos
Frequent Visitor
2 years ago
Solved

Find result nearest a target date

Hello,

 

I have a table that includes the following data about transplants:

  • ID of transplant
  • Date of transplant
  • One or more laboratory results (with the lab date)

Table looks like this:

 

GRF #Dt GRFDt Labo #1MLL #1Dt Labo #2MLL #2Dt Labo #3MLL #3
102-févr-2223-févr-229104-mars-229111-mai-2293
214-févr-2207-mars-229814-mars-2298  
323-févr-2216-mars-2210023-mars-2210008-juin-22100
425-févr-2218-mars-2210025-mars-22100  
501-mars-2222-mars-2210029-mars-22100  
621-mars-2211-avr-229119-avr-229128-juin-22100
707-avr-2204-mai-229802-juin-229805-juil-2299
802-mai-2224-mai-2296    
904-mai-2226-mai-2210001-août-22100  
1006-mai-2203-juin-229106-juil-229128-juil-2297
1106-mai-2202-juin-229611-juil-2296  
1214-juin-2229-juil-225924-août-2259  
1315-juin-2214-juil-229213-sept-2292  
1417-juin-2211-juil-2299    
1522-juil-2226-août-229914-oct-229924-oct-22100

 

"GRF #" is the ID transplant

"Dt GRF" is date of transplant

"Dt Labo #1" is date of 1st lab

"MLL #1" is the result of the first lab

(same as above for "Dt Labo #2" / "MLL #2" (2nd lab) and "Dt Labo #3" / "MLL #3" (3rd lab)).

 

What I need is find, for each transplant, the lab result nearest a target date. For each transplant, the target date is 100 days after day of transplant. That lab result should appear in a new column named "Day 100 MLL".

 

For example, "GRF #1" has May 13th 2022 (100 days after February 2nd 2022) as its target date, so lab #3 (May 11th 2022) will be the nearest lab and "Day 100 MLL" should have 93 as its value.

 

Thank you for your help!

  • Hello Mythicos,

     

    Can you please try this approach:

     

    1. Calculate the Target Date

    Target Date = [Dt GRF] + 100
    

    2. Difference from Target Date

    Diff Labo #1 = ABS(DATEDIFF([Target Date], [Dt Labo #1], DAY))
    Diff Labo #2 = IF(NOT(ISBLANK([Dt Labo #2])), ABS(DATEDIFF([Target Date], [Dt Labo #2], DAY)), BLANK())
    Diff Labo #3 = IF(NOT(ISBLANK([Dt Labo #3])), ABS(DATEDIFF([Target Date], [Dt Labo #3], DAY)), BLANK())
    

    3. Nearest Lab Result

    Day 100 MLL = 
    SWITCH(
        TRUE(),
        [Diff Labo #1] <= [Diff Labo #2] || ISBLANK([Diff Labo #2]) && [Diff Labo #1] <= [Diff Labo #3] || ISBLANK([Diff Labo #3]), [MLL #1],
        [Diff Labo #2] <= [Diff Labo #1] || ISBLANK([Diff Labo #1]) && [Diff Labo #2] <= [Diff Labo #3] || ISBLANK([Diff Labo #3]), [MLL #2],
        [MLL #3]
    )
    

    Hope this helps!

2 Replies

  • Hello Mythicos,

     

    Can you please try this approach:

     

    1. Calculate the Target Date

    Target Date = [Dt GRF] + 100
    

    2. Difference from Target Date

    Diff Labo #1 = ABS(DATEDIFF([Target Date], [Dt Labo #1], DAY))
    Diff Labo #2 = IF(NOT(ISBLANK([Dt Labo #2])), ABS(DATEDIFF([Target Date], [Dt Labo #2], DAY)), BLANK())
    Diff Labo #3 = IF(NOT(ISBLANK([Dt Labo #3])), ABS(DATEDIFF([Target Date], [Dt Labo #3], DAY)), BLANK())
    

    3. Nearest Lab Result

    Day 100 MLL = 
    SWITCH(
        TRUE(),
        [Diff Labo #1] <= [Diff Labo #2] || ISBLANK([Diff Labo #2]) && [Diff Labo #1] <= [Diff Labo #3] || ISBLANK([Diff Labo #3]), [MLL #1],
        [Diff Labo #2] <= [Diff Labo #1] || ISBLANK([Diff Labo #1]) && [Diff Labo #2] <= [Diff Labo #3] || ISBLANK([Diff Labo #3]), [MLL #2],
        [MLL #3]
    )
    

    Hope this helps!

    • Mythicos's avatar
      Mythicos
      Frequent Visitor

      Hello Sahir_Maharaj, I tried your solution and there were some problems with the treatment of blanks.

      But I took what you proposed and changed the Switch arguments a little, by first testing the "blank" scenarios and it worked perfectly.

       

      SWITCH(TRUE(),
          ISBLANK(Chimerism[Diff Labo #2]) && ISBLANK(Chimerism[Diff Labo #3]), Chimerism[MLL #1],
          ISBLANK(Chimerism[Diff Labo #3]) && ([Diff Labo #1] <= [Diff Labo #2]), Chimerism[MLL #1],
          ISBLANK(Chimerism[Diff Labo #3]) && ([Diff Labo #2] <= [Diff Labo #1]), Chimerism[MLL #2],
          ([Diff Labo #1] <= [Diff Labo #2]) && (Chimerism[Diff Labo #1] <= [Diff Labo #3]), Chimerism[MLL #1],
          ([Diff Labo #2] <= [Diff Labo #1]) && (Chimerism[Diff Labo #2] <= [Diff Labo #3]), Chimerism[MLL #2],
          Chimerism[MLL #3]
      )

       

      So thank you!