Search code examples
type-conversionconcatenationparameter-passingpowerquerym

Power Query throws: "We cannot apply operator & to types Text and Number." How do I embed a numeric parameter in the TSQL code of an M query?


I try to parametrize TSQL code in the Power Query M language.

I defined a self-built parameter TestID in the menu bar at the top. The parameter is a numeric value. Embedding it with [data_format_0] = "&TestID&") throws the error:

We cannot apply operator & to types Text and Number.
Details:
Operator=&

TestID = Excel.CurrentWorkbook() {[Name="TestID"]}[Content]{0}[Column1]

select ...   
where  
time@timestamp  
between  
(select distinct max([time@timestamp]) from [POD01EVENTLOG] where [data_format_4] = 1 and [data_format_0] = "&TestID&")  
and  
(select distinct max([time@timestamp]) + 60 from [POD01EVENTLOG] where [data_format_4] = 1 and [data_format_0] = "&TestID&")

And my Power Query M code is:

let 
    TestID = Excel.CurrentWorkbook() {[Name="TestID"]}[Content]{0}[Column1],
    Source = Sql.Database("SERVERNAME", "DBNAME", 
    [Query="
SELECT *

from [POD01RAWDATA]

where 
time@timestamp 
between 
(select distinct max([time@timestamp]) from [POD01EVENTLOG] where [data_format_4] = 1 and [data_format_0] = "&TestID&")
and 
(select distinct max([time@timestamp]) + 60 from [POD01EVENTLOG] where [data_format_4] = 1 and [data_format_0] = "&TestID&")
"])

in
    Source

The question: If Power Query throws: "We cannot apply operator & to types Text and Number.", how do I concatenate the numeric parameter in the M string?


Solution

  • You can't concatenate number and text so convert the testid to TEXT before concatenating it

    " & Number.ToText(TestID,"D") & "