Forum Discussion
Get nearest date to target date from another table
- 3 years ago
Hi, eWise
According to your description , there is no associated [AcessTime] field in the scan table and you want to show the [UserId] field in the 'Scans' table. I download your .csv files , the [warehouseId] in the fourth row of your 'Scans' table does not have the data to be associated with in the 'WarehouseAccess' table, so the display is empty.
Here are the steps you can follow:
(1)I use the data you provided, the two table do not need to create relationship.
(2)We can click "New column" create two calculated columns in 'Scans' table :
AccessTime = var _groupBy_ID =DISTINCT( SELECTCOLUMNS( FILTER('WarehouseAccess' , 'WarehouseAccess'[warehouseId]='Scans'[warehouseId]) ,"AccessTime", [AccessTime] )) var _compare_table =ADDCOLUMNS( CROSSJOIN({'Scans'[ScanDate]},_groupBy_ID) , "diff" ,ABS( [AccessTime]-[ScanDate]) ) var _min_diff= MINX(_compare_table ,[diff]) var _min_date = FILTER(_compare_table , [diff] =_min_diff) return CONCATENATEX(_min_date,[AccessTime],",")UserId = var _groupBy_ID =DISTINCT( SELECTCOLUMNS( FILTER('WarehouseAccess' , 'WarehouseAccess'[warehouseId]='Scans'[warehouseId]) ,"AccessTime", [AccessTime] )) var _compare_table =ADDCOLUMNS( CROSSJOIN({'Scans'[ScanDate]},_groupBy_ID) , "diff" ,ABS( [AccessTime]-[ScanDate]) ) var _min_diff= MINX(_compare_table ,[diff]) var _min_date =SELECTCOLUMNS( FILTER(_compare_table , [diff] =_min_diff) , "date", [AccessTime]) var _user_table = FILTER('WarehouseAccess' , 'WarehouseAccess'[warehouseId]='Scans'[warehouseId] && 'WarehouseAccess'[AccessTime] in _min_date ) return CONCATENATEX(_user_table,[userId],",")(3)Then we can meet your need , the result is as follows:
If this method cannot meet your need ,you can provide special output data as a table so that we can help you better .
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
- 3 years ago
Much thanks v-yueyunzh-msft this worked as intended.
Thanks Greg_Deckler but it isn't working. All dates are showing as 12/30/1899 00:09:47 with the difference being only in the time.
Two questions, I dont see VAR _Whid = [Whid] being used. Not sure wether that's by design or not. Also, is VAR _AccessTimes = RELATED('WareHouseAccess') meant to call the table or the AccessTime?
Thanks,
eWise Did you see my second post? I deleted the first one. I thought I had edited it but for some reason both posts stayed. I believe the second one is correct where it returns the dates versus the time differences like the first one.
- eWise3 years ago
Helper II
I did see your second post Greg_Deckler . I tired that and played around a lot but it still give me the off dates like Tue, 17 Jun 1777 19:21:45 which I can't even validate.