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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Eesa
Frequent Visitor

Calculate Dynamically Change Duration

Hi, 

 

I have a FLIGHT table below 

Eesa_0-1640080683666.png

 

I want to use a slicer in my dashbord and when I adjust the slicer between any date/time, I want to get the total time in air. 

To imagine this I created a simple illustration below. The red dashed line is my slicer to pick a start date/time and end date/time. Each blue line line represent a flight and its actual duration. 

Eesa_1-1640080991599.png

 

Now if I want to adjust my slicer between 1/1/2021 19:00 and 1/2/2021 5:00, I must get a total time in air of 25 hrs. 
I have a calendar table that has a relation with FLIGHT table and in which i will adjust my slicer from. 

So, how to do that in Power BI? 

Thank you for your help

1 ACCEPTED SOLUTION

Hi @Eesa ,

 

Try to create a new measure based on your origianl [Time in Air] measure.

Correct Measure with total =
SUMX ( 'Table', [Time in Air] )

Result is as below.

2.png

 

Best Regards,
Rico Zhou

 

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

4 REPLIES 4
Eesa
Frequent Visitor

I have re-written the measure to correct the error and works very well in matrix visual but it can't work with card visual as it gives me BLANK. 
Here is the measure: 

 

Time in Air =
var sdate = MINX(ALLSELECTED(CalendarDIM), CalendarDIM[Date])
var endate = MAXX(ALLSELECTED(CalendarDIM), CalendarDIM[Date]) + 1
var StartDate = MAX(MAX(Table[take off]), sdate)
var EndDate = MIN(MIN(Table[land on]), endate)

return CALCULATE( sumx(Table, DATEDIFF(StartDate, EndDate,MINUTE)) , FILTER(Table,
(Table[take off] >= StartDate && Table[take off] <= EndDate ) || (Table[land on] >= StartDate && Table[land on] <= EndDate )
 
Here is the result: 
Eesa_1-1640196335584.png

 

I'll really appreciate if anyone can help me fix this. 
Thank you 

 

Hi @Eesa ,

 

Try to create a new measure based on your origianl [Time in Air] measure.

Correct Measure with total =
SUMX ( 'Table', [Time in Air] )

Result is as below.

2.png

 

Best Regards,
Rico Zhou

 

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

amitchandak
Super User
Super User

@Eesa , Assume you have independent date and time slicer (not joined with table)

 


measure =
var _sdate = minx(allselected('Date'), 'Date'[Date])
var _edate = Maxx(allselected('Date'), 'Date'[Date])
var _stime = minx(allselected('Time'), 'Date'[Time])
var _etime = Maxx(allselected('Time'), 'Date'[Time])
var _st = _sdate + _stime
var _ed = _edate + _etime
var _start = max(Table[take off], _st)
var _end = min(Table[Land on], _ed)
return
calculate(sumx(Table, datediff(_start,_end,second)), filter( Table, (Table[take off] >= _st && Table[take off] <=ed) || (Table[Land on] >= _st && Table[Land on] <=ed) ))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thank you @amitchandak for your reply. 

I got an error message with max(Table[take off], _st). The message: "A single value for column 'take off' in table 'Table' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.