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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
FarmerKenny
Helper II
Helper II

How do I use the DATE parameter with my SQL Direct Query?

I have a simple select statment with a where clause that contains date. I would like to replace the Date column reference with a DATE datatype parameter. I have searched this forum numerous times and I can only find examples where the parameter is a TEXT datatype.  If that's the only way that it can be done, I will accept that.  How do I formate the DATA Parameter reference in my direct query SQL statment?  '"StartTime"' is how my date parm is  currently formatted and that throws a Token Comma expected error.  Help!

1 ACCEPTED SOLUTION
FarmerKenny
Helper II
Helper II

The answer turned out to be making every column be the same datatype -- in this case a DATE.  The syntax that finally worked was this: 

CAST(CreatedDate as DATE)  >= '"&StartTime&"'

View solution in original post

3 REPLIES 3
FarmerKenny
Helper II
Helper II

The answer turned out to be making every column be the same datatype -- in this case a DATE.  The syntax that finally worked was this: 

CAST(CreatedDate as DATE)  >= '"&StartTime&"'
FarmerKenny
Helper II
Helper II

 

let
    Source = Sql.Database("hadev19", "ECCOVIA", [Query="WITH AllRows AS (#(lf)select#(lf)                    LedgerType,#(lf)                    FamilyAcct,#(lf)                    ClientID,#(lf)                    CreatedDate,#(lf)                    Charge_Amount,#(lf)                    [Service],#(lf)                    PaymentsToDate,#(lf)                    Balance#(lf)                   #(lf)                from ECCOVIA.dbo.[Account_LedgerByFamily] #(lf)where  LedgerType in ('Charge', 'Payment') #(lf)and CAST(CreatedDate as DATE)  >= '"StartTime"' #(lf)--group by LedgerType, FamilyAcct, ClientID#(lf))#(lf)   #(lf)select#(lf)           #(lf)      LedgerType,#(lf)      FamilyAcct,#(lf)      ClientID,      #(lf)        MAX(CAST(CreatedDate as DATE)),#(lf)        Charge_Amount,#(lf)        [Service],#(lf)        PaymentsToDate,#(lf)         Balance#(lf)      #(lf)from#(lf)#(lf)AllRows#(lf)where NOT Exists (select * from ECCOVIA.dbo.[Account_LedgerByFamily] b#(lf)where b.ClientID = AllRows.ClientID#(lf)And LedgerType = 'Payment'#(lf)and CreatedDate  >= '"StartTime"'#(lf)group by LedgerType, FamilyAcct, ClientID,Charge_Amount,[Service], PaymentsToDate, Balance#(lf);"]),
    #"Renamed Columns" = Table.RenameColumns(Source,{{"", "CreatedDate"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"CreatedDate", type date}, {"FamilyAcct", type text}, {"ClientID", type text}})
in
    #"Changed Type"

My Query is above. 

 

@FarmerKenny Write your code like this:

 

let
    Source = Sql.Database (
        "autumn\sql2019",
        "ContosoRetailDW",
        [
            Query = 
        "
        SELECT * 
        FROM Dates
        WHERE [Date] = 
        "
            & "'"
            & DateTime.ToText ( TargetDateParameter )
            & "'"
        ]
    )
in
    Source

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors