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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply

Help trying to join two tables with date comparison

Hi I have two tables (or two power query requests) like this;

Table A

Name / DateOfContract (AAAAMMJJ)

John    /  20240102

Robert /  20240202

Herbert / 20240302

 

Table B

FromDate   / Rate

20240101  / 10€

20240301  / 15€

 

I would like to associate in A the newest rate from B (eq.  for each item of A =>max FromDate of B and B.Date <= A.DateOfContract)

 

It would give

 

Name / DateOfContract / Rate

John    / 20240102           /10€

Robert / 20240202          /10€

Herbert / 20240302        /15€

 

Can you help please?

1 ACCEPTED SOLUTION
Sahir_Maharaj
Super User
Super User

Hello @PatrickByGecko,

 

Can you please try the following:

 

DateOfContract_Date = DATE(
    LEFT([DateOfContract], 4),
    MID([DateOfContract], 5, 2),
    RIGHT([DateOfContract], 2)
)
Rate = 
VAR ContractDate = DATE(
    LEFT(TableA[DateOfContract], 4),
    MID(TableA[DateOfContract], 5, 2),
    RIGHT(TableA[DateOfContract], 2)
)
RETURN
CALCULATE(
    MAX(TableB[Rate]),
    TableB[FromDate] <= ContractDate,
    ALL(TableB[FromDate]),  // Removes filters that might restrict the row context
    EARLIER(TableB[FromDate]) = MAX(TableB[FromDate]) // Gets the latest 'FromDate' less than 'ContractDate'
)

 

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

View solution in original post

1 REPLY 1
Sahir_Maharaj
Super User
Super User

Hello @PatrickByGecko,

 

Can you please try the following:

 

DateOfContract_Date = DATE(
    LEFT([DateOfContract], 4),
    MID([DateOfContract], 5, 2),
    RIGHT([DateOfContract], 2)
)
Rate = 
VAR ContractDate = DATE(
    LEFT(TableA[DateOfContract], 4),
    MID(TableA[DateOfContract], 5, 2),
    RIGHT(TableA[DateOfContract], 2)
)
RETURN
CALCULATE(
    MAX(TableB[Rate]),
    TableB[FromDate] <= ContractDate,
    ALL(TableB[FromDate]),  // Removes filters that might restrict the row context
    EARLIER(TableB[FromDate]) = MAX(TableB[FromDate]) // Gets the latest 'FromDate' less than 'ContractDate'
)

 

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

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