Forum Discussion

tkavitha911's avatar
tkavitha911
Icon for Helper III rankHelper III
1 year ago
Solved

urgent help needed in power query

Hi Team, I need help with filtering dates in Power Query Editor. I have a requirement to filter records that fall After 3 months (including the current month and the following two months) and also ...
  • burakkaragoz's avatar
    1 year ago

    Hi tkavitha911 ,

     

    You can achieve this in Power Query by creating date column from your Year and Month values, then comparing that date to the current date with the required offset. Here’s step-by-step example:


    1. Combine Year and Month into Date column

    Assume your columns are [Year] and [Month]. Add custom column with this formula:

    m
     
    = #date([Year], [Month], 1)

    This will create date at the start of each month.


    2. Get today’s date and calculate offsets

    Add two custom columns:

    • For "After months" (includes current month and next two months):
    m
     
    let
        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 months":
    m
     
    let
        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 months" column to filter for dates within the current and next two months.
    • Use the "After months" column to filter for dates after months from now.

    Summary of Steps:

    1. Combine year and month into date.
    2. Add logical columns for your two filter conditions.
    3. Filter your data based on those columns.

    Let me know if you need sample code or further clarification!
    translation and formatting supported by AI

  • Thejeswar's avatar
    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 >= 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,