Date.AddDays Function in Power Query
The Date.AddDays function returns a Date/DateTime/DateTimeZone value with the day portion incremented by the number of days provided. It also handles incrementing the month and year potions of the value as appropriate.
Syntax
Date.AddDays(dateTime as any, numberOfDays as number) as any
Example:
Power Query M
let Source = Table.FromRecords( { [Name = "Ashish", Year = "20-07-2024"], [Name = "Katrina", Year = "21-07-2024"], [Name = "John", Year = "22-07-2024"], [Name = "Salman", Year = "23-07-2024"], [Name = "Akshay", Year = "24-07-2024"], [Name = "Shahrukh", Year = "25-07-2024"], [Name = "Alia", Year = "26-07-2024"], [Name = "Karishma", Year = "27-07-2024"], [Name = "Anjali", Year = "28-07-2024"], [Name = "Jenelia", Year = "29-07-2024"], [Name = "Esha", Year = "30-07-2024"], [Name = "Kareena", Year = "31-07-2024"], [Name = "Aishwariya", Year = "01-08-2024"], [Name = "Pallavi", Year = "02-08-2024"], [Name = "Jatin", Year = "03-08-2024"], [Name = "Joseph", Year = "04-08-2024"], [Name = "Hemant", Year = "05-08-2024"], [Name = "Deepti", Year = "06-08-2024"] } ), // Convert the "Year" column from text to date format Converted = Table.TransformColumnTypes(Source, {{"Year", type date}}), // Modify the "Year" column directly by adding 1 year to each date return = Table.TransformColumns(Converted, {{"Year", each Date.AddDays(_, 1), type date}}) in return
The output will be shown in the following image:
Note: Here's how it works:
- Column Specification: In the Table.TransformColumns function, we explicitly specify the column (e.g., "Year") we want to transform. This ensures that the function applies only to that column and leaves other columns untouched.
- Function Application: The each keyword is shorthand for a function. It processes each value in the specified column. Inside the function:
- _ refers to the current value being processed from the specified column.
- For example, in each Date.AddDays(_, 1), _ represents the current value from the "Year" column.