Home » Querying SQL Server Data with Power BI Desktop

Now we can actually edit the queries to match the requirements for a report’s design. The main Edit query window opens in a completely separate window as a shown below.

Edit query Main window

To allow for access to some additional information right at the get go, I would recommend immediately jumping to the View tab on the Ribbon, as seen below and then 1) click on the query settings button which will show several properties in the Query Settings property box (right side in below screen print) and 2) click the Formula Bar check box. The formula bar will allow the query designer to see what formula is being used to retrieve the data.

View Changes

Advanced Editor in Power BI Desktop

Another interesting view is to jump back to the home tab, and click on the Advanced Editor option. This button appropriately opens the advanced editor window, but more importantly shows you the query that is being executed against the data source. The syntax may look unfamiliar to you; PBID uses M, the Power Query Formula Language. I hope to do an upcoming tip on using the Power Query Formula Language, but for now, you can see in the below illustration the language specifies the data source connection and then requests the data from the SalesOrderHeader. The second illustration below filters on SalesOrderNumber (4 sales orders to be exact). You can see the filter details show up in M query.

Advanced Editor

Advanced Editor with filter

Filtered Rows in Power BI Desktop

You will also notice that once the filter was applied (you apply a filter similar to how you would in an Excel table by clicking the down arrow next to the column header), the Query Settings actually shows that the results were filtered by adding a Filtered Rows line to the Applied Steps.query settigns filter

filtered rows

These filtered results are then the basis for the data loaded onto to the main dashboard design grid; notice in the below illustration from the design grid that only the 4 filtered rows appear in the report results.

query results

The neat part is that we can easily go back to the Edit Queries window and “undo” our filter in the Query Setting window. As shown below, by clicking on the X icon next to Filtered Rows, we can remove the filter which results in all rows now being returned to the query results.

remove filter
remove filter results

Modifying Columns Displayed in Power BI Desktop

If we again select close and load on the Edit Query window, now all the rows are shown in our dashboard, as seen in the below illustration.design grid query results no filter

Just like it is not a good idea to do a “SELECT *” in regular T-SQL, it would also be beneficial to include only the columns we need for our reports. Thus the Query Editor provides a quick way to remove columns from a query by selecting the columns to be deleted and then right mouse clicking and selecting Remove Columns (or you could keep the selected columns by selecting Remove Other Columns).

remove columns

Group By in Power BI Desktop

An alternate transformation that we can complete is a Group by rollup. As shown below, we can transform the Sales Reason table into a rollup count by ReasonType using the Group by functionality. The below series of illustrations show the transformation that the table goes through to eventually generate a quick graph that shows the Count of ReasonTypes.group by select

group by window

group by results
group by results in grid

Duplicate a Query in Power BI Desktop

We can also easily duplicate a query table by right mouse clicking on a table and then selecting Duplicate. Now we have the ability to make adjustments to the new query without impacting the original query.duplicate
duplicate table

Rename a Query in Power BI Desktop

Furthermore, we can easily rename the query, as shown below, by editing the Name Property.rename query

Split a Column in Power BI Desktop

Next we can easily parse out a column by splitting a column into several parts. First as illustrated below, we select the column, then click Split Column and choose by Number of Characters. Next, we tell the query designer how to split up the column and finally tell it to do the split repeatedly until the end of the string.split column

split column window
split column results

Review of Advanced Editor with all Changes

If we review both the Advanced Editor and the Query Settings in the below screen print, you will notice that it shows all the work we have done on the query including duplicating a column, reordering the column, and splitting the column.New M query

Data Type Change in Power BI Desktop

Finally, we can easily change the data type of a column. For instance, we can adjust a DateTime field to Data Only by using the date only functionality.dateonly

Conclusion

The Edit Query functionality in the Power BI Desktop (PBID), even in these early stages of its release, contains a rich set of features and functionality. For instance, PBID provides the ability to: 1) duplicate a column 2) split a column into parts 3) to change column data types. Furthermore, we can filter the data based on column values which in turn filter the results on the dashboard. Finally, we can use the group by function to aggregate values in a query. We have touched just the tip of the BPID functionality, so look for more tips on the new Power BI Desktop and how to get the most out of this new tool.

Next Steps

MSSQLTips.com delivers SQL Server resources to solve real world problems for DBAs, Architects, DevOps Engineers, Developers, Analysts, Cloud and Business Intelligence Pros – all for free. The content we serve is all human written and based on our authors’ real-world experience. Check out tips, articles, scripts, videos, tutorials, live events and more all related to SQL Server on-premises and in the cloud.