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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ptmuldoon
Helper II
Helper II

Filter for Last X Months

I am pulling in data from json files that contains data for the last XX years.  Currently the file is only through 2019 data, that will change as more is added.

 

And I'm trying to then filter that data down to the Last X months.  I tried to filter it down to the last 3 using InPreviousNMonths, but think have an issue trying to set a starting date?

Should the below work to get just the last 3 months?   I do hope to make that date more dynamic eventually, but just learning at this point.

 

#"Filtered Rows" = Table.SelectRows(#"Changed Type", each Date.IsInPreviousNMonths(#date(2019,12,1), 3))

 

 

 

 

 

1 ACCEPTED SOLUTION
v-yingjl
Community Support
Community Support

Hi @ptmuldoon ,

Considering the updated date in your table, you can create query like this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc0xDgAgDALAvziblKIOvsX4/2+oi1LHC5SOkQjCnFY8zfzoLVLkvxCplwhd/CGFXavdqCiKqrgD+/EbONjJXA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Descending}}),
    EndDate = Date.From(List.Max(#"Sorted Rows"[Date])),
    StartDate = 
    Date.AddMonths(EndDate,-2),
    FilterRows = 
    Table.SelectRows(
        #"Sorted Rows",
        each [Date] >= StartDate and [Date] <= EndDate
    )
in
    FilterRows

re.png

Attached a sample file in the below, hopes to help you.

In addition, you can refer this blog: “In the Previous” Date Filters In Power BI/Get&Transform/Power Query 

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-yingjl
Community Support
Community Support

Hi @ptmuldoon ,

Considering the updated date in your table, you can create query like this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc0xDgAgDALAvziblKIOvsX4/2+oi1LHC5SOkQjCnFY8zfzoLVLkvxCplwhd/CGFXavdqCiKqrgD+/EbONjJXA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Descending}}),
    EndDate = Date.From(List.Max(#"Sorted Rows"[Date])),
    StartDate = 
    Date.AddMonths(EndDate,-2),
    FilterRows = 
    Table.SelectRows(
        #"Sorted Rows",
        each [Date] >= StartDate and [Date] <= EndDate
    )
in
    FilterRows

re.png

Attached a sample file in the below, hopes to help you.

In addition, you can refer this blog: “In the Previous” Date Filters In Power BI/Get&Transform/Power Query 

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Jimmy801
Community Champion
Community Champion

Hello @ptmuldoon 

 

you can just use the GUI to get what you want

Jimmy801_0-1612167176789.png

 

Jimmy801_1-1612167192215.png

 

The function used here has time intelligence and uses the current date of your system and filters all dates that are witin the last 3 months

 

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

mahoneypat
Employee
Employee

Typically you would put your [Date] column where you have a hard-coded date now #date(2019,12,1) so it is dynamic whenever you refresh.  If you want data since a fixed date, you can just filter for greater than that date instead.

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


smpa01
Super User
Super User

@ptmuldoon  can you try this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ1MAQipVgdGNcIlWuMyjVB5Zqics1QueaoXAtUriUK19AAlYvqKkOIq2IB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Column1]), Int64.Type),
    #"Grouped Rows" = Table.Group(#"Inserted Year", {"Year"}, {{"ad", each _, type table [Column1=nullable date, Year=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let x =[ad],
       #"Inserted Month" = Table.AddColumn(x, "Month", each Date.Month([Column1]), Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Inserted Month", "Maxmonth", each List.Max(#"Inserted Month"[Month])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Filter", each ([Maxmonth]-[Month])+1),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each [Filter] >= 1 and [Filter] <= 3),
    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Column1"})
in
    #"Removed Other Columns"),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ad"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column1"}, {"Column1"})
in
    #"Expanded Custom"

from

Capture.PNG

to here with the code above

Capture.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors