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
- Get data › SQL Server › Server and Database › expand Advanced options.
- Paste the SQL into SQL statement › OK. Power BI may ask Native Database Query – You are about to run a native database query › Run.
- 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.
Ravindra Bagale's Tip – मराठी
मित्रांनो, बरेच students native query box मध्ये SELECT * लिहितात आणि मग Power Query मध्ये columns काढतात, त्यामुळे database तरीही सगळं पाठवतो. SQL मध्ये फक्त लागणारे columns लिहा, ORDER BY टाळा (model rows ची order ठेवत नाही), आणि लांब query चा सगळ्यांना पुन्हा वापरता येईल असा view बनवायला DBA ला सांगा. हा नियम लक्षात ठेवा.
Ravindra Bagale's Tip – हिंदी
दोस्तों, बहुत से students native query box में SELECT * लिखते हैं और फिर Power Query में columns हटाते हैं, तो database फिर भी सब कुछ भेजता है. SQL में सिर्फ़ ज़रूरी columns लिखो, ORDER BY छोड़ दो (model rows का order नहीं रखता), और DBA से कहो कि लंबी query को एक view बना दे जिसे सब दोबारा इस्तेमाल कर सकें. यह नियम याद रखो.