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
Raj12
Helper III
Helper III

How to get only 1st rows with ID's having date between a range (45days) in Power Query

I have data with Id's and dates.

Each ID can have multiple dates with them. 

 

As an output I want only those rows per ID :

1) If ID subsequent dates falls between 45 days then give 1st date row

2) If susequent date is outside 45 days then give both date rows

Raj12_1-1678276792070.png

 

2 ACCEPTED SOLUTIONS
latimeria
Solution Specialist
Solution Specialist

Hi @Raj12 ,

 

Give a try to this:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc47DsAgDAPQu2RGShwGykgrtYdA3P8aDYiWz/pkW86Z0kmOFCzKKqpUXDcx89X8ZMdi92WGr4vJLBfHXrO6h70bF3tSz4XRbeaFgc3syP+lvA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, Date = _t]),
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Id"}, {{"AllRows", each _, type table [Id=nullable text, Date=nullable date]}}),
    #"Added Shifted Date" = Table.TransformColumns(
        #"Grouped Rows",
        {
            {"AllRows",each 
                Table.FromColumns(    
                    Table.ToColumns(_) & {{null} & List.RemoveLastN(_[Date],1)},
                    Table.ColumnNames(_) & {"Previous Date"}
                )
            }
        }
    ),
    Expanded = Table.Combine(#"Added Shifted Date"[AllRows]),
    #"Changed Type" = Table.TransformColumnTypes(Expanded,{{"Previous Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Delta", each Duration.Days([Date] - [Previous Date]),Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each [Delta] > 45 or [Delta] = null)
in
    #"Filtered Rows"

 

 

 

latimeria_0-1678294416430.png

 

View solution in original post

Raj12
Helper III
Helper III

latimeria Thank you for the help!

View solution in original post

2 REPLIES 2
Raj12
Helper III
Helper III

latimeria Thank you for the help!

latimeria
Solution Specialist
Solution Specialist

Hi @Raj12 ,

 

Give a try to this:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc47DsAgDAPQu2RGShwGykgrtYdA3P8aDYiWz/pkW86Z0kmOFCzKKqpUXDcx89X8ZMdi92WGr4vJLBfHXrO6h70bF3tSz4XRbeaFgc3syP+lvA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, Date = _t]),
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Id"}, {{"AllRows", each _, type table [Id=nullable text, Date=nullable date]}}),
    #"Added Shifted Date" = Table.TransformColumns(
        #"Grouped Rows",
        {
            {"AllRows",each 
                Table.FromColumns(    
                    Table.ToColumns(_) & {{null} & List.RemoveLastN(_[Date],1)},
                    Table.ColumnNames(_) & {"Previous Date"}
                )
            }
        }
    ),
    Expanded = Table.Combine(#"Added Shifted Date"[AllRows]),
    #"Changed Type" = Table.TransformColumnTypes(Expanded,{{"Previous Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Delta", each Duration.Days([Date] - [Previous Date]),Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each [Delta] > 45 or [Delta] = null)
in
    #"Filtered Rows"

 

 

 

latimeria_0-1678294416430.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