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
miguelfelix93
New Member

overlapping time records from two tables

Hi all,

I have two tables:

  • [production_data]  which has the executed production orders for a line that packs jam.
  • [downtime_data] which stores the machine periods of inactivity (not running)

What I'm trying to find is how much downtime is contained within each production order (there can be downtime out of prod orders but I don't care for that one.

Φ = µ - δ = MIN(B,Y)- MAX(A,X)

miguelfelix93_0-1716357431003.png

 

 

A <- -> B Duration of Production Event

X <- -> Y Duration of Downtime Event

δ <- -> μ Downtime contained in Production Event

 

This is an example of production_data table:

prod_start_timeprod_end_timeproduct_descplanned_qtydelivered_qtyuom
5/21/2024 6:30:005/21/2024 7:18:08Fig 500g x 12IT14401428IT
5/21/2024 8:26:145/21/2024 10:42:28Strawberry 200g x 24IT56005279IT

 

This is an example of downtime_data table:

dt_start_timedt_end_timeduration_minmachine_desc
5/21/2024 6:52:245/21/2024 6:54:332.15Filler 1
5/21/2024 7:18:085/21/2024 8:26:1468.09999999Filler 1

 

the logic I have in mind is something like a for loop:

 

for i in production_data:

for j in downtime_data:

dt_overlap=  MAX(MIN(dt_end_time, prod_end_time)- MAX(dt_start_time, prod_start_time),0)

prod_order_dt=prod_order_dt + dt_overlap

 

I just need a new column "prod_order_dt" in the [production_data] table that shows the cummulative sum of the downtimes that overlap with the time range of every process order, but I'm not sure yet if DAX would be the best option (i guess doing this in power query would involve a heavy workload by creating a custom column that invokes a custom function). 

 

Would any of you be able to help?

1 REPLY 1
robert199re
New Member

This query assumes that start_time and end_time columns are present in both tables and are of a compatible date-time type. The SUM function calculates the total downtime for each production order by adding up the overlapping intervals. The CASE statement checks if the downtime period falls within the production order’s time frame and calculates the overlapping time. The LEFT JOIN ensures that all production orders are listed, even if there’s no corresponding downtime.                                                                           iadpworkforcenow.com

 

SELECT
p.order_id,
p.start_time,
p.end_time,
SUM(CASE
WHEN d.start_time BETWEEN p.start_time AND p.end_time THEN
LEAST(p.end_time, d.end_time) - GREATEST(p.start_time, d.start_time)
ELSE 0
END) AS downtime
FROM
production_data p
LEFT JOIN
downtime_data d
ON
d.start_time <= p.end_time AND d.end_time >= p.start_time
GROUP BY
p.order_id, p.start_time, p.end_time;

Helpful resources

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