Forum Discussion

TARUNEY's avatar
TARUNEY
Frequent Visitor
2 years ago
Solved

Date Filter for last 2 months Data

I am trying to filter the data  on the date field (Resolved Date) from the last 2 month from the current date. I am using the below custom column for the same with the expression  = Table.AddColumn(...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi TARUNEY ,

    Have you solved your problem? The function Date.IsInPreviousNMonths will return false when passed a value that occurs within the current month.
    Date.IsInPreviousNMonths - PowerQuery M | Microsoft Learn


    Please try this:
    Here is my sample data and today is 2024.3.25 and I test it in Direct Query mode:


    Please use this M function to add a custom column:

    if [Resolved Date] >= Date.AddMonths(DateTime.Date(DateTime.LocalNow()), -2) and [Resolved Date] <= DateTime.Date(DateTime.LocalNow()) and [Resolved Date] <> null then true else if [Resolved Date] = null then null else false

     

    And the final output is as below:



    Here is the whole M function in the Advanced Editor:

    let
        Source = Sql.Databases("VM0"),
        test1 = Source{[Name="test"]}[Data],
        dbo_test = test1{[Schema="dbo",Item="test"]}[Data],
        #"Added Custom" = Table.AddColumn(dbo_test, "Custom", each if [Resolved Date] >= Date.AddMonths(DateTime.Date(DateTime.LocalNow()), -2) and [Resolved Date] <= DateTime.Date(DateTime.LocalNow()) and [Resolved Date] <> null then true else if [Resolved Date] = null then null else false)
    in
        #"Added Custom"


    Best Regards,
    Dino Tao
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.