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 Aniya v-yueyunzh-msft for that. It does look promising, however in some instances, it's not pulling the accesstime even where it there was an access time at the warehouse. Am attaching sample data, with an additonal twist where I need to also pull the user id corresponding to the closest [AccessTime]. I need all the columns in the scans table in final results and the calculated AccessTime and the userId.
Please let me know incase it's something is not clear.
Thanks.
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
- eWise3 years ago
Helper II
Much thanks v-yueyunzh-msft this worked as intended.