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
RWNESB
Regular Visitor

Power Query SQL Statement Help >= Prior month start date

Hello,

It seems as though this would be simple, however I'm struggling to get this to work.  I have a basic SQL statement and want to add in my where clause a dynamic date.  I would like the date to be the prior month starting date.  Is this possible?  Any suggestions would be appreciated.  Thanks in advance.

RWNESB_0-1644000185912.png

 

1 ACCEPTED SOLUTION
watkinnc
Super User
Super User

The variable can be it's own query, just make a new blank query. Name it StartPriorMonth, and then in the formula bar:

 

= Text.From(Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()),-1)))

 

(also took care of making it a text value).  Now you can add it to your query with the ampersand:

 

WHERE
          Table.DateField>=" & StartPriorMonth)

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

View solution in original post

5 REPLIES 5
watkinnc
Super User
Super User

The variable can be it's own query, just make a new blank query. Name it StartPriorMonth, and then in the formula bar:

 

= Text.From(Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()),-1)))

 

(also took care of making it a text value).  Now you can add it to your query with the ampersand:

 

WHERE
          Table.DateField>=" & StartPriorMonth)

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Thank you!  Got it working.  I needed to tinker with the variable format a bit, but your original input was exactly what I needed.  You taught me a valuable lesson today, I really appreciate it.  

watkinnc
Super User
Super User

I would do this from the query editor, so that you can make a separate variable to put in your WHERE clause. Nevertheless, assuming your statement needs quotes in the query editor, so here is the record value to use in SQL.Databases:

 

[Query = "SELECT...WHERE TABLE.DATEFIELD >= "&Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()), -1))]

--Nate

 

 

 

 

 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Hi @watkinnc ,

Sincerely, thank you for taking the time to respond!  I'm close.  I tried without a variable... Unnsuccessfuly - my problem is that I don't know how to insert a variable within the Advanced Editor.  Pardon my ignorance, just never done this before.  I'm receiving an error "Expression.Error: We cannot apply operator & to types Text and Date."  It makes sense, since I have the string SQL statement that I'm trying to add a date to.  This is where I'm at (simplified):

 

let
     Source = Odbc.Query("dsn=ABC123", "

     SELECT

          Table.field1, table.field2, ...
     FROM

          Table
     WHERE
          Table.DateField>=" & Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()),-1))) 
in
     Source

 

How & where do I create the variable?

 

Thanks again!

tackytechtom
Super User
Super User

Hi @RWNESB,

 

If I understood correctly, you would like to fetch data from your table where DateField is prior the month of your StartDate.

In normal t-sql, I would probably write it like this:

WHERE
TABLE.DATEFIELD < DATEADD(MONTH,-1, TABLE.StartDate);

 

I could have misunderstood you, but the syntax might help you anyway.

 

Good Luck!

 

/Tom

https://www.tackytech.blog

https://www.instagram.com/tackytechtom



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

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.

Top Solution Authors