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

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.

Relationships between tables

In Power BI, relationships between tables are essential for creating accurate and insightful reports. These relationships define how data from different tables interact with each other when performing analyses or creating visualizations. Here's a detailed overview of how relationships between tables work in Power BI: Types of Relationships: One-to-one (1:1):   This is the most common type of relationship in Power BI. It signifies that one record in a table can have multiple related records in another table. For example, each customer can have multiple orders. Many-to-One (N:1):   This relationship type is essentially the reverse of a one-to-many relationship. Many records in one table can correspond to one record in another table. For instance, multiple orders belong to one customer. One-to-Many (1:N):   Power BI doesn't support direct one-to-many relationships.  One record in table can correspond to many records in another table.  Many-to-Many (N:N):  ...

SQL Fundamentals

SQL, or Structured Query Language, is the go-to language for managing relational databases. It allows users to interact with databases to retrieve, manipulate, and control data efficiently. SQL provides a standardized way to define database structures, perform data operations, and ensure data integrity. From querying data to managing access and transactions, SQL is a fundamental tool for anyone working with databases. 1. Basics of SQL Introduction : SQL (Structured Query Language) is used for managing and manipulating relational databases. SQL Syntax : Basic structure of SQL statements (e.g., SELECT, INSERT, UPDATE, DELETE). Data Types : Different types of data that can be stored (e.g., INTEGER, VARCHAR, DATE). 2. SQL Commands DDL (Data Definition Language) : CREATE TABLE : Define new tables. ALTER TABLE : Modify existing tables. DROP TABLE : Delete tables. DML (Data Manipulation Language) : INSERT : Add new records. UPDATE : Modify existing records. DELETE : Remove records. DQL (Da...