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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Tom_G
Helper II
Helper II

Matching Date Ranges Across Two Tables

Hi - I have two tables - one called ShopDeliveries and one called ShopHistory - the tables are linked together on the Shop Number column. 

 

The answer im looking for is the Shop Owner column on the ShopDeliveries table below. To create this column I need to match the shop delivery with the correct shop owner at the time the delivery was made. 

 

Thanks for your help!

 

ShopDeliveries

Shop NumberCallingIDArrival DateDeparture DateAmount DeliveredShop Owner
10034534567810/04/201811/04/201850John
10034686754613/01/202116/01/2021150John
10034729685720/07/202026/07/2020422Amy
10034798125630/11/202002/12/202036Justin

 

ShopHistory

ShopNumberShopOwnerStart DateEnd DateIndex
100345John01/01/201815/05/20183
100345Sara15/05/201811/11/20192
100345Lee11/11/2019 1
100346Tim19/08/201815/09/20203
100346Amy15/09/202018/12/20202
100346John18/12/2020 1
100347Bill14/04/201820/06/20203
100347Amy20/06/202031/10/20202
100347Justin31/10/2020 1
6 REPLIES 6
Jihwan_Kim
Super User
Super User

Hi, @Tom_G 

In order to achieve the above result, I created the model like below picture.

The link to pbix file is down below.

 

DeliveredShop Owner =
CALCULATE (
SELECTEDVALUE ( History[ShopOwner] ),
FILTER (
VALUES ( History[Start Date] ),
History[Start Date] <= SELECTEDVALUE ( Deliveries[Arrival Date] )
),
FILTER (
VALUES ( History[End Date] ),
History[End Date] >= SELECTEDVALUE ( Deliveries[Departure Date] )
|| History[End Date] = BLANK ()
)
)

 

Picture1.png

 

https://www.dropbox.com/s/48h3n2b8vl46bx6/TomG.pbix?dl=0 

 

 

Hi, My name is Jihwan Kim.

If this post helps, then please consider accept it as the solution to help other members find it faster.

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


amitchandak
Super User
Super User

@Tom_G , Try a new column like in ShopDeliveries

 


maxx(filter(ShopHistory, ShopHistory[ShopNumber] = ShopDeliveries[ShopNumber] && ShopDeliveries[Departure Date]>= ShopHistory[Start Date]
&& ShopDeliveries[Departure Date] >=ShopHistory[End Date]) ,ShopHistory[ShopOwner])

Thanks  @Jihwan_Kim  - the measure produces the right result. For the sake of integration in my model I was looking for a calculated column.

 

I tried the DAX from @amitchandak  for the calculated column but it's not quite giving the expected result (see new column below). Any ideas how to tweak this? Thanks

 

ShopNumber     Arrival Date        Departure Date Shop Owner       New Column

100345  10/04/2018         11/04/2018         John     

100346  13/01/2021         16/01/2021         John      Tim

100347  20/07/2020         26/07/2020         Amy       Bill

100347  30/11/2020         02/12/2020         Justin    Justin

Hi, @Tom_G 

Can you try the below for the calculated column ?

 

DeliveredShop Owner column =
CALCULATE (
MAX( History[ShopOwner]),
 
FILTER (
VALUES ( History[Start Date] ),
History[Start Date] <= Deliveries[Arrival Date]
),
FILTER (
VALUES ( History[End Date] ),
History[End Date] >= Deliveries[Departure Date]
|| History[End Date] = BLANK ()
)
)
 
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


@Jihwan_Kim , @amitchandak - would you know how to translate Jihwan's calculated column below into the M language, so I could create the column in the Power Query Editor instead?  

Thanks a lot 

 

DeliveredShop Owner column =
CALCULATE (
MAX( History[ShopOwner]),
 
FILTER (
VALUES ( History[Start Date] ),
History[Start Date] <= Deliveries[Arrival Date]
),
FILTER (
VALUES ( History[End Date] ),
History[End Date] >= Deliveries[Departure Date]
|| History[End Date] = BLANK ()
)
)

Thanks a lot @Jihwan_Kim for your help - it works on the test file! I also tweaked the query from @amitchandak below, which also seems to work on the test file.

Unfortunately both queries are a strain on memory consumption on my full data set so BI is having issues processing them.

 

Column = maxx(filter(ShopHistory, ShopHistory[ShopNumber] = ShopDeliveries[ShopNumber] && ShopDeliveries[Arrival Date]>= ShopHistory[StartDate]
&& ShopDeliveries[Departure Date] <=ShopHistory[EndDate]
|| ShopHistory[ShopNumber] = ShopDeliveries[ShopNumber] && ShopDeliveries[Arrival Date]>= ShopHistory[StartDate] && ISBLANK(ShopHistory[EndDate])) ,ShopHistory[ShopOwner])

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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