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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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?
➤ 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?
➤ 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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.