Forum Discussion
Nearest Record Search based on Date
Hi,
I have a table of events with the structure below. Each event has a file number, an event code, user and date and some of them are exception events (based on some other logic that's on the IsExceptionEvent column). On this table, I need to create a calculated column called Responsible Party that, in case the event is an exception event, gets the user code from the nearest non-exception event.
Events Table Sample:
| File Number | Business Unit | Event Code | Event Date | Event User Code | FileEvent | IsExceptionEvent | Responsible Party |
| 632701 | TE | FVALA | 16/03/2023 11:52 | AOAS | 632701FVALA | TRUE | |
| 632701 | TE | CBAEA | 15/03/2023 00:00 | AOVO | 632701CBAEA | TRUE | |
| 632701 | TE | FDRAA | 20/03/2023 15:45 | NOOA | 632701FDRAA | FALSE | |
| 632701 | TE | FCTMA | 20/03/2023 15:44 | NOOA | 632701FCTMA | FALSE | |
| 632701 | TE | TDEPE | 15/03/2023 00:00 | NOOA | 632701TDEPE | FALSE | |
| 632701 | TE | TDEPA | 15/03/2023 00:00 | NOOA | 632701TDEPA | FALSE | |
| 632701 | TE | GDELA | 15/03/2023 00:00 | NOOA | 632701GDELA | FALSE |
What I've been trying to do, logically, is to get the latest non-exception event before the current event and the earliest non-exception event after it and after checking the difference between the current event's date and the events before and after, choose the closest one and define the current event's Responsible Party as the nearest event's event user code.
So far, I've been able to use the FILTER() function to get all the Non-Exception events onto a variable, but I'm unable to use the variable in other FILTER() functions (to get the previous and next events) since DAX doesn't allow me to reference the columns from the variable name.
Am I going about this with the wrong logic or is there a workaround to reference columns from the variable?
Thank you in advance for any help.