Power Query - Custom SQL, allow end user to change date range

denzo36

Board Regular
Joined
Sep 22, 2019
Messages
218
Office Version
  1. 365
Platform
  1. Windows
Hi all,

One of the more annoying tasks of my job is running ad-hoc reports from SQL server that need to be refreshed on say a weekly, monthly, yearly etc. basis. This is usually just a simple data extraction of data without too much manipulation going on post extraction.

What I tend to do is run the query through power query below, that way when I need to refresh I can refresh the query. This then gets sent onto my boss or whoever is requesting the data.

1677861770488.png


What I'd like to do is allow the end user to be able to run the query themselves, without the need for me to manually refresh and then send it to them.

Is there anything like a custom function in power query that could created that would allow the user some sort of on screen interface where they can adjust the date range of the query themselves that would run the query for the data range they choose?

This seems like a long shot I know.

Thanks in advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
you could move the file with the PQ in it to their machine or to a shared drive. In Queries and Connections you can set PQ to refresh upon the opening of the Spreadsheet.
 

Attachments

  • Screenshot 2023-03-03 113158.jpg
    Screenshot 2023-03-03 113158.jpg
    64.1 KB · Views: 31
Upvote 0
USing Microsoft query would work too, we do this for our accounts department. Copy and paste the SQL, create variables in MSQuery and link them to cells. They can change the dates in the worksheet and just refresh the table
 
Upvote 0
USing Microsoft query would work too, we do this for our accounts department. Copy and paste the SQL, create variables in MSQuery and link them to cells. They can change the dates in the worksheet and just refresh the table
Thanks, our office doesn't have that at the moment unfortunately but will keep it in mind for the future
 
Upvote 0
It's built into Excel, it's what existed before the whole Get and Transform stuff
 
Upvote 0
It's built into Excel, it's what existed before the whole Get and Transform stuff
I've never seen that before, are you talking about Access? If not, could you do some basic screenshots?
 
Upvote 0
Regarding allowing the user to adjust the date range, you could give them named range cells to enter the start and end dates:
Book1
AB
1Date Start: 01/01/2023
2End Date:03/15/2023
Sheet1
Cell Formulas
RangeFormula
B2B2=TODAY()

Cell B1 is named StartDate, and cell B2 is named EndDate. They can be brought into Power Query without having to turn them into tables:
Power Query will bring them in as a table with one column (Column1) and one row with the date in the single cell.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="StartDate"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    Column1 = ChangedType{0}[Column1]
in
    Column1
The ChangeType step is needed because the simple date will be brought in as a DateTime data type. Then right click on the date (not the column) and select Drill Down. The Queries will have the same name as the Named Range - StartDate and EndDate in this case.
Those names can now be used in your SQL queries. I'm not familiar enough with SQL, but in M Code you could turn
Power Query:
= Table.SelectRows(#"Changed Type", each [Date] >= #date(2023, 2, 1) and [Date] <= #date(2023, 3, 15))
into
Power Query:
= Table.SelectRows(#"Changed Type", each [Date] >= StartDate and [Date] <= EndDate)
with the same result.
Of course, if they change the date, they'll have to learn how to navigate to the Refresh All button and click it - or save the file and re-open it! The Refresh all could also be added to the QAT.
There's plenty of other embellishments possible - making the cell Data Validation lists, breaking the date into three cells for each date (Day, Month, Year) and assembling the named date cell from those three cells, and plenty others!
Hope that helps.
 
Upvote 0
Regarding allowing the user to adjust the date range, you could give them named range cells to enter the start and end dates:
Book1
AB
1Date Start: 01/01/2023
2End Date:03/15/2023
Sheet1
Cell Formulas
RangeFormula
B2B2=TODAY()

Cell B1 is named StartDate, and cell B2 is named EndDate. They can be brought into Power Query without having to turn them into tables:
Power Query will bring them in as a table with one column (Column1) and one row with the date in the single cell.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="StartDate"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    Column1 = ChangedType{0}[Column1]
in
    Column1
The ChangeType step is needed because the simple date will be brought in as a DateTime data type. Then right click on the date (not the column) and select Drill Down. The Queries will have the same name as the Named Range - StartDate and EndDate in this case.
Those names can now be used in your SQL queries. I'm not familiar enough with SQL, but in M Code you could turn
Power Query:
= Table.SelectRows(#"Changed Type", each [Date] >= #date(2023, 2, 1) and [Date] <= #date(2023, 3, 15))
into
Power Query:
= Table.SelectRows(#"Changed Type", each [Date] >= StartDate and [Date] <= EndDate)
with the same result.
Of course, if they change the date, they'll have to learn how to navigate to the Refresh All button and click it - or save the file and re-open it! The Refresh all could also be added to the QAT.
There's plenty of other embellishments possible - making the cell Data Validation lists, breaking the date into three cells for each date (Day, Month, Year) and assembling the named date cell from those three cells, and plenty others!
Hope that helps.
That is awesome, thank you!
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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