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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

SQL Statement Dynamic Date range

Hi all,

 

I need to make the EndDate and StartDate respectively, in the following line a dynamic date range.

 

Sql.Database("Server", "DB", [Query= "exec dbo.WhatEver 'yyyy-mm-dd', 'yyyy-mm-dd'"])

 

The above Mcode line was created by a SQL statement, exec dbo.WhatEver 'yyyy-mm-dd', 'yyyy-mm-dd'. As far as I know, I cannot use a where clause in an exec statement. How will one call this date range dynamically?

 

I have tried creating dataparameters in power query and then combining it in a text line with the exec statement but you cannot use such a text line for the Query.

 

 

1 ACCEPTED SOLUTION
Daryl-Lynch-Bzy
Community Champion
Community Champion

HI @Anonymous  - I believe you need to use an Open Rowset query to run a SQL stored proc.  Please read the following blog by Teo Lachev:  Power BI Dynamic M Query Parameters Reloaded – Prologika

View solution in original post

2 REPLIES 2
Daryl-Lynch-Bzy
Community Champion
Community Champion

HI @Anonymous  - I believe you need to use an Open Rowset query to run a SQL stored proc.  Please read the following blog by Teo Lachev:  Power BI Dynamic M Query Parameters Reloaded – Prologika

Hello Daryl,

 

I actually solved it by doing to following,

 

Start = Date.ToText(DateStart, "yyyy-MM-dd"),
End = Date.ToText(DateEnd, "yyyy-MM-dd"),
Query = "execute statement" & " " & "'" & End & "'" & ", " & "'" & Start & "'",

Source = Sql.Database("Server name", "Database", [Query= Query])

 

Where the DateEnd and DateStart were determined as follows

 

Date.AddDays(Date.EndOfWeek(Date.From(DateTime.LocalNow())), -8)

Date.AddDays(Date.EndOfWeek(Date.From(DateTime.LocalNow())), -15)

 

Respectively

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.