Need help on dynamic date filter in Power Query

spycein

Board Regular
Joined
Mar 8, 2014
Messages
135
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,
I need help to filter dates in power query based on cell value mentioned in excel.
Following is my sample sales dataset

Order DateRegionRepItemUnitsUnit CostTotal
06-Jan-21​
EastJonesPencil
95​
1.99189.05
23-Jan-21​
CentralKivellBinder
50​
19.99999.50
09-Feb-21​
CentralJardinePencil
36​
4.99179.64
26-Feb-21​
CentralGillPen
27​
19.99539.73
15-Mar-21​
WestSorvinoPencil
56​
2.99167.44
01-Apr-21​
EastJonesBinder
60​
4.99299.40
18-Apr-21​
CentralAndrewsPencil
75​
1.99149.25
05-May-21​
CentralJardinePencil
90​
4.99449.10
22-May-21​
WestThompsonPencil
32​
1.9963.68

i need to filter Order date column in power query based on value mentioned in excel sheet cells, For example if i put 01/04/2021 as Start Date and 31/05/2021 as End Date then the table will return the value between these two date range.

Following is my power query M code looks like:

let
Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"OrderDate", type date}, {"Region", type text}, {"Rep", type text}, {"Item", type text}, {"Units", Int64.Type}, {"Unit Cost", type number}, {"Total", type number}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [OrderDate] >= #date(2022, 4, 1) and [OrderDate] <= #date(2022, 5, 31))
in
#"Filtered Rows"

I am looking for a solution to link the dates dynamically. Also sharing the excel workbook for your reference.


Thank you so much in advance.

Best Regards,
Shib
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You will first want to set up the 2 cells that will house your Start Date and your End Date as Named Ranges (one for each date), from either the Name Manager or the Name Box (when the cell is selected), making sure that there are no spaces in the name. Then add each of them as tables in your Power Query by clicking the "From Table/Range" button in the Get & Transform Data, and format accordingly in the Power Query Editor. Note, in my test version, I have changed the names of the Columns in each. But it will be NamedRange[#"Column Name"]{0} as the structure

Then in the Filtered Rows Step, you'll just need to change it from:
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [OrderDate] >= #date(2022, 4, 1) and [OrderDate] <= #date(2022, 5, 31))

To
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [OrderDate] >= StartDate[#"Start Date"]{0} and [OrderDate] <= EndDate[#"End Date"]{0})

Then Close & Load. Each time you update the 2 Date cells and refresh your Query, the data will filter based on the Start Date and End Date cell populations.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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