Forum Discussion
New to BI. Writing SQL queries to parse data. I am obviously missing something.
1) No, that SQL statement box is actually for SQL statements as you would normally write, not for the Power Query language (it's called M. I'm just going to call it that now). It's just not generally recommended to use that feature at all. You don't even have to write M language formulas yourself. The buttons generate them. If you feel ambitious you can write them yourself but it's rarely necessary unless you're doing really crazy stuff.
2) You just connect to your database and pick a table. You can mess with the query in the query editor from there. That's where you would do things like pick specific columns or filter rows.
3) Yes, this is done by merging queries in the query editor.
4) Using the buttons does give you the exact data you need. The order of operations is simply different from what you're used to.
I second Anonymous's view recommendation. I wish I had that option in my current situation.
When I select from a SQL source in PBI Desktop I always make my queries in the Query Editor like this. I prefer using the advanced editor to write the M# code myself, and use parameters to have my values for Server and Database. I do this for 2 reasons - I can easily create a new query by dublicating one of my other queries and then just replace the T-SQL code and I just have to change 2 values if my data source changes since I just change the parameters and then don't have to change all my queries.
let
Source = Sql.Database(Server, Database, [Query="
--SQL Query Start
SELECT
a.DWID_Customer AS 'CustomerKey'
, a.CustomerNo AS 'Customer No'
, a.Name AS 'Customer Name'
, a.CustomerLabel AS 'Customer'
, a.CustomerPostingGroup AS 'Customer Posting Group'
, a.Country AS 'Customer Country'
, a.City + ', ' + a.Country AS 'Customer City'
, a.SalespersonCode AS 'Salesperson Code'
, a.SalespersonName AS 'Salesperson Name'
, a.SalespersonName + ' - ' + a.SalespersonCode AS 'Salesperson'
FROM D_Customer a
--SQL Query End
"])
in
Source
- alwweb7 years agoAdvocate II
If the SQL refreshes are timing out, you are probably correct that you are pulling too much data at once, but, doing all of the joins in the SQL may also slow it down more.
I am a SQL person first and a Power BI person second, so I look at things a little differently from most people on this forum.
1) I like bringing in all of the tables individually and using the relationships in Power BI rather than "flattening" my data source by doing all of the joins when pulling in the data. This gives me more power for creating my formulas, etc inside of Power BI.
2) I would prefer to bring in one table at a time with a query for each tables (some denormalization where it makes sense by doing the joins) if I need to remove a lot of columns or some of the rows. This way, if the SQL database is optimized properly, it will take much less time to refresh. Rather than pulling in all of the data and then removing a bunch, we only pull what we need.
On your comment from page 3 that I responded to earlier. I realized you missed where the other user was putting in the M code. It is after the import happens, by clicking Advanced Editor on the Home tab of the Power Query Editor.
For books, to understand the concept of normalization that I mentioned earleir in this comment, I recommend any of the edtitions of Jan Harrington's Relational Database Design Clearly Explained. I like the 1st edition best if you can find it in a used book store.
For writing T-SQL (MIcrosoft's flavor of the SQL language - stands for Transact-SQL) there are a huge number of books to choose from. If you are going to be doing a lot of query writing and don't have an IT team to create VIEWS that already have what you need in them, I recommend taking a 3 day course. If you like learning on your own, one of the most popular books are the ones by Itzik Ben-Gan. Either T-SQL Querying or SQL Fundamentals.
I hope some of this helps.
- alwweb6 years agoAdvocate II
newpi - my default database name is part of my DSN definition on a page with an option to change the default database.
If your databasename can't be defined in the DSN, is there a way to set it in the connection string above the SQL statement by using the database= option?
- newpi6 years agoHelper V
This is now solved. The 4 missing columns data was loading as Binary and I converted it to text type in the query editor and could load all columns
- AFoulk8 years agoHelper II
sdjensen wrote:When I select from a SQL source in PBI Desktop I always make my queries in the Query Editor like this. I prefer using the advanced editor to write the M# code myself, and use parameters to have my values for Server and Database. I do this for 2 reasons - I can easily create a new query by dublicating one of my other queries and then just replace the T-SQL code and I just have to change 2 values if my data source changes since I just change the parameters and then don't have to change all my queries.
let Source = Sql.Database(Server, Database, [Query=" --SQL Query Start SELECT a.DWID_Customer AS 'CustomerKey' , a.CustomerNo AS 'Customer No' , a.Name AS 'Customer Name' , a.CustomerLabel AS 'Customer' , a.CustomerPostingGroup AS 'Customer Posting Group' , a.Country AS 'Customer Country' , a.City + ', ' + a.Country AS 'Customer City' , a.SalespersonCode AS 'Salesperson Code' , a.SalespersonName AS 'Salesperson Name' , a.SalespersonName + ' - ' + a.SalespersonCode AS 'Salesperson' FROM D_Customer a --SQL Query End "]) in SourceCould someone please spell this out easier for me? I am not a SQL guy but really need to go this route with my complex BI model. I have many tables loading (some with millions of rows that I would like to try and filter before they load into query editor first) and many editor steps that I feel could be reduced by SQL coding doing a lot of the joins and filters I need done.
My model times out and fails scheduled refresh during business hours a lot due to everything taxing on the SQL server.
Is there a good beginniners book to SQL I could pick up to maybe learn enough to join and filter as I need too?
- ElizabethTachji7 years agoHelper III
Hi,
Where does the data reside?
Elizabeth Tachjian
Analytics Realtime
- newpi6 years agoHelper V
Hi sdjensen
I'm using MYSQL and connecting to the server using ODBC connection.
I'm very new to power bi and I want to do something similar. Copy paste my query into power bi and load the data. How do I go about it? The syntax i'm using is similar to yours.Source = Odbc.DataSource("dsn=1accountsdbuni", [Odbc.Query=
--- Query here ----])
Syntax works fine but I'm getting following error:
Expression.Error: The provided options are not valid. Details: [List]
- alwweb6 years agoAdvocate II
Can you tell me what steps you are following to get to the point where you are receiving the error? I don't have a MySQL to play with, but I may be able to spot where there is a problem.
I assume when you connect to the ODBC source, after selection yoru DSN in the From ODBC window, then you add your query, exactly as it is in the MySQL editor into the SQL statement (optional) box, then get your error when you click OK. Is there anything else going on?
Have you tried using the MySQL connector?
- newpi6 years agoHelper V
alwweb
I'm trying to run a MYSQL query within Power BI. I have connected to MYSQL using an ODBC connector. I was facing issues connecting directly with MYSQL and hence went the ODBC route and that worked for.
Now all I want to do is run a MYSQL query I have written with multiple joins and everything within Power BI. The query runs fine and gives me result in a MYSQL client but unable to make it run in Power BI. - alwweb6 years agoAdvocate II
newpi Thank you for the additional info.
I just tested an ODBC connection to one of my Microsoft SQL databases (this is all I have right now) and it worked. I created an ODBC connection that said it was 32/64 bit and a User DSN. I would assume it needs to match the PowerBI "bitness" and the server bitness.
I then chose Import from ODBC and my DSN and query was as shown in the image below. It connected without any errors. On the security page, I picked Windows security, but make sure that you are connecting with a method supported by MySQL. ODBC connections can be weird, so I'm sorry that I don't have a MySQL server to test this with to help you out.
Please let me know if this helps. - newpi6 years agoHelper V
alwweb Thanks. Just solved this. I just had to modify my query. In MYSQL client I was just writing and doing a select * from table name and that was working fine there but in power bi I had to do Select * from database.tablename and it worked.
Also, but I'm facing another issue here. I have close 8 columns in my output but in the power bi after connecting with SQL optional statement I'm only seeing first 4 columns and missing the others. This query has multiple left joins so I don't know if that is the issue.Power BI Columns
All columns in MYSQL client