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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
ALMalecha
Frequent Visitor

Dynamic M Query against Azure SQL Database - Direct Query

I'm creating a PBI report that utilizes Direct Query, and we want it to return one record.

 

I've been using the article Dynamic M query parameters in Power BI Desktop - Power BI | Microsoft Docs for reference, and have my query updated. 

 

WorkOrderID is a parameter created in Manage Parameters. 

 

 

 

let   
    selectedworkorder = Text.Combine ({"'", Number.ToText(WorkOrderID), "'"}),
    dynamicfilter = Text.Combine({" where dm.workorderid = ",  selectedworkorder}),
    
    reportquery = "
           Select DM.Division, DM.Foreman, DM.Customer, J.PONumber, DM.JobName, DM.Yard, DM.WorkDate, weekend.Date AS WeekEndDate, DM.WorkStart, DM.LunchStart, DM.LunchEnd, DM.WorkEnd, DM.InvoiceProperty1Label, DM.InvoiceProperty1Value, DM.InvoiceProperty2Label,  DM.InvoiceProperty2Value, DM.InvoiceProperty3Label, DM.InvoiceProperty3Value, DM.InvoiceProperty4Label, DM.InvoiceProperty4Value, DM.InvoiceProperty5Label, DM.InvoiceProperty5Value, DM.BillableType, DM.WorkActivity, DM.MasterTimesheetID,           DM.WorkOrderID          from dbo.WorkOrderDataMart DM         join dbo.DimJob J on DM.JobID = J.JobID          join dbo.FactTimesheet t on DM.WorkOrderID = T.TimesheetID         join dbo.DimDate weekend on t.WeekEndDateID = weekend.DateKey ",

    FinalQuery = Text.Combine({reportquery, dynamicfilter}),

    Source = Sql.Database("proeazuretest.database.windows.net", "WJC", FinalQuery)  
    
in
    Source

 

 

 

However, I'm getting the below error.   When I pull out the query itself and run against our DB, it works fine.   I'm wondering if I have a syntax error, or if someone has encountered this, and found a resolution?

Thank you, 

 

 

Expression.Error: We cannot convert the value "
Select ..." to type Record.
Details:
Value=
Select DM.Division, DM.Foreman, DM.Customer, J.PONumber, DM.JobName, DM.Yard, DM.WorkDate, weekend.Date AS WeekEndDate, DM.WorkStart, DM.LunchStart, DM.LunchEnd, DM.WorkEnd, DM.InvoiceProperty1Label, DM.InvoiceProperty1Value, DM.InvoiceProperty2Label, DM.InvoiceProperty2Value, DM.InvoiceProperty3Label, DM.InvoiceProperty3Value, DM.InvoiceProperty4Label, DM.InvoiceProperty4Value, DM.InvoiceProperty5Label, DM.InvoiceProperty5Value, DM.BillableType, DM.WorkActivity, DM.MasterTimesheetID, DM.WorkOrderID from dbo.WorkOrderDataMart DM join dbo.DimJob J on DM.JobID = J.JobID join dbo.FactTimesheet t on DM.WorkOrderID = T.TimesheetID join dbo.DimDate weekend on t.WeekEndDateID = weekend.DateKey where dm.workorderid = '32253'
Type=[Type]

1 ACCEPTED SOLUTION
ALMalecha
Frequent Visitor

Figured it out - it was a syntax error:

The source statement should have been:  Source = Sql.Database("proeazuretest.database.windows.net", "WJC", [Query=FinalQuery]

 

let   
    selectedworkorder = Text.Combine ({"'", Number.ToText(WorkOrderID), "'"}),
    dynamicfilter = Text.Combine({" where dm.workorderid = ",  selectedworkorder}),
    
    reportquery = " Select DM.Division, DM.Foreman, DM.Customer, J.PONumber, DM.JobName, DM.Yard, DM.WorkDate, weekend.Date AS WeekEndDate, DM.WorkStart, DM.LunchStart, DM.LunchEnd, DM.WorkEnd, DM.InvoiceProperty1Label, DM.InvoiceProperty1Value, DM.InvoiceProperty2Label,  DM.InvoiceProperty2Value, DM.InvoiceProperty3Label, DM.InvoiceProperty3Value, DM.InvoiceProperty4Label, DM.InvoiceProperty4Value, DM.InvoiceProperty5Label, DM.InvoiceProperty5Value, DM.BillableType, DM.WorkActivity, DM.MasterTimesheetID, DM.WorkOrderID from dbo.WorkOrderDataMart DM  join dbo.DimJob J on DM.JobID = J.JobID  join dbo.FactTimesheet t on DM.WorkOrderID = T.TimesheetID join dbo.DimDate weekend on t.WeekEndDateID = weekend.DateKey",

    FinalQuery = Text.Combine({reportquery, dynamicfilter}),

    Source = Sql.Database("proeazuretest.database.windows.net", "WJC", [Query=FinalQuery])  
    
in
    Source

 

View solution in original post

2 REPLIES 2
ALMalecha
Frequent Visitor

Figured it out - it was a syntax error:

The source statement should have been:  Source = Sql.Database("proeazuretest.database.windows.net", "WJC", [Query=FinalQuery]

 

let   
    selectedworkorder = Text.Combine ({"'", Number.ToText(WorkOrderID), "'"}),
    dynamicfilter = Text.Combine({" where dm.workorderid = ",  selectedworkorder}),
    
    reportquery = " Select DM.Division, DM.Foreman, DM.Customer, J.PONumber, DM.JobName, DM.Yard, DM.WorkDate, weekend.Date AS WeekEndDate, DM.WorkStart, DM.LunchStart, DM.LunchEnd, DM.WorkEnd, DM.InvoiceProperty1Label, DM.InvoiceProperty1Value, DM.InvoiceProperty2Label,  DM.InvoiceProperty2Value, DM.InvoiceProperty3Label, DM.InvoiceProperty3Value, DM.InvoiceProperty4Label, DM.InvoiceProperty4Value, DM.InvoiceProperty5Label, DM.InvoiceProperty5Value, DM.BillableType, DM.WorkActivity, DM.MasterTimesheetID, DM.WorkOrderID from dbo.WorkOrderDataMart DM  join dbo.DimJob J on DM.JobID = J.JobID  join dbo.FactTimesheet t on DM.WorkOrderID = T.TimesheetID join dbo.DimDate weekend on t.WeekEndDateID = weekend.DateKey",

    FinalQuery = Text.Combine({reportquery, dynamicfilter}),

    Source = Sql.Database("proeazuretest.database.windows.net", "WJC", [Query=FinalQuery])  
    
in
    Source

 

Hi @ALMalecha ,

 

I am glad that you can figure out your problem. Please kindly accept your workaround as the solution. More people who with similar problems will benefit from it.

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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