Filter Date First Monday of Week

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I import a table of data into PQ with a column of dates.

I wish to filter this column for dates greater than or equal to the Monday of the current week

In Excel this would be:
Excel Formula:
=TODAY()-WEEKDAY(TODAY(),2)

At the moment I'm generating the week number in the data and using PQ to filter as:
Power Query:
= Table.SelectRows(#"Changed Type", each [WeekNum] = List.Max(#"Changed Type"[WeekNum]))

I can either generate this Monday date in an Excel function then pass this to PQ to filter the date column with OR use a standalone function to calculate this date and then filter the data column.

Preference would be to do this all in PQ and drop the [WeekNum] column from the data. I've tried to create this function but it doesn't work:
Power Query:
= () => Date.StartOfWeek(DateTime.LocalNow)

Can anyone help?
TIA,
Jack
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Would this work for you?
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}}),
    #"Inserted Start of Week" = Table.AddColumn(#"Changed Type", "Start of Week", each Date.StartOfWeek([Date]), type datetime),
    #"Start of Week" = #"Changed Type1"[Start of Week],
    LatestWeek=List.Max(#"Start of Week"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Inserted Start of Week",{{"Start of Week", type date}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type1", each [Start of Week] = LatestWeek)
in
    #"Filtered Rows"

Peter
 
Upvote 0
Solution
Hi Peter,

Thank you for reply and this does work :)

I thought it might be faster to calculate a single value for start of (current) week and then filter by that, but your approach works so will mark as solved and reply back if I figure it out - as much to learn for myself.

Cheers and Merry Christmas!
Jack
 
Upvote 0
maybe
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Date = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    CurrentWeek = Table.AddColumn(Date, "Custom", each Date.IsInCurrentWeek([Date])),
    Filter = Table.SelectRows(CurrentWeek, each ([Custom] = true)),
    TSC = Table.SelectColumns(Filter,{"Date"})
in
    TSC
 
Upvote 0
Hey @sandy666 that also works (with slight tweak for additional columns), thank you for suggesting :)
 
Upvote 0
Thanks from me as well, every day is a learning one. Much neater than my solution and solves the problem if there are no dates entered in the current week.
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,711
Members
449,118
Latest member
MichealRed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top