Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
eWise
Helper II
Helper II

Get nearest date to target date from another table

I have two tables, one for scan items with ScanDate and another table for warehouses with warehouse AccessTime. Am trying to get the nearest AccessTime to the ScanDate for each item. It could be before or after the ScanDate as long as it's the closest.

 

Below is sample data and desired results.

eWise_0-1663509969563.pngeWise_1-1663509992428.pngeWise_2-1663510014191.png

 

I have tried various examples and none works. I would appreciate any help.

 
2 ACCEPTED SOLUTIONS

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.

vyueyunzhmsft_2-1663726200721.png

 

Here are the steps you can follow:

(1)I use the data you provided, the two table do not need to create relationship.

vyueyunzhmsft_0-1663726015202.png

(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:

vyueyunzhmsft_1-1663726159317.png

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

View solution in original post

Much thanks @v-yueyunzh-msft this worked as intended. 

View solution in original post

8 REPLIES 8
v-yueyunzh-msft
Community Support
Community Support

Hi, @eWise 

According to your description, you want to group according to [WhID] to find the closest [AccessTime] to the corresponding [ScanDate].Right?

This is the steps you can follow:

We don't need to establish a relationship between these two tables.

(1)This is my test data:

vyueyunzhmsft_0-1663559655154.png

vyueyunzhmsft_1-1663559663689.png

(2)We can create a calculated column : "AccessTime"

 

AccessTime = var _groupBy_ID =DISTINCT( SELECTCOLUMNS( FILTER('WarehouseAccess' , 'WarehouseAccess'[WhID]='Scans'[WhID]) ,"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],",")

 

(3)Then we can meet your need , the result is as follows:

vyueyunzhmsft_2-1663559805983.png

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

 

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

 

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. 

 

Scans.csv

WarehouseAccess.csv

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.

vyueyunzhmsft_2-1663726200721.png

 

Here are the steps you can follow:

(1)I use the data you provided, the two table do not need to create relationship.

vyueyunzhmsft_0-1663726015202.png

(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:

vyueyunzhmsft_1-1663726159317.png

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

Much thanks @v-yueyunzh-msft this worked as intended. 

eWise
Helper II
Helper II

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.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

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.

Greg_Deckler
Super User
Super User

@eWise Thinking something like:

 

AccessTime Column =
  VAR __WhID = [WhID]
  VAR __ScanDate = [ScanDate]
  VAR __AccessTimes = RELATED('WarehouseAccess')
  VAR __Min = MAXX(FILTER(__AccessTimes,[AccessTime]<[ScanDate]),[AccessTime])
  VAR __Max = MINX(FILTER(__AccessTimes,[AccessTime]>=[ScanDate]),[AccessTime])
  VAR __MinOffset = __ScanDate - __Min
  VAR __MaxOffset = __Max - __ScanDate
RETURN
  IF(__MinOffset < __MaxOffset, __Min, __Max)

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.