cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
chetanamare Regular Visitor
Regular Visitor

Match tables using date columns using nearest dates

Hello experts,

We have two tables coming from multiple MS SQL Server sources located at different locations. The data is as below:

 

EmpLocation

   

EmpId

Location

Active From

Active Till

1

A

01-01-2019

01-02-2019

1

B

02-02-2019

01-03-2019

 

EmpReporting

EmpId

Reporting Id

Money Spent

Report From

Report Till

ReportFlag

1

1

500

31-01-2018

06-01-2019

First

1

2

600

07-01-2019

13-01-2019

 

1

3

550

14-01-2019

20-01-2019

 

1

4

700

21-01-2019

27-01-2019

 

1

5

800

28-01-2019

28-01-2019

Last

1

6

600

04-02-2019

10-02-2019

First

1

7

550

11-02-2019

17-02-2019

 

1

8

700

18-02-2019

24-02-2019

 

1

9

800

25-02-2019

25-02-2019

Last

  

 

I want to form a relationship between the tables using Employee Id and dates.  As it is visible that the dates are not an exact match.

 

Consider the cells having Orange colored values. Here the EmpId 1 was active at Location A from 01-Jan-19 till 01-Feb-19. But the reporting that he/she has done starts at 31st-Dec-2018 and ends at 28-01-2019. The requirement is to show how much was spent by an employee on each location.

Also, i need the details from the second table when the data point in the first table is selected.

 

Note that the Report From(EmpReporting) date could be less/greater than or equal to the Active From Date(EmpLocation Table). Similar scenario is with the Report till and Active till dates.

 

Require your help in figuring out how the relations would work as per the given scenario in Power BI.

 

 

 

Regards,

Chetan

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: Match tables using date columns using nearest dates

Hi @chetanamare ,

 

Create a measure as below and add the measure in a table visual.

 

Measure = 
CALCULATE (
    MAX ( EmpLocation[Location] ),
    FILTER (
        EmpLocation,
        'EmpLocation'[Active From] <= MAX ( EmpReporting[Report Till] )
            && EmpLocation[Active Till] >= MAX ( EmpReporting[Report Till] )
    )
)

Capture.PNG

 

Pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Community Support Team
Community Support Team

Re: Match tables using date columns using nearest dates

Hi @chetanamare ,

 

Create a measure as below and add the measure in a table visual.

 

Measure = 
CALCULATE (
    MAX ( EmpLocation[Location] ),
    FILTER (
        EmpLocation,
        'EmpLocation'[Active From] <= MAX ( EmpReporting[Report Till] )
            && EmpLocation[Active Till] >= MAX ( EmpReporting[Report Till] )
    )
)

Capture.PNG

 

Pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 61 members 1,395 guests
Please welcome our newest community members: