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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
igonzalezb
Helper I
Helper I

Find corresponding worker based on shift begining and end datetimes

I have table A of events:

Event ID   Station ID   Timestamp (datatime)   
10A 
20B 
30A 

 

And I have table B of workers and their shifts:

Station ID     Worker ID      Shift start (datetime)     Shift end (datetime)     
A1  
A2  
B1  
A1  

 

I would like to fill a column on table A with the corresponding Worker ID, so I can calculate things like number of events per worker, etc.

Note datetime granularity is needed. Shifts may or may not start one day and end on the next one. There is no overlap of shifts. Some intervals of time may have no worker assigned. There is no more than one worker assigned at any given time-station combination. The same worker may be assigned to different stations on different datetimes.

How would one go about modeling this relationship in an elegant manner?

1 ACCEPTED SOLUTION

I would keep the tables disconnected, or - if you must - create a Stations dimensional table.

 

Regardless, the calculated column in the Events table would just reference the  entire workers_shifts table with the appropriate filters

 

 

Meta Code:

 

Emp = 
var s = selectedvalue([Station ID])

var d = selectedvalue([Timestamp])

Return calculate(max(workers[Worker ID]),workers[Station ID]=s,workers[Shift start]<=d,workers[Shift end]>=d)

View solution in original post

7 REPLIES 7
lbendlin
Super User
Super User

You may want to provide more details. What if worker shifts overlap  and the event at the station happened during that time?

I would keep the tables disconnected, or - if you must - create a Stations dimensional table.

 

Regardless, the calculated column in the Events table would just reference the  entire workers_shifts table with the appropriate filters

 

 

Meta Code:

 

Emp = 
var s = selectedvalue([Station ID])

var d = selectedvalue([Timestamp])

Return calculate(max(workers[Worker ID]),workers[Station ID]=s,workers[Shift start]<=d,workers[Shift end]>=d)

That works! Had to make some modifications though. Code ended up being this:

 

Assigned Worker = 
   CALCULATE(
      MAX(workers[Worker ID]),
      workers[Station ID] = EARLIER([Station ID]),
      workers[Shift start]<=EARLIER([Timestamp]),
      workers[Shift end] >= EARLIER([Timestamp])
   )

 

Thank you, @lbendlin 

Good.  You will want to unlearn the usage of EARLIER() and want to migrate to using variables.

Had to use earlier because with selectedvalue the formula did not work. It returned blanks everywhere. But yes, instead of earlier I'm using variables like this:

Assigned Worker = 
   VAR s = [Station ID]
   VAR d = [Timestamp]
   RETURN CALCULATE(
      MAX(workers[Worker ID]),
      workers[Station ID] = s,
      workers[Shift start]<=d,
      workers[Shift end] >= d
   )

 

 

Any reason why EARLIER is not recomended even when it does the trick?

 

 

EARLIER and EARLIEST only go back one context or to the outermost context. Variables allow you to freeze the values at any context level. Kind of ironic - these are not really variables, rather constants.

@lbendlin Hello. As stated in the post, there is no overlap. Let's assume there are no edge cases.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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