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
alya1
Helper V
Helper V

Find value in table 2 when ID and Date(if possible or closest older date) match between 2 tables

Hi All,

 

Not sure if this is possible but I have 2 tables like below...

table 1:

IDDate
11/1/11
11/2/11
21/1/11
21/2/11
31/1/11
31/2/11

table 2:

IDDateHours
11/1/111
11/2/112
21/1/113
21/5/114
312/21/105
31/1/116


I would like to add a column in table 1 by using value (Hours) from table 2 that matches ID exactly AND Date if possible. If no dates match, then use value (Hours) from closest prior date.  
This is tricky because some ID don't have matching dates such as ID 2: 1/1/11 match but there is no 1/2/11 in table 2 so I would like to use the closest date going backwards of 1/1/11. For ID 3: no dates match so I would like to use the closest date going backwards for both cases. 

Goal table

IDDateHours
11/1/111
11/2/112
21/1/113
21/2/113
31/1/115
31/2/116

Thank you!

2 ACCEPTED SOLUTIONS
ahadkarimi
Solution Specialist
Solution Specialist

Hi @alya1, load your two tables, and using the measure below, you can achieve this result, if you encounter any issues, let me know.
Downnload PBIX file

ahadkarimi_0-1724460991321.png

 

 

Hours = 
VAR currentID = 'Table 1'[ID]
VAR currentDate = 'Table 1'[Date]
VAR closestDate =
    CALCULATE(
        MAX('Table 2'[Date]),
        FILTER(
            ALL('Table 2'),
            'Table 2'[ID] = currentID && 'Table 2'[Date] <= currentDate
        )
    )
VAR resultHours = 
    CALCULATE(
        MAX('Table 2'[Hours]),
        FILTER(
            ALL('Table 2'),
            'Table 2'[ID] = currentID && 'Table 2'[Date] = closestDate
        )
    )
RETURN
    resultHours

 

 

Did I answer your question?  If so, please mark my post as the solution!✔️
Your Kudos are much appreciated!  Proud to be a Responsive Resident!

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = LOOKUPVALUE(Table2[Hours],Table2[Date],CALCULATE(max(Table2[Date]),FILTER(Table2,Table2[ID]=EARLIER(Table1[ID])&&Table2[Date]<=EARLIER(Table1[Date]))),Table2[ID],Table1[ID])

Hope this helps.

Ashish_Mathur_0-1724469683964.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = LOOKUPVALUE(Table2[Hours],Table2[Date],CALCULATE(max(Table2[Date]),FILTER(Table2,Table2[ID]=EARLIER(Table1[ID])&&Table2[Date]<=EARLIER(Table1[Date]))),Table2[ID],Table1[ID])

Hope this helps.

Ashish_Mathur_0-1724469683964.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
ahadkarimi
Solution Specialist
Solution Specialist

Hi @alya1, load your two tables, and using the measure below, you can achieve this result, if you encounter any issues, let me know.
Downnload PBIX file

ahadkarimi_0-1724460991321.png

 

 

Hours = 
VAR currentID = 'Table 1'[ID]
VAR currentDate = 'Table 1'[Date]
VAR closestDate =
    CALCULATE(
        MAX('Table 2'[Date]),
        FILTER(
            ALL('Table 2'),
            'Table 2'[ID] = currentID && 'Table 2'[Date] <= currentDate
        )
    )
VAR resultHours = 
    CALCULATE(
        MAX('Table 2'[Hours]),
        FILTER(
            ALL('Table 2'),
            'Table 2'[ID] = currentID && 'Table 2'[Date] = closestDate
        )
    )
RETURN
    resultHours

 

 

Did I answer your question?  If so, please mark my post as the solution!✔️
Your Kudos are much appreciated!  Proud to be a Responsive Resident!

DataNinja777
Super User
Super User

Hi @alya1 ,

You can generate your desired output by creating a calculated column using the following DAX formula:

Column (Filled) = 
VAR CurrentID = 'Table 1'[ID]
VAR CurrentDate = 'Table 1'[Date]
VAR ClosestPreviousHours =
    CALCULATE(
        MAX('Table 2'[Hours]),
        FILTER(
            'Table 2',
            'Table 2'[ID] = CurrentID &&
            'Table 2'[Date] <= CurrentDate
        )
    )
RETURN
    IF(
        ISBLANK('Table 1'[Column]),
        ClosestPreviousHours,
        'Table 1'[Column]
    )

The [Column] is generated from the standard lookupvalue dax formula, and the DAX formula above is applied to create the [Column (Filled)] next to it.

DataNinja777_0-1724459885855.png

I have attached an example pbix file for your reference.  

Helpful resources

Announcements
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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Kudoed Authors