Ravindra BagaleCourses & study guides

4. Connecting to Databases: SQL Server, MySQL, DirectQuery and Live Connection

4.3 Advanced Options and Native SQL Queries

Clicking Advanced options in the SQL Server dialog shows:

Option What it does
Command timeout in minutes How long to wait for a slow query before failing
SQL statement Your own SQL query instead of choosing tables in the Navigator
Include relationship columns Adds columns to navigate to related tables (usually untick for cleaner tables)
Navigate using full hierarchy Shows schemas as folders in the Navigator
Enable SQL Server Failover support For high-availability server setups

Example 1 – order lines for our six Maharashtra cities (JOIN + WHERE):

SELECT  o.OrderID, o.OrderDateTime, o.ProductID, o.Quantity, o.Amount,
        o.DeliveryTimeMins, o.OrderStatus, o.Platform,
        s.StoreID, s.StoreName, s.Area, s.City
FROM    dbo.Orders    AS o
INNER JOIN dbo.DarkStore AS s ON s.StoreID = o.StoreID
WHERE   s.City IN ('Pune', 'Solapur', 'Nashik', 'Sambhaji Nagar', 'Kolhapur', 'Nagpur')
  AND   o.OrderDateTime >= '2025-01-01';

Example 2 – daily summary per city and platform (GROUP BY):

SELECT  CAST(o.OrderDateTime AS date)   AS OrderDate,
        s.City, o.Platform,
        COUNT(DISTINCT o.OrderID)       AS Orders,
        SUM(o.Amount)                   AS Sales,
        AVG(CAST(o.DeliveryTimeMins AS decimal(10,2))) AS AvgDeliveryMins
FROM    dbo.Orders o
JOIN    dbo.DarkStore s ON s.StoreID = o.StoreID
WHERE   o.OrderStatus = 'Delivered'
GROUP BY CAST(o.OrderDateTime AS date), s.City, o.Platform;

Steps in Power BI

  1. Get data › SQL Server › Server and Database › expand Advanced options.
  2. Paste the SQL into SQL statement › OK. Power BI may ask Native Database Query – You are about to run a native database query › Run.
  3. Preview the result › Transform Data.

Native queries and query folding

After a hand-written SQL statement, Power Query usually cannot fold the next steps back to the server, so filters and merges you add later run on your computer. Prefer the Navigator (or a database view) and let Power Query generate SQL. Use native SQL for logic that must run in the database, and put all filters inside the SQL.

Tip

Always write an explicit column list instead of SELECT *. New columns added to the table later will not break your report, and less data is transferred.

Practice task

Lakshat theva, write a native SQL query that returns cancelled Amazon Now orders in Nagpur and Kolhapur with Store Name and Area. Then do the same using the Navigator + Power Query filters, and compare View Native Query (next section).

Ravindra Bagale's Tip

Friends, many students write SELECT * in the native query box and then remove columns in Power Query, so the database still sends everything. List only the columns you need in the SQL, leave out ORDER BY (the model doesn't keep row order), and ask the DBA to turn a long query into a view that everyone can reuse. Remember this rule.