top of page

You are learning Power Query in MS Excel

How to perform date and time manipulations within your data using Power Query?

Power Query offers a robust set of functions for manipulating dates and times within your data. Here's a breakdown of common techniques:

1. Working with Dates:

* Extracting Date Parts:
* Use functions like `Date.Year`, `Date.Month`, `Date.Day` to extract specific parts (year, month, day) from a date or datetime value.
* Adding/Subtracting Dates:
* Functions like `Date.AddDays`, `Date.AddMonths`, `Date.AddYears` allow you to add or subtract a specified number of days, months, or years to a date.
* Date Differences:
* Use `Date.Diff` to calculate the difference between two dates in days, months, or years.

2. Working with Times:

* Extracting Time Parts:
* Functions like `Time.Hour`, `Time.Minute`, `Time.Second` can extract specific parts (hour, minute, second) from a datetime value.
* Adding/Subtracting Time:
* Use `Time.Add` or `Time.Subtract` to add or subtract a specified duration (hours, minutes, seconds) to a time value.

3. Working with Date & Time Together:

* Creating Datetimes:
* Combine dates and times using `DateTime.Combine` to create a single datetime value.
* Extracting Date/Time from Text:
* Functions like `DateTime.FromText` can convert text strings representing dates or times into datetime values, considering specified formats.

4. Working with Time Zones:

* Converting Time Zones:
* Use `DateTimeZone.ToLocal` or `DateTimeZone.ToUtc` to convert datetimes between local time and Coordinated Universal Time (UTC).

Here are some resources for further exploration:

* Microsoft Documentation on Date Functions: [https://learn.microsoft.com/en-us/powerquery-m/date-functions](https://learn.microsoft.com/en-us/powerquery-m/date-functions)
* Microsoft Documentation on DateTime Functions: [https://learn.microsoft.com/en-us/powerquery-m/datetime-functions](https://learn.microsoft.com/en-us/powerquery-m/datetime-functions)

Examples:

* Adding 3 months to a date:

```m
let today = Date.Now()
let threeMonthsFromToday = Date.AddMonths(today, 3)
```

* Extracting hour from a datetime:

```m
let orderDatetime = DateTime.LocalNow()
let orderHour = Time.Hour(orderDatetime)
```

Remember, these are just a few examples. Power Query offers a variety of functions to handle even complex date and time manipulations within your data.

bottom of page