DAX (Data Analysis Expressions) in Power BI includes a comprehensive set of date and time functions. These functions are essential for creating calculated columns and measures that involve dates and times, enabling powerful time-based analysis and insights. Here's an overview of some key DAX date and time functions:
1. DATE
Syntax:
DATE(year, month, day)
Description:
Creates a date value from year, month, and day components.
Example:
DATE(2023, 5, 20) -- Returns: May 20, 2023
2. TIME
Syntax:
TIME(hour, minute, second)
Description:
Creates a time value from hour, minute, and second components.
Example:
TIME(14, 30, 0) -- Returns: 2:30 PM
3. TODAY
Syntax:
TODAY()
Description:
Returns the current date.
Example:
TODAY() -- Returns: Current date
4. NOW
Syntax:
NOW()
Description:
Returns the current date and time.
Example:
NOW() -- Returns: Current date and time
5. YEAR
Syntax:
YEAR(<date>)
Description:
Returns the year component of a date.
Example:
YEAR(DATE(2023, 5, 20)) -- Returns: 2023
6. MONTH
Syntax:
MONTH(<date>)
Description:
Returns the month component of a date.
Example:
MONTH(DATE(2023, 5, 20)) -- Returns: 5
7. DAY
Syntax:
DAY(<date>)
Description:
Returns the day component of a date.
Example:
DAY(DATE(2023, 5, 20)) -- Returns: 20
8. HOUR
Syntax:
HOUR(<datetime>)
Description:
Returns the hour component of a time value.
Example:
HOUR(TIME(14, 30, 0)) -- Returns: 14
9. MINUTE
Syntax:
MINUTE(<datetime>)
Description:
Returns the minute component of a time value.
Example:
MINUTE(TIME(14, 30, 0)) -- Returns: 30
10 . SECOND
Syntax:
SECOND(<datetime>)
Description:
Returns the second component of a time value.
Example:
SECOND(TIME(14, 30, 0)) -- Returns: 0
11. DATEDIFF
Syntax:
DATEDIFF(<start_date>, <end_date>, <interval>)
Description:
Returns the difference between two dates in the specified interval (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR).
Example:
DATEDIFF(DATE(2023, 5, 1), DATE(2023, 5, 20), DAY) -- Returns: 19
12. EOMONTH
Syntax:
EOMONTH(<start_date>, <months>)
Description:
Returns the last day of the month, offset by a specified number of months.
Example:
EOMONTH(DATE(2023, 5, 20), 1) -- Returns: June 30, 2023
13. WEEKDAY
Syntax:
WEEKDAY(<date>, [return_type])
Description:
Returns the day of the week as an integer (1-7), with an optional return_type to specify the start of the week.
Example:
WEEKDAY(DATE(2023, 5, 20)) -- Returns: 7 (Saturday, assuming the week starts on Sunday)
14. WEEKNUM
Syntax:
WEEKNUM(<date>, [return_type])
Description:
Returns the week number for a given date, with an optional return_type to specify the start of the week.
Example:
WEEKNUM(DATE(2023, 5, 20)) -- Returns: 20 (20th week of the year)
15. FORMAT
Syntax:
FORMAT(<value>, <format_string>)
Description:
Converts a value to text according to the specified format.
Example:
FORMAT(TODAY(), "DD/MM/YYYY") -- Returns: Formatted date as "20/05/2023"
DAX date and time functions are powerful tools for manipulating and analyzing dates and times in Power BI. They enable robust time-based calculations and are essential for any time intelligence analysis. By mastering these functions, you can enhance your reports and dashboards with accurate and insightful date and time metrics.
Comments
Post a Comment