Forum Discussion
urgent help needed in power query
- 1 year ago
Hi tkavitha911 ,
You can achieve this in Power Query by creating a date column from your Year and Month values, then comparing that date to the current date with the required offset. Here’s a step-by-step example:
1. Combine Year and Month into a Date column
Assume your columns are [Year] and [Month]. Add a custom column with this formula:
m= #date([Year], [Month], 1)
This will create a date at the start of each month.
2. Get today’s date and calculate offsets
Add two custom columns:
- For "After 3 months" (includes current month and next two months):
mlet CurrentDate = Date.From(DateTime.LocalNow()), StartMonth = Date.StartOfMonth(CurrentDate), EndMonth = Date.AddMonths(StartMonth, 2), ThisDate = #date([Year], [Month], 1) in ThisDate >= StartMonth and ThisDate <= EndMonth
- For "After 9 months":
mlet CurrentDate = Date.From(DateTime.LocalNow()), CheckDate = Date.AddMonths(Date.StartOfMonth(CurrentDate), 9), ThisDate = #date([Year], [Month], 1) in ThisDate > CheckDate
3. Filter your table
- Use the "After 3 months" column to filter for dates within the current and next two months.
- Use the "After 9 months" column to filter for dates after 9 months from now.
Summary of Steps:
- Combine year and month into a date.
- Add logical columns for your two filter conditions.
- Filter your data based on those columns.
Let me know if you need a sample M code or further clarification!
translation and formatting supported by AI - 1 year ago
Hi tkavitha911 ,
As rightly said by burakkaragoz , you may have to use Custom Column to flag those days which fall in current month and next two month and a separate custom column to flag those records that fall after 9 months.
In case you want to have this flag in the same custom column, you may have to use the below logic inside the custom column.
let CurrentDate = Date.From(DateTime.LocalNow()), StartMonth = Date.StartOfMonth(CurrentDate), EndMonth = Date.AddMonths(StartMonth, 3), After9Months = Date.AddMonths(StartMonth,9) ThisDate = #date([Year], [Month], 1) in (ThisDate >= StartMonth and ThisDate <= EndMonth) or ThisDate >= After9Monthsor if you have a separate Date Column available you can use the below Logic
let CurrentDate = Date.From(DateTime.LocalNow()), StartMonth = Date.StartOfMonth(CurrentDate), EndMonth = Date.AddMonths(StartMonth, 3), After9Months = Date.AddMonths(StartMonth,9) in ([Dates] >= StartMonth and [Dates] <= EndMonth) or [Dates] >= After9MonthsacRegards,
Hi tkavitha911 ,
As rightly said by burakkaragoz , you may have to use Custom Column to flag those days which fall in current month and next two month and a separate custom column to flag those records that fall after 9 months.
In case you want to have this flag in the same custom column, you may have to use the below logic inside the custom column.
let
CurrentDate = Date.From(DateTime.LocalNow()),
StartMonth = Date.StartOfMonth(CurrentDate),
EndMonth = Date.AddMonths(StartMonth, 3),
After9Months = Date.AddMonths(StartMonth,9)
ThisDate = #date([Year], [Month], 1)
in
(ThisDate >= StartMonth and ThisDate <= EndMonth) or ThisDate >= After9Months
or if you have a separate Date Column available you can use the below Logic
let
CurrentDate = Date.From(DateTime.LocalNow()),
StartMonth = Date.StartOfMonth(CurrentDate),
EndMonth = Date.AddMonths(StartMonth, 3),
After9Months = Date.AddMonths(StartMonth,9)
in
([Dates] >= StartMonth and [Dates] <= EndMonth) or [Dates] >= After9Monthsac
Regards,