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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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

 

 

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.

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

 

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors