The example includes a Sales table containing order and delivery dates. DAX can compute the difference between two dates by subtracting one from the other. This produces the number of days between the two dates – a task that can be accomplished through a calculated column.
How is it possible to compute the difference between the two dates, only computing working days and skipping weekends and holidays? Simple math is no longer useful here, and DAX does not offer a predefined function.
A solution to this scenario requires a date table – more details here – with a specific column, IsWorkingDay, which indicates whether that particular day is a working day or not. The following figure shows an example:
The IsWorkingDay column should be added to the existing Date table, otherwise it is necessary to create an ad-hoc date table for this purpose. In the demo file of this article, IsWorkingDay is computed by simply checking if the date is a Saturday or Sunday.
'Date'[IsWorkingDay] = NOT WEEKDAY( 'Date'[Date] ) IN { 1,7 }
More information about how to create working day columns are available in this article.
Using the IsWorkingDay column, the Sales table can now include a new calculated column which writes as follows:
Sales[DeliveryWorkingDays] = CALCULATE( COUNTROWS ( 'Date'), DATESBETWEEN ( 'Date'[Date], Sales[Order Date], Sales[Delivery Date] – 1 ), 'Date'[IsWorkingDay] = TRUE, ALL ( Sales ) )
The function uses the DATESBETWEEN function, which returns a table with all the dates between the boundaries – Order Date and Delivery Date in the example. The result of DATESBETWEEN is further restricted by CALCULATE, which applies the second filter to only consider working days.
Once the two filters are applied by CALCULATE, the Date table specifically filters the working days between order and delivery. Then, the COUNTROWS function returns the number of working days in the DeliveryWorkingDay column, as shown in this final figure:
Returns the dates between two given dates.
DATESBETWEEN ( <Dates>, <StartDate>, <EndDate> )
Evaluates an expression in a context modified by filters.
CALCULATE ( <Expression> [, <Filter> [, <Filter> [, … ] ] ] )
Counts the number of rows in a table.
COUNTROWS ( [<Table>] )
Articles in the DAX 101 series
- Computing running totals in DAX
- Counting working days in DAX
- Summing values for the total
- Year-to-date filtering weekdays in DAX
- Creating a simple date table in DAX
- Automatic time intelligence in Power BI
- Using CONCATENATEX in measures
- Previous year up to a certain date
- Sorting months in fiscal calendars
- Using USERELATIONSHIP in DAX
- Mark as Date table
- Row context in DAX
- Filter context in DAX
- Introducing CALCULATE in DAX
- Understanding context transition in DAX
- Using KEEPFILTERS in DAX
- Variables in DAX
- Using RELATED and RELATEDTABLE in DAX
- Introducing ALLSELECTED in DAX
- Introducing RANKX in DAX