Forum Discussion
Identifying the 2 most recent days in Power Query
Hi,
I would like to create a dynamic filter that identifies the 2 most recent days in a new column
My code for that step thus far is:
= Table.AddColumn(#"Added Conditional Column", "Custom.1", each if [Close of Business Date] = List.Max(#"Renamed Columns"[Close of Business Date]) then "Current Day" else if [Close of Business Date] = #date(2020, 1, 20) then "Prev Day" else null)
But I would like for:
"[Close of Business Date] = #date(2020, 1, 20)"
to be a dynamic filter that selects the 2nd most recent day in this column. Is there a relatively easy way to do this?
Hello Anonymous
check out this solution. It involves of creating a list of the 2 latest dates in a date-column. This list is then used to filter the whole table
let Source = #table ( {"Date","Column1"}, { {"43466","test1"}, {"43678","test2"}, {"43554","test3"}, {"43819","test4"}, {"43831","test5"}, {"43833","test6"}, {"43838","test7"}, {"43851","test8"}, {"43851","test9"}, {"43848","test10"} } ), ToDate = Table.TransformColumns ( Source, { { "Date", each Date.From(Number.From(_)), type date } } ), GetListToFilter = List.FirstN ( List.Sort ( List.Distinct ( ToDate[Date] ), Order.Descending ), 2 ), SelectRows = Table.SelectRows ( ToDate, (row)=> List.Contains ( GetListToFilter, row[Date] ) ) in SelectRowsCopy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.
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
13 Replies
- Jimmy801Community Champion
Hello Anonymous
check out this solution. It involves of creating a list of the 2 latest dates in a date-column. This list is then used to filter the whole table
let Source = #table ( {"Date","Column1"}, { {"43466","test1"}, {"43678","test2"}, {"43554","test3"}, {"43819","test4"}, {"43831","test5"}, {"43833","test6"}, {"43838","test7"}, {"43851","test8"}, {"43851","test9"}, {"43848","test10"} } ), ToDate = Table.TransformColumns ( Source, { { "Date", each Date.From(Number.From(_)), type date } } ), GetListToFilter = List.FirstN ( List.Sort ( List.Distinct ( ToDate[Date] ), Order.Descending ), 2 ), SelectRows = Table.SelectRows ( ToDate, (row)=> List.Contains ( GetListToFilter, row[Date] ) ) in SelectRowsCopy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.
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 - edhansCommunity Champion
Change [Close of Business Date] = #date(2020, 1, 20) to
[Close of Business Date] = Date.AddDays(#date(2020, 1, 20),-2)
- AnonymousNot applicable
Thanks Ed,
I was looking to see if there was a way to make a dynamic filter, so I could identify the two most recent days in that column
- edhansCommunity Champion
Can you provide a clear set of data (text, not an image) and what you are trying to do? A filter is a Table.SelectRows statement, and you are doing Table.AddColumn. You cannot filter with AddColumn.
- AnonymousNot applicable
Hey Anonymous
You can use the TODAY() Function in DAX: https://docs.microsoft.com/en-us/dax/today-function-dax
So you can just use the Date = TODAY()
And the Date.AddDays(TODAY(),-2)
That way it will be dynamic relative to "today"
- edhansCommunity Champion
TODAY() though doesn't work in Power Query, which is what I thought this was about. You'd need to start with DateTime.LocalNow() for today's date and time, but still not clear on the requirements.
- AnonymousNot applicable
I am tyring to do this in power query
I don't know if the current data time function because there is never data in there for the current day. I also cannot just pick the day before the max value because the next most recent day might be from 3 days prior to that