Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DukeCarey
Regular Visitor

Pass date parameter to a SQL function

I need to pull data via a SQL function that requires a date parameter.  The SQL query is very simple: SELECT * FROM dbo.FunctionName('20230630').  

 

All the search results I've found focus on using date parameters to filter the dataset once it is returned to Power Query. However, this function does not return a date column.  Thus, filtering the dataset in Power Query is not an option.  

 

Is there any way to do this?

 

Thanks

 

 

2 ACCEPTED SOLUTIONS

@DukeCarey 

I created a parameter, called startdate, in your case it is linked to an Excel sheet, it;s fine. Here is the modified code, which should work for as well. Modify the Date format as per your source system.

let
    DateParam = "'" & Date.ToText(StartDate, "yyyy-MM-dd") & "'",
    Source = Sql.Database(".", "ContosoRetailDW_2019", [Query="SELECT * FROM dbo.GetCustomersByDateFirstPurchase(" & DateParam & ")", CreateNavigationProperties=false])
in
    Source

  

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

@DukeCarey 

As there are two data sources talking to each other, the privacy level needs to be set, you may set it to None in this case: Go to Options and Settings then set it up for each source.

Fowmy_1-1702184633514.png

 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

6 REPLIES 6
Fowmy
Super User
Super User

@DukeCarey 
When you connect using the SQL as the source, your query should look like this. I created a function with a date parameter and and it works:

let
    Source = Sql.Database(".", "ContosoRetailDW_2019", [Query="SELECT * FROM dbo.GetCustomersByDateFirstPurchase('2004-03-14')", CreateNavigationProperties=false])
in
    Source

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thanks for the response.  I should have been a little clearer with my question.  I'm setting this up for a user who will be unwilling/unable to edit a single query to use a different date, and in this case there are several queries in the workbook that will use the date. Only one will use it as the input to the SQL function, though.

 

So, the goal is to use an Excel table (residing in K1:K2) where the user could change the date and the one query would use THAT date for the function's parameter, while the other queries will filter their results using the same date parameter.

I hope I've made that clearer rather than confusing the issue

The particulars are that I've converted the table into a parameter named StartDate that can be used to filter other queries, but I also want it to go in as the parameter to that SQL function.

@DukeCarey 

I created a parameter, called startdate, in your case it is linked to an Excel sheet, it;s fine. Here is the modified code, which should work for as well. Modify the Date format as per your source system.

let
    DateParam = "'" & Date.ToText(StartDate, "yyyy-MM-dd") & "'",
    Source = Sql.Database(".", "ContosoRetailDW_2019", [Query="SELECT * FROM dbo.GetCustomersByDateFirstPurchase(" & DateParam & ")", CreateNavigationProperties=false])
in
    Source

  

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thanks Fowmy, I think we're inching closer now.  After mimicking what you posted I ran into the formula.firewall error and I'm stumped there now.

 

Formula.Firewall: Query 'PivotPurchaseTypesSince' (step 'Source') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.

 

 

@DukeCarey 

As there are two data sources talking to each other, the privacy level needs to be set, you may set it to None in this case: Go to Options and Settings then set it up for each source.

Fowmy_1-1702184633514.png

 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

BTW, I tried to "flatten" the query based on a blog post and tried this:

let
SourceX = Excel.CurrentWorkbook(){[Name="StartDate"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
Column1 = #"Changed Type"{0}[Column1],
DateParam = "'" & Date.ToText(Column1, "yyyy-MM-dd") & "'",

Source = Sql.Database("test.database.windows.net", "testdata", [Query="SELECT * FROM rpt.PivotPurchaseTypesSince(" & DateParam & ")"])
in
Source

 

but then got this error:

Expression.Error: A cyclic reference was encountered during evaluation.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.