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
Anonymous
Not applicable

lookup value within date range

Scenario

Adding a column with Role to the timesheets table. 

 

Each employee has a role, which will change over time. I want to look up the role of the employee based on the date of the timesheet. The right role can be found by looking at the date of the timesheet. this date should be in the date range of the employee role table. 

 

Question

Which expression is needed?

 

Timesheets

Employee

Date 

John

1-3-2019

John

1-3-2020

Chris

1-8-2019

Chris

1-9-2019

 

Employee Role

Employee

Role

Start Date

End Date

John

Medior staff

1-1-2019

31-12-2019

John 

Senior staff

1-1-2020

31-12-2020

Chris

Junior staff

1-6-2019

31-12-2019

Christ

Medior Staff

1-1-2020

31-12-2019

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@Anonymous  You're looking for an approximate lookup. See if this post helps: 

https://excelwithallison.blogspot.com/2020/06/dax-approximate-lookup.html

 

Use the 'bonus between' measure and edit for your data: 

 

Bonus Between =
CALCULATE (
    SELECTEDVALUE ( EmployeeRole[Role] ),
    FILTER (
        EmployeeRole,
        EmployeeRole[Start Date] < SELECTEDVALUE(Timesheets[Date]
            && EmployeeRole[End Date] > SELECTEDVALUE(Timesheets[Date]
    )
)

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Thanks you both!

Allison is the winner 🙂 

 

I have made a little adjustment to also include results which are on the start or end date. This was not clearly mentioned in my first post. < and > are replaced for <= and >=.

 

Variable  =
CALCULATE (
    SELECTEDVALUE ( EmployeeRole[Role] ),
    FILTER (
        EmployeeRole,
        EmployeeRole[Start Date] <= SELECTEDVALUE(Timesheets[Date]
            && EmployeeRole[End Date] => SELECTEDVALUE(Timesheets[Date]
    )
)
 
amitchandak
Super User
Super User

@Anonymous , A new column in timesheet to get role

 

new column =
maxx(filter('Employee Role', 'Employee Role'[Start Date] <=Timesheets[Date] && 'Employee Role'[End Date] >=Timesheets[Date]
&& 'Employee Role'[Employee] =Timesheets[Employee]),'Employee Role'[Role])

AllisonKennedy
Super User
Super User

@Anonymous  You're looking for an approximate lookup. See if this post helps: 

https://excelwithallison.blogspot.com/2020/06/dax-approximate-lookup.html

 

Use the 'bonus between' measure and edit for your data: 

 

Bonus Between =
CALCULATE (
    SELECTEDVALUE ( EmployeeRole[Role] ),
    FILTER (
        EmployeeRole,
        EmployeeRole[Start Date] < SELECTEDVALUE(Timesheets[Date]
            && EmployeeRole[End Date] > SELECTEDVALUE(Timesheets[Date]
    )
)

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

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.