Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
joshua1990
Post Prodigy
Post Prodigy

Filter Date Column based on second Date column

I have a table with 2 date columns:

Date1Date2
01.01.202405.01.2024
01.01.202409.01.2024

 

Here I would like to filter the second column to the next 15 months based on the Date1 columns.

 

How would you do that in Power Query?

2 ACCEPTED SOLUTIONS
jennratten
Super User
Super User

Hello @joshua1990,

Your message doesn't say if you are trying to retain records in Date2 is greater than or less than 15 months from Date1 so I have given an example below which can solve for both. I have also used dates that can demonstrate the expected result.

In the screenshot below I have added a column which calculates the number of months between Date2 and Date1.  This creates the ability for you to then see the difference and filter based on your use case.  If seeing the difference as a separate column is not preferred then you can either remove the column or use the same logic in your filter statement.

jennratten_0-1721567326525.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJW0lEygjBNlGJ1UMSNcYibwMVjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}}),
    date_diff = Table.AddColumn(
                // starting table
            #"Changed Type",
            // name of new column to be created
            "MonthsBtwnDate2AndDate1", 
            each 
                // declare inline variables
                let 
                    // calculate the compelete years elapsed
                    DateDiffInYears = 
                        Date.Year ( [Date2] ) - Date.Year ( [Date1] ),
                    // calculate the remaining months
                    MonthsRemaining = 
                        Date.Month ( [Date2] ) - Date.Month ( [Date1] ),
                    // calculate the total number of months
                    DateDiffInMonths = 
                        ( DateDiffInYears * 12 ) + MonthsRemaining
                in
                    DateDiffInMonths,
                    Int64.Type
        )
in
    date_diff

 

  If you want to filter Date2 based on Date1 then it would be like this (retaining rows where Date2 is less than 15 months from Date1).

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJW0lEygjBNlGJ1UMSNcYibwMVjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}}),
    SelectRowsLessThan15Months = Table.SelectRows (
                // starting table
            #"Changed Type",
            each 
                // declare inline variables
                let 
                    // calculate the compelete years elapsed
                    DateDiffInYears = 
                        Date.Year ( [Date2] ) - Date.Year ( [Date1] ),
                    // calculate the remaining months
                    MonthsRemaining = 
                        Date.Month ( [Date2] ) - Date.Month ( [Date1] ),
                    // calculate the total number of months
                    DateDiffInMonths = 
                        ( DateDiffInYears * 12 ) + MonthsRemaining
                in
                    DateDiffInMonths < 15
        )
in
    SelectRowsLessThan15Months

jennratten_0-1721568802297.png

 

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

View solution in original post

AmiraBedh
Super User
Super User

In Power Query you can't filter a column based on another, but here is a woraround :

This is my initial dataset :

AmiraBedh_0-1721567560618.png

 

You can create a custom column to calculate the end date of the 15-month period from Date 1 :

AmiraBedh_1-1721567622068.png

 

Date.AddMonths([Date1], 15)

 

Then another  custom column  IsIn15Months to flag rows where Date2 is within 15 months of Date1 :

 

AmiraBedh_2-1721567766060.png

 

 

= Table.AddColumn(#"Added Custom", "Custom", each if [Date2] <= [End Date] then true else false)

 

 

AmiraBedh_3-1721567869621.png

Filter the rows where IsIn15Months is true.


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

3 REPLIES 3
jennratten
Super User
Super User

@joshua1990 If my post helped you please consider kudoing and accepting as a solution.

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

AmiraBedh
Super User
Super User

In Power Query you can't filter a column based on another, but here is a woraround :

This is my initial dataset :

AmiraBedh_0-1721567560618.png

 

You can create a custom column to calculate the end date of the 15-month period from Date 1 :

AmiraBedh_1-1721567622068.png

 

Date.AddMonths([Date1], 15)

 

Then another  custom column  IsIn15Months to flag rows where Date2 is within 15 months of Date1 :

 

AmiraBedh_2-1721567766060.png

 

 

= Table.AddColumn(#"Added Custom", "Custom", each if [Date2] <= [End Date] then true else false)

 

 

AmiraBedh_3-1721567869621.png

Filter the rows where IsIn15Months is true.


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696
jennratten
Super User
Super User

Hello @joshua1990,

Your message doesn't say if you are trying to retain records in Date2 is greater than or less than 15 months from Date1 so I have given an example below which can solve for both. I have also used dates that can demonstrate the expected result.

In the screenshot below I have added a column which calculates the number of months between Date2 and Date1.  This creates the ability for you to then see the difference and filter based on your use case.  If seeing the difference as a separate column is not preferred then you can either remove the column or use the same logic in your filter statement.

jennratten_0-1721567326525.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJW0lEygjBNlGJ1UMSNcYibwMVjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}}),
    date_diff = Table.AddColumn(
                // starting table
            #"Changed Type",
            // name of new column to be created
            "MonthsBtwnDate2AndDate1", 
            each 
                // declare inline variables
                let 
                    // calculate the compelete years elapsed
                    DateDiffInYears = 
                        Date.Year ( [Date2] ) - Date.Year ( [Date1] ),
                    // calculate the remaining months
                    MonthsRemaining = 
                        Date.Month ( [Date2] ) - Date.Month ( [Date1] ),
                    // calculate the total number of months
                    DateDiffInMonths = 
                        ( DateDiffInYears * 12 ) + MonthsRemaining
                in
                    DateDiffInMonths,
                    Int64.Type
        )
in
    date_diff

 

  If you want to filter Date2 based on Date1 then it would be like this (retaining rows where Date2 is less than 15 months from Date1).

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJW0lEygjBNlGJ1UMSNcYibwMVjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}}),
    SelectRowsLessThan15Months = Table.SelectRows (
                // starting table
            #"Changed Type",
            each 
                // declare inline variables
                let 
                    // calculate the compelete years elapsed
                    DateDiffInYears = 
                        Date.Year ( [Date2] ) - Date.Year ( [Date1] ),
                    // calculate the remaining months
                    MonthsRemaining = 
                        Date.Month ( [Date2] ) - Date.Month ( [Date1] ),
                    // calculate the total number of months
                    DateDiffInMonths = 
                        ( DateDiffInYears * 12 ) + MonthsRemaining
                in
                    DateDiffInMonths < 15
        )
in
    SelectRowsLessThan15Months

jennratten_0-1721568802297.png

 

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.