Execute a DAX query (select columns)
Executes a DAX query using SELECTCOLUMNS to retrieve specific columns. Supply the DAX query in the Query parameter.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
SELECT *
FROM execute_dax_query
WITH (Query='EVALUATE
SELECTCOLUMNS (
''MyTable'',
"MyColumn1 alias", [MyColumn1],
"MyColumn2 alias", [MyColumn2]
)
ORDER BY "MyColumn2 alias"'
)
-- More info on 'EVALUATE' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/selectcolumns/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_POWER_BI_IN_GATEWAY], 'SELECT *
FROM execute_dax_query
WITH (Query=''EVALUATE
SELECTCOLUMNS (
''''MyTable'''',
"MyColumn1 alias", [MyColumn1],
"MyColumn2 alias", [MyColumn2]
)
ORDER BY "MyColumn2 alias"''
)
-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/selectcolumns/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_POWER_BI_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM execute_dax_query
WITH (Query=''EVALUATE
SELECTCOLUMNS (
''''MyTable'''',
"MyColumn1 alias", [MyColumn1],
"MyColumn2 alias", [MyColumn2]
)
ORDER BY "MyColumn2 alias"''
)
-- More info on ''EVALUATE'' statement and DAX queries:
-- https://dax.guide/st/evaluate/
-- https://dax.guide/selectcolumns/
-- https://learn.microsoft.com/en-us/dax/dax-queries
-- https://learn.microsoft.com/en-us/dax/dax-syntax-reference'
EXEC (@MyQuery) AT [LS_TO_POWER_BI_IN_GATEWAY]