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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

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
v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

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 others find it more quickly.

View solution in original post

1 REPLY 1
v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

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 others find it more quickly.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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