Filter Query by last Working Day

Skybluekid

Well-known Member
Joined
Apr 17, 2012
Messages
1,231
Office Version
  1. 365
Platform
  1. Windows
Hi

I am a little stumped.

I would like to filter my query by the previous working day. So, if I was to run my query on a Monday it would only bring back items with a date of the previous Friday. But when I run it on the Tuesday, it would bring back results for the Monday etc.

Thanks in Advance
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    tbl = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    d = Date.From(DateTime.LocalNow()),
    dow = Date.DayOfWeek(d, Day.Saturday),
    // if day of week (dow) is Sat = 0, Sun = 1, or Mon = 2 then return Friday else yesterday
    filterdate = if dow < 3 then Date.AddDays(d, -dow -1) else Date.AddDays(d,-1),
    Result = Table.SelectRows(tbl, each [Date] = filterdate)
in
    Result

Book1
ABCDEFGH
1DataQuery Output(on May 3, 2023)
2IDDateQtyIDDateQty
315/1/20231125/2/202332
425/2/20233235/2/20235
535/2/2023545/2/202335
645/2/202335115/2/202316
754/28/202333155/2/202322
864/30/202323195/2/202318
974/30/202324
1084/30/20232
1195/1/20237
12105/1/202329
13115/2/202316
14124/27/202327
15134/28/202319
16144/30/202315
17155/2/202322
18165/1/202320
19174/29/202338
20185/1/20234
21195/2/202318
22205/1/202330
23
Sheet4
 
Upvote 0
Solution
This looks that it might work. Just a really stupid question, as I am still quite new to Power Query, where in my steps would I put this?
 
Upvote 0
open a blank query, open the advanced editor, replace all the text in the advanced editor with the above code. Edit the code data source and column name to match your data source and column header of your Date column.
 
Upvote 0
Thank you very much for the Code, it works a treat.
 
Upvote 0
@JGordan11, Is there a way to incorporate the code into my original query? What my query does scan a folder of PDF's and extracts data. So with your solution, I am refreshing two queries. I should have mentioned this in my original question.
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

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