cancel
Showing results for
Did you mean:
Highlighted
Helper V

## Duration Days

Hello guys,

I need to calculate and display the duration days. The calculation is

I have a 2 tables: 1) 'Dates' table (created using calendarauto())

2) 'Tickets' table    Columns: Ticket opened Date, Restored date, Ticket closed date(all are in date/time format),Ticket Number

If Restored date is having a date then 'Restored date - Ticket opened date', if Restored date is empty then Ticket closed date - Ticket opened date. This should return  the duration days(Coumn) in decimals. For ex" 2.3 Days, 7.2 Days.

After getting the days. I need to show them in the ranges like . For ex: If it returns 2.3 it should fall under 0-2 Days. If it returns 7.2 it should fall under 6-10 days.

Finally i need two columns. 1) Duration  2) Days Ranges . Kindly refer  below.

1) Duration column has days

 Ticket Opened Date Restored Date Ticket Closed Date Duration 5/29/2020 6/1/2020 3.2 6/2/2020 6/10/2020 8.7 6/15/2020 6/20/2020 5 6/12/2020 6/15/2020 3.3 5/15/2020 5/20/2020 5.2 5/29/2020 6/2/2020 4.3 6/1/2020 6/10/2020 9.2 6/2/2020 6/6/2020 4.7 6/2/2020 6/11/2020 9.2 6/3/2020 6/6/2020 3.3

2) These are the Days Ranges. which need to be created and mapped to above calculation.

 0-2 Days 3-5 Days 6-10 Days 11-30 Days 31-60 Days 61-90 Days 91-120 Days 121-150 days 151-180 days 181+ Days

Final result should be 2 columns 1)Duration 2) Days Range.

Kindly Help me with solution. Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User VI

## Re: Duration Days

The first block of M code is not a measure but an example of how to create the Duration column.  Basically, in the query editor on the Add Column tab, click on Custom Column and then put an expression like this in the pop up box, substituting your actual column names in.

``= if [Ticket Closed Date] = null then Duration.TotalHours([Restored Date]-[Ticket Opened Date])/24 else Duration.TotalHours([Ticket Closed Date]-[Ticket Opened Date])/24``

Then, hit Close & Apply to load the data with the new column, and then click on New Column in the ribbon and enter the DAX expression to get your new Days Range column (follow the pattern started to add more date ranges).

Regards,

Pat

Did I answer your question? Please mark my post as a solution! Kudos are also appreciated!

Proud to be a Super User!

8 REPLIES 8
Highlighted
Super User VI

## Re: Duration Days

Here is an example query with your data that shows how to get your Duration Column

``````let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dY9LCsAgDESvUrIWP0mj9Szi/a/R1PoJ0i4EHZ6PmVKAHWaHHj0YiC6M6yGHLEI1RWLUsVB+vC+bOhJ4WdBrngexW+YPstQYVhnvll5G9309U3t2jZqhuz50/psUlyTthEjCt4R+JG1QvQE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Ticket Opened Date" = _t, #"           Restored Date" = _t, #"Ticket Closed Date" = _t, #" Duration" = _t]),
#"Renamed Columns" = Table.RenameColumns(Source,{{"           Restored Date", "Restored Date"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Ticket Opened Date", type date}, {"Restored Date", type date}, {"Ticket Closed Date", type date}, {" Duration", type number}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "DurationNew", each if [Ticket Closed Date] = null then Duration.TotalHours([Restored Date]-[Ticket Opened Date])/24 else Duration.TotalHours([Ticket Closed Date]-[Ticket Opened Date])/24),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"DurationNew", type number}})
in
#"Changed Type1"``````

.

You can then use SWITCH(TRUE() ... in a Calculated Column to get your Range using this pattern

``````Range =
SWITCH (
TRUE (),
Tickets[ Duration] <= 2, "0-2 Days",
Tickets[ Duration] <= 5, "3-5 Days",
Tickets[ Duration] <= 10, "6-10 Days"
)``````

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

Did I answer your question? Please mark my post as a solution! Kudos are also appreciated!

Proud to be a Super User!

Highlighted
Helper V

## Re: Duration Days

@mahoneypat  Kindly elaborate please( I mean step by step). I am unable to distinguish between these measures.

Kindly help.

Highlighted
Super User VI

## Re: Duration Days

The first block of M code is not a measure but an example of how to create the Duration column.  Basically, in the query editor on the Add Column tab, click on Custom Column and then put an expression like this in the pop up box, substituting your actual column names in.

``= if [Ticket Closed Date] = null then Duration.TotalHours([Restored Date]-[Ticket Opened Date])/24 else Duration.TotalHours([Ticket Closed Date]-[Ticket Opened Date])/24``

Then, hit Close & Apply to load the data with the new column, and then click on New Column in the ribbon and enter the DAX expression to get your new Days Range column (follow the pattern started to add more date ranges).

Regards,

Pat

Did I answer your question? Please mark my post as a solution! Kudos are also appreciated!

Proud to be a Super User!

Highlighted
Community Support

## Re: Duration Days

Hi, @ghouse_peer

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

You may create two measures as below.

``````Duration =
var _restoredate = SELECTEDVALUE('Table'[Restored Date])
return
IF(
NOT(ISBLANK(_restoredate)),
var hours = DATEDIFF(SELECTEDVALUE('Table'[Ticket Opened Date]),_restoredate,HOUR)
var result1 = INT(hours/24)+MOD(hours,24)/24
return
result1
,
var hours = DATEDIFF(SELECTEDVALUE('Table'[Ticket Opened Date]),SELECTEDVALUE('Table'[Ticket Closed Date]),HOUR)
var result2 = INT(hours/24)+MOD(hours,24)/24
return
result2
)

Date Range =
IF(
ISFILTERED('Table'[Ticket Opened Date]),
SWITCH(
TRUE(),
[Duration]>=0&&[Duration]<=2,"0-2",
[Duration]>=3&&[Duration]<=5,"3-5",
[Duration]>=6&&[Duration]<=10,"6-10",
[Duration]>=11&&[Duration]<=30,"11-30",
[Duration]>=31&&[Duration]<=60,"31-60",
[Duration]>=61&&[Duration]<=90,"61-90",
[Duration]>=91&&[Duration]<=120,"91-120",
[Duration]>=121&&[Duration]<=150,"121-150",
[Duration]>=151&&[Duration]<=180,"151-180",
"181+"
)
)
``````

Result:

Best Regards

Allan

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

Highlighted
Helper V

## Re: Duration Days

@mahoneypat  Thanks for the solution. I got the required result. But i have one  Problem the order is not the same.

when i took tht column in table it is showing this way. I need this in orderwise. ex: 0-2 Days next 3-5 Days next 6-10 Days. As discussed earlier. Kindly help.

Highlighted
Community Support

## Re: Duration Days

Hi, @ghouse_peer

There is no direct way to solve the problem. As a workaround, you may create a new table with range and index columns. The pbix file is attached in the end.

Test:

Then you may create a measure as below.

``Rank = LOOKUPVALUE(Test[Index],Test[Range],[Date Range])``

Finally you can click the measure column header to sort the table visual.

Best Regards

Allan

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

Highlighted
Helper V

## Re: Duration Days

@mahoneypat Thanks for the solution. Got the ranges which is required. Thank you.

Highlighted
Helper V

## Re: Duration Days

@v-alq-msft Thanks for the suggestions, solutions which u hv provided. It helped me a lot to learn how we can workaroud in another way. The way u showcased the solution through PBIX file. Thank you very much.

Announcements

#### August Community Highlights

Check out a full recap of the month!

#### August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

#### Experience what’s next for Power BI

Join us for an in-depth look at the new Power BI features and capabilities at the free Microsoft Business Applications Launch Event.

Top Solution Authors
Top Kudoed Authors