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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
StefanH74
Frequent Visitor

calculate hours within selected daterange

Hello all,

 

I have a fact-table with productionruns which contains the start- and end-date & time.

now to calculate the production-duration would be as simple as making a caclulated column

 

PrunTotalTime = vProductionRun[end]-vProductionRun[start]

 

 

and then SUM that column

however the problem lies within the fact that these runs can be quite long, in fact longer than the period I want to do calculations on.

I have a Date-table, and a slicer to select the daterange I want to measure.

so my calculation should be something like:

 

PrunTotalTime = MIN(vProductionRun[end],MAX(dimDate,[Date]))-MAX(vProductionRun[start],MIN(dimDate,[Date]))

 

 

but it needs to do that for all lines seperately, and I cant get that to work.

 

as a measure it wont work because it doesnt look at all lines seperately.

 

as a calculated column it doesnt work because it ignores the setting of the date-slicer and just takes the min- and max-date of the entire date-table

 

any ideas on how to do this would be greatly appreciated!

Thanx in advance for your contribution,

 

Stefan

1 ACCEPTED SOLUTION
Rakesh1705
Super User
Super User

Since I have not any available data, I have created one data set to demonstrate the solution
Source data

Rakesh1705_0-1722853798190.png

Creating a Duplicate of the same and rename it as "Start End". Removing start time and end time

Rakesh1705_1-1722853853498.png

Added a custom column to create a calendar with the in between dates of start and end dates

Rakesh1705_2-1722853932176.png

Expanded table got 103 rows whereas original table has 18 rows

Rakesh1705_3-1722853992525.png

Overall hours is calculated in the main table

Rakesh1705_4-1722854050303.png

Average hours is calculated

Rakesh1705_5-1722854077920.png

With the help of lookupvalue formula hours per day against each work in loaded in "Start End" Table

Rakesh1705_6-1722854160096.png

Final Visualization

Rakesh1705_7-1722854189178.pngRakesh1705_8-1722854207817.png

If this is ok with you please accept the same as your solution.

View solution in original post

4 REPLIES 4
Rakesh1705
Super User
Super User

Share the sample data

Rakesh1705
Super User
Super User

Since I have not any available data, I have created one data set to demonstrate the solution
Source data

Rakesh1705_0-1722853798190.png

Creating a Duplicate of the same and rename it as "Start End". Removing start time and end time

Rakesh1705_1-1722853853498.png

Added a custom column to create a calendar with the in between dates of start and end dates

Rakesh1705_2-1722853932176.png

Expanded table got 103 rows whereas original table has 18 rows

Rakesh1705_3-1722853992525.png

Overall hours is calculated in the main table

Rakesh1705_4-1722854050303.png

Average hours is calculated

Rakesh1705_5-1722854077920.png

With the help of lookupvalue formula hours per day against each work in loaded in "Start End" Table

Rakesh1705_6-1722854160096.png

Final Visualization

Rakesh1705_7-1722854189178.pngRakesh1705_8-1722854207817.png

If this is ok with you please accept the same as your solution.

Hello Rakesh,

 

thanx for your help so far. It is certainly helpful.

and even though it is close, it is not exactly right.

for example, if I would have a run from Jan 1, 0:00 until Jan 2 8:00, and I set my slicer to start on Jan 2 then your solution give me 16 hours (the average of 1 day) where I would need to see 8 hours (Jan 2, from 0:00 until 8:00)

SachinNandanwar
Super User
Super User

Please post some sample data to help you better.



Regards,
Sachin
Check out my Blog

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.