Skip to main content

Date and Time Functions

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

Popular posts from this blog

Performance Optimization

Performance optimization in SQL is crucial for ensuring that your database queries run efficiently, especially as the size and complexity of your data grow. Here are several strategies and techniques to optimize SQL performance: Indexing Create Indexes : Primary Key and Unique Indexes : These are automatically indexed. Ensure that your tables have primary keys and unique constraints where applicable. Foreign Keys : Index foreign key columns to speed up join operations. Composite Indexes : Use these when queries filter on multiple columns. The order of columns in the index should match the order in the query conditions. Avoid Over-Indexing:  Too many indexes can slow down write operations (INSERT, UPDATE, DELETE). Only index columns that are frequently used in WHERE clauses, JOIN conditions, and as sorting keys. Query Optimization Use SELECT Statements Efficiently : SELECT Only Necessary Columns : Avoid using SELECT * ; specify only ...

DAX UPPER Function

The DAX UPPER function in Power BI is used to convert all characters in a text string to uppercase. This function is useful for standardizing text data, ensuring consistency in text values, and performing case-insensitive comparisons. Syntax: UPPER(<text>) <text>: The text string that you want to convert to uppercase. Purpose: The UPPER function helps ensure that text data is consistently formatted in uppercase. This can be essential for tasks like data cleaning, preparing text for comparisons, and ensuring uniformity in text-based fields. E xample: Suppose you have a table named "Customers" with a column "Name" that contains names in mixed case. You want to create a new column that shows all names in uppercase. UppercaseName = UPPER(Customers[Name]) Example Scenario: Assume you have the following "Customers" table: You can use the UPPER function as follows: Using the UPPER function, you can convert all names to uppercase: UppercaseName = ...

TechUplift: Elevating Your Expertise in Every Click

  Unlock the potential of data with SQL Fundamental: Master querying, managing, and manipulating databases effortlessly. Empower your database mastery with PL/SQL: Unleash the full potential of Oracle databases through advanced programming and optimization. Unlock the Potential of Programming for Innovation and Efficiency.  Transform raw data into actionable insights effortlessly. Empower Your Data Strategy with Power Dataware: Unleash the Potential of Data for Strategic Insights and Decision Making.