Basic Time and date functions in Power BI
In this blog post, we will learn one of the most important part of DAX: time and date functions. These functions allow you to work with dates and times in various ways, such as extracting, formatting, aggregating, comparing, and filtering. You can use these functions to create dynamic and interactive reports that show trends, patterns, and insights over time.
Time and date functions
Time and date functions are DAX functions that deal with dates and times. They can take values from a column containing date or time and give single value or a table as an output. Time and date functions are useful for several reasons:
- create calculations based on dates and times
- format and display dates and times in different ways
- compare and filter dates and times such as finding the latest or earliest date
How to create a date table in Power BI?
A date table is a special table that contains a list of dates, such as year, month, quarter, week, day, etc. It is essential for time and date calculations in Power BI. There are two ways to create a date table in Power BI:
Option 1: CALENDARAUTO function, which returns a table with a single column named “Date” that contains a contiguous set of dates based on the data in your model considering first date of the starting year till the last date of input in the data. You can also specify the fiscal year end month as an optional argument inside the brackets.
For example, in the sample excel file we have earliest date of entry as 02-04-2017 and the latest date is 01-04-2020. So if we use the following formula it creates a date table with starting date as 01-01-2017 and last date in date table as 31/12/2020 , even if our data has 01-04-2020 as last date of entry.DateTable = CALENDARAUTO()
DateTable =
CALENDAR (DATE(2017, 1, 1), DATE(2020, 12, 31))
NOTE: After creating a date table, you need to mark it as a date table in Power BI. The table must be marked as a Date table in the model (via the Mark as Date Table setting), in case the relationship between the Date table and any other table is not based on the Date. You can do this by following these steps:
- Go to the Model view in Power BI Desktop
- Select the date table in the Fields pane
- Click on the Table Tools tab in the ribbon
- Click on the Mark as Date Table button
- Select the date column that contains unique and contiguous dates
- Click OK
Some common time and date functions
Time and date functions can be used for various purposes. You can find the full list of time and date functions and their descriptions on the Microsoft Docs website. In this blog post, we will learn some of the common time and date functions that you can use in Power BI.
Extracting Functions are used to extract a specific part of a date or a time, such as year, month, day, hour, minute, second, etc. Some of the extracting functions are:
FUNCTION | DESCRIPTION | SYNTAX |
---|---|---|
YEAR | Returns the year of a date as a four-digit integer in the range 1900-9999 | YEAR(<datetime>) |
MONTH | Returns the month of a date as a number from 1 (January) to 12 (December) | MONTH(<datetime>) |
QUARTER | Returns a number from 1 to 4, i.e., the quarter of the year that the date falls into. 1: (January – March) 2: (April – June) 3: (July – September) 4: (October – December) |
QUARTER(<datetime>) |
DAY | Returns the day of the month of a date as a number from 1 to 31 | DAY(<datetime>) |
WEEKDAY | Returns the day of the week of a date as a number from 1 (Sunday) to 7 (Saturday) Note: If you want to have weekday name you can use FORMAT Function WeekdayName = FORMAT(WEEKDAY('DATE'[Date]), "dddd") |
WEEKDAY(<datetime>, [<return_type>]) Return Types: 1 or omitted: Sunday is considered the first day of the week by default. 2: Monday is considered the first day of the week. And so on |
WEEKNUM | Returns the week number of a date according to the ISO 8601 standard or a custom return type |
WEEKNUM(<date> [, <return_type>]) Return Types: System 1: The week containing January 1 is the first week of the year and is numbered week 1. System 2: The week containing the first Thursday of the year is the first week of the year and is numbered as week 1. This system is the methodology specified in ISO 8601, which is commonly known as the European week numbering system. |
HOUR | Returns the hour of a time as a number from 0 (12:00 A.M.) to 23 (11:00 P.M.) | HOUR(<datetime>) |
MINUTE | Returns the minute of a time as a number from 0 to 59 | MINUTE(<datetime>) |
SECOND | Returns the second of a time as a number from 0 to 59 | SECOND(<datetime>) |
Comparing functions
Comparing functions are used to compare two dates or times and return a logical value (true or false) or a numeric value (positive, negative, or zero). Some of the comparing functions are:
- =: Returns true if two values are equal, and false otherwise
- <>: Returns true if two values are not equal, and false otherwise
- >: Returns true if the first value is greater than the second value, and false otherwise
- <: Returns true if the first value is less than the second value, and false otherwise
- >=: Returns true if the first value is greater than or equal to the second value, and false otherwise
- <=: Returns true if the first value is less than or equal to the second value, and false otherwise
For example, the following formula returns true if the date column in the Sales table is greater than Last date for DiscountPrice date column, and false otherwise:
Sales After Discount Date = Sales[SalesDate]>Sales[Last Date for DiscountPrice]
Comments
Post a Comment