How to use DAX function in Power BI to add and subtract datetime hours, minutes and seconds?

How to use DAX function in Power BI to add and subtract datetime hours, minutes and seconds?

In Power BI, we can use DAX functions to add and subtract datetime. Here's an example, assuming we want to add 3 hours to the current time:

After8Hour = NOW() + TIME(8,0,0)

Here we use NOW()the function to return the current datetime, and then use TIME()the function to add the specified number of hours, minutes and seconds. In this example, we're adding 8 hours, so pass 8 as the first argument, and pass 0 for both minutes and seconds.

In addition to the above examples, here are some common datetime addition and subtraction examples:

plus a certain time interval

Suppose we have an order that needs to be shipped within 2 days of placing the order, we can use the following formula to calculate the shipping date:

ShippingDate = OrderDate + 2

Here we directly add a time interval of 2 days to the OrderDate.

minus a certain time interval

Suppose we have a schedule that needs to remind us 30 minutes before the meeting starts, we can use the following formula to calculate the reminder time:

eminderTime = MeetingTime - TIME(0, 30, 0)

Here we use TIME()the function to convert the 30-minute time interval into a datetime type value, and then MeetingTimesubtract the time interval from the above.

Calculate time interval between two datetimes

The function in Power BI DAX DATEDIFF()is used to calculate the difference between two dates or times. It takes three parameters: interval type (eg year, month, day), start date and end date.

Following is DATEDIFF()the basic syntax of the function:

DATEDIFF(<interval>, <start_date>, <end_date>)

For example, to calculate the number of days between January 1, 2020 and February 1, 2020, you can use the following formula:

DATEDIFF(DAY, "1/1/2020", "2/1/2020")

This will return a value of 31, the number of days between the two dates.

The interval parameter must be one of the following:

  • YEAR
  • QUARTER
  • MONTH
  • DAY
  • HOUR
  • MINUTE
  • SECOND

Note that DATEDIFF()the function does not include the calculation of the end date. If you want to include the end date, you need to add 1 to the result.

For example, to count the number of days between January 1, 2020 and February 1, 2020 (both dates included), you can use the following formula:

DATEDIFF(DAY, "1/1/2020", "2/1/2020") + 1

This returns a value of 32, the number of days between the two dates, inclusive.

Guess you like

Origin blog.csdn.net/xili1342/article/details/131581761