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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
smpa01
Super User
Super User

POWER Query SQL Statement Dynamic Filtering

Hi,

 

I am building a query by connecting to a SQL Server Table. To avoid having all the rows I have done a custom SQL Statement to filter while connceting to SQL server as following.

 

select [$Table].[CreatedDate] as [CreatedDate],
       [$Table].[Site ID] as [Site ID]       
   from [dbo].[vw_xxxxxxx] as [$Table]
   WHERE ([CreatedDate] >= N'2016-08-20 00:00:00') 

Now whenever I run this query I need to see all the rows WHERE ([CreatedDate] >=(Today) - (2 years).

 

So if I run it today (08/20/2018), I need to see the rows with Created Date values from 08/20/2016. If I run it tomorrow (08/21/2018), I need to see the rows with Created Date values from 08/21/2016. 

 

Is there any way to dynamically pass on this criteria to SQL statement.

 

Appreciate any help.

 

Thanks

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION
Thejeswar
Resident Rockstar
Resident Rockstar

Hi @smpa01,

Since this is a direct SQL Statement, you should be able to directly use GETDATE() function as in case of SQL Server.

 

Did you try something like this

 

select [$Table].[CreatedDate] as [CreatedDate],
       [$Table].[Site ID] as [Site ID]       
   from [dbo].[vw_xxxxxxx] as [$Table]
   WHERE ([CreatedDate] between getdate() and CAST((CAST(YEAR(getdate())-2 as VARCHAR(4))+'-'+CAST(MONTH(getdate()) as VARCHAR(2))+'-'+CAST(DAY(getdate()) as VARCHAR(2))) as DateTime)

 

View solution in original post

2 REPLIES 2
Thejeswar
Resident Rockstar
Resident Rockstar

Hi @smpa01,

Since this is a direct SQL Statement, you should be able to directly use GETDATE() function as in case of SQL Server.

 

Did you try something like this

 

select [$Table].[CreatedDate] as [CreatedDate],
       [$Table].[Site ID] as [Site ID]       
   from [dbo].[vw_xxxxxxx] as [$Table]
   WHERE ([CreatedDate] between getdate() and CAST((CAST(YEAR(getdate())-2 as VARCHAR(4))+'-'+CAST(MONTH(getdate()) as VARCHAR(2))+'-'+CAST(DAY(getdate()) as VARCHAR(2))) as DateTime)

 

Thanks @Thejeswar it worked. You saved me. Thanks mate.

 

This is what I have used and it did the trick

select [$Table].[CreatedDate] as [CreatedDate]
     from [dbo].[vw_xxxx] as [$Table]
   WHERE [CreatedDate] >= CAST((CAST(YEAR(getdate())-2 as VARCHAR(4))+'-'+CAST(MONTH(getdate()) as VARCHAR(2))+'-'+CAST(DAY(getdate()) as VARCHAR(2))) as DateTime)

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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