Quantcast
Channel: Smart ERP Solutions - Microsoft BI
Viewing all articles
Browse latest Browse all 22

SQL Server data in Power BI Desktop

$
0
0

Power BI Desktop provides an integrated environment for retrieving and transforming data in order to define datasets that you can use to create reports containing different types of visualizations. After you’ve created your datasets and reports, you can publish them to the Power BI website, where you can work with them or share them as you would those created through the web tools.

Although similar to the Power BI service in many ways, Power BI Desktop provides a more robust environment for transforming data, in addition to being able to retrieve data from a wider range of sources, including SQL Server.

Retrieving data from SQL Server

To connect to a data source, select the data source from the Get Data window and select Connect. In the following image, Sql server database is selected from the other data connection category. 

Once click on connect and provide the sql server database name as below 

Whichever approach you take, you’ll be prompted to provide connection information, including the SQL Server instance and, optionally, the database name. You can also specify a T-SQL query at this time, if you want to be more precise in defining the data you initially return. You can select the tables and click lode button you want to import into Power BI Desktop, as shown in the following figure.

 

At the bottom of the Navigator dialog box you have options for either loading the data or editing the data. If you   click Load, you will load the data as is, and the tables will be added as datasets, which you can view in Data view. From there, you can launch Query Editor to transform the data or start using the data to create visualizations. Or you can instead click the Edit button to launch right into Query Editor, which is what we’ll do here.

Transforming data in Query Editor

Transforming the data is where all the fun begins. In Query Editor, you apply a series of steps to each data set in order to transform the data. Query Editor records each transformation in the Applied Steps box at the bottom of the Query Settings pane. The following figure shows the Person dataset, with two steps applied.

By default, a dataset starts with the steps Source and Navigation, which define the initial connection and data retrieval. At this point, the data is just as we imported it from the source database.

You can now continue to the next step, which is to remove all but the Business EntityID, First Name, and Last Name columns. To remove the other columns, click the header of the first column, press and hold the Control key, and click the other column headers. After you’ve selected all the columns to be deleted, right-click the header of one of the selected columns, and then click Remove Columns. Query Editor will remove the columns and add the Removed Columns step to the Applied Steps box.

The only transformation left is to sort the rows, based on the LastName value. To do so, once again click the arrow at the top of the column and click Sort Ascending. This sorts the rows and adds the Sorted Rows step to the Applied Steps box. You should be left with a dataset similar to what’s shown in the following figure.

There are a couple items worth noting in the figure. First, you have a set of columns in your dataset that contain oddly colored values called Table and Value. Query Editor provides this information to point you to data related in other tables in the source data. However, this information does not show up in your final dataset.

You might have also noticed that I renamed the dataset to Sales, to make it more suitable to the type of data. In addition, we now have steps in the Applied Steps box. Power BI Desktop retains these steps so we can return to the query at any time to view how the data was transformed as well as modify those transformations if needed. We can even move steps up or down or remove them altogether. Be careful, however, when you start changing or moving steps around because there can be dependencies between steps and you could end up with unexpected results.

After you apply these transformations, you should end up with a dataset that looks similar to the one shown in the following figure.

For the Sales Territory table, shorten the name to exclude the schema and apply the following transformations:

·         Remove all columns except Territory ID, Name, Country Region Code, and Group.

·         Rename the Country Region Code column to the Country Code column.

Your final table should look something like what’s in the following figure.

That’s all there is to applying basic transformations to a dataset. Overall, Power BI Desktop makes this an intuitive and painless process. These might not be particularly complex transformations, but for some users, they will be plenty.

Query Editor includes lots of other types of transformations for performing such tasks as removing duplicates, splitting columns, merging queries, replacing values, pivoting data, or applying mathematical expressions. You can even access the DAX code underlying the dataset, as shown in the following figure, as well as access the DAX expressions used to transform the data.

For now, working with these expressions, like other advanced features, is beyond the scope of this article (but will perhaps get covered in a subsequent article or two). For now, I encourage you to play around with the transformations and get a sense of what you can do in Query Editor. In the meantime, let’s look at a couple other steps we can take right now.

Grouping data in Query Editor

Power BI Desktop lets us group data just like we can in SQL Server. For example, suppose we want to include a visualization in our report that aggregates sales data across years. One approach we can take is to duplicate the Sales dataset and use the copy to create a separate, aggregated dataset.

To duplicate the Sales dataset in Query Editor, right-click the dataset in the left pane, and then click Duplicate. Change the name of the new dataset to Annual Sales, and then remove all but the Order Date and Subtotal columns.

We are now ready to group the data. To do so, click the Group By button on the Transform ribbon. This launches the Group By dialog box, shown in the following figure.

The figure shows how you should configure the settings to aggregate the Sub Total values based on the Order Date column. We will end up with a column named Yearly Sales that shows the total amount of sales for each year.

That’s all there is to grouping data. You might have noticed that the Applied Steps box includes all the original transformations from the Sales dataset. You can play with these if you like, or leave things just as they are. You could have also re-imported the table and grouped the data that way.


Viewing all articles
Browse latest Browse all 22

Trending Articles