Forum Discussion
Working Days Between Two dates in two Tables
- 4 years ago
Here is another file that is simplified with the table and column names matching your examples so it should be easier for you to follow. I have also included step-by-step instructions of what to do to integrate this with your script (text in green font shown in the snip below). Below is a snip from the Advanced Editor view of Table2. Essentially you will copy/paste into your query and then make a couple of edits.
My Apologies i should of mentioned i have a Common "Ticket ID" Field
Working Days are Monday to Friday
| Table 1 | |
| Ticket ID | Opened Date |
| 552 | 15/04/2022 |
| Table 2 | ||
| Ticket ID | Escalated Date | Working Days Passed |
| 552 | 18/04/2022 | 1 |
To further develop the previous solution I provided, if you have scenario in which there is a ticket ID with an end date that does not exist in the start date table, without any other error handling the value would return an error, shown below in the Days Elapsed column. If you wanted to specify a different (number) value to be returned instead of an error, you could implement a try-otherwise statement, which would return the specified number, shown below in the Days Elapsed2 column.
SNIP (Ticket 3 exists in the EndDate table but not in the StartDate table)
SCRIPT
let
SampleStartDates = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLWN7LQNzIwMlKK1YlWMgKKmOibQAViAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ticket = _t, #"Start Date" = _t]), {{"Start Date", type date}}),
SampleEndDates_AllRecordsMatch = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLRN9E3MjAyUorViVYyAgsYGyBEjIEipvqGUIFYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ticket = _t, #"End Date" = _t]),{{"End Date", type date}}),
// New table with a record that does not have a start date.
SampleEndDates_WithExtraRecord = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLRN9E3MjAyUorViVYyAgsYGyBEjIEipvqGUIFYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ticket = _t, #"End Date" = _t]),{{"End Date", type date}}),
DaysElapsed_WithoutHandlerForExtraRecord = Table.AddColumn (SampleEndDates_WithExtraRecord, "Days Elapsed", each
Duration.Days (
// The value of the End Date column from the current table.
[End Date] -
// The value of the Start Date column
// for the first row with a matching Ticket, from the other table.
Table.SelectRows(SampleStartDates, (x)=> x[Ticket]=[Ticket]){0}[Start Date]
), Int64.Type
),
DaysElapsed_WithHandlerForExtraRecord1 = Table.AddColumn (DaysElapsed_WithoutHandlerForExtraRecord, "Days Elapsed2", each
try Duration.Days (
// The value of the End Date column from the current table.
[End Date] -
// The value of the Start Date column
// for the first row with a matching Ticket, from the other table.
Table.SelectRows(SampleStartDates, (x)=> x[Ticket]=[Ticket]){0}[Start Date]
) otherwise 0, Int64.Type
)
in
DaysElapsed_WithHandlerForExtraRecord1