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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Conditional Merge with a date range

Hi. 

First off- I did try to use these to solve my query but i do not understand this (magic) function so it really didnt work for me, though i think the answer may lie in there somewhere. (see below link) 

community.fabric.microsoft.com/t5/Desktop/Merging-info-with-a-date-in-a-first-table-and-a-range-of-d...

 

Here is my question, I have attached two sample query tables below. What I need to do is pull in which contract is relevant for the part number given the condition that the transaction date falls between the contract dates. If I was to do this in Excel I could use a formula like

"=IF(ISNA(XLOOKUP(partnumber,Table2[partnumber],Table2[partnumber])),"",LOOKUP(2,1/(Table2[StartDate]<=[Transactiondate])/(Table2[EndDate]>=[TransactionDate]),Table2[ContractName]))"

 

essentially I need to do a merge that first checks where the transaction range lies between the two dates in the contract table and then pull through the contract name based on the part number match. 

 

@v-xuding-msftmaybe you can help?

 

 

Table 1

Supplier codePart NumberTransaction datecontract name
11111A101/02/2018 
11111A129/10/2016 
11111A306/11/2019 
11111A411/01/2017 
11111A506/04/2016 
22222A111/08/2018 
11111A620/12/2018 
22222A218/10/2018 
11111A712/02/2019 
22222A306/03/2017 
22222A301/02/2017 
22222A119/06/2019 
22222A306/03/2017 

 

Table 2

 

Contract referencePart NumberStartDateEndDate
Supplier 1 contract 1A101/01/201631/12/2019
Supplier 1 contract 1A201/01/201631/12/2019
Supplier 1 contract 1A301/01/201631/12/2019
Supplier 1 contract 1A401/01/201631/12/2019
Supplier 1 contract 1A501/01/201631/12/2019
Supplier 1 contract 1A601/01/201631/12/2019
Supplier 1 contract 1A701/01/201631/12/2019
Supplier 2 Contract 1A101/01/202031/12/2025
Supplier 2 Contract 1A201/01/202031/12/2025
Supplier 2 Contract 1A301/01/202031/12/2025
1 ACCEPTED SOLUTION
NarenM
New Member

Hi kgiboin - To get the contract name based on the part number match and the condition that the transaction date falls between the contact dates.

Table 1 (Transactions)
Supplier code
Part Number
Transaction date
contract name
Table 2 (Contracts)
Contract reference
Part Number
StartDate
EndDate

Steps to Merge the Tables

Load the Tables into Power Query:
Select the data for both tables and load them into Power Query (in Excel, go to the "Data" tab > "From Table/Range").

Convert the Date Columns to Date Type:
Click on the "Transaction date," "StartDate," and "EndDate" columns.
In the "Transform" tab, select "Date" from the "Data Type" dropdown.

Merge the Queries:
With Table 1 selected, click on the "Home" tab > "Merge Queries."
Select Table 2, and match the "Part Number" columns in both tables.
Choose "Inner" (or "Full Outer" if you want to keep all rows), then click "OK."

Expand the New Column:
Click on the expand button in the "Table 2" column.
Select the columns "Contract reference," "StartDate," and "EndDate."

Add a Custom Column:
Go to the "Add Column" tab > "Custom Column."
Enter the following formula, replacing the column names if necessary:
= if [Transaction date] >= [StartDate] and [Transaction date] <= [EndDate] then [Contract reference] else null

Clean Up:
You may want to remove unnecessary columns or rename the custom column to "contract name."
If there are duplicate rows, you can use the "Remove Duplicates" option.

Apply and Close:
Click "Close & Apply" in Power BI or "Close & Load" in Excel.

You should now have a table that includes the relevant contract name for each transaction, based on the part number and transaction date. If the transaction date doesn't fall between the contract dates, the contract name will be null.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thanks, that worked. Would still be interested to see a solution using the one step approach in the link if anyone knows how?

Regards

NarenM
New Member

Hi kgiboin - To get the contract name based on the part number match and the condition that the transaction date falls between the contact dates.

Table 1 (Transactions)
Supplier code
Part Number
Transaction date
contract name
Table 2 (Contracts)
Contract reference
Part Number
StartDate
EndDate

Steps to Merge the Tables

Load the Tables into Power Query:
Select the data for both tables and load them into Power Query (in Excel, go to the "Data" tab > "From Table/Range").

Convert the Date Columns to Date Type:
Click on the "Transaction date," "StartDate," and "EndDate" columns.
In the "Transform" tab, select "Date" from the "Data Type" dropdown.

Merge the Queries:
With Table 1 selected, click on the "Home" tab > "Merge Queries."
Select Table 2, and match the "Part Number" columns in both tables.
Choose "Inner" (or "Full Outer" if you want to keep all rows), then click "OK."

Expand the New Column:
Click on the expand button in the "Table 2" column.
Select the columns "Contract reference," "StartDate," and "EndDate."

Add a Custom Column:
Go to the "Add Column" tab > "Custom Column."
Enter the following formula, replacing the column names if necessary:
= if [Transaction date] >= [StartDate] and [Transaction date] <= [EndDate] then [Contract reference] else null

Clean Up:
You may want to remove unnecessary columns or rename the custom column to "contract name."
If there are duplicate rows, you can use the "Remove Duplicates" option.

Apply and Close:
Click "Close & Apply" in Power BI or "Close & Load" in Excel.

You should now have a table that includes the relevant contract name for each transaction, based on the part number and transaction date. If the transaction date doesn't fall between the contract dates, the contract name will be null.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors