
- •Contributors
- •Table of Contents
- •Preface
- •Technical requirements
- •Installing a Power BI gateway
- •Getting ready
- •How it works
- •Authentication to data sources
- •Getting ready
- •How it works
- •Main challenges that Power Query solves
- •Getting ready
- •Technical requirements
- •Getting data and connector navigation
- •Getting ready
- •Creating a query from files
- •Getting ready
- •How it works...
- •Creating a query from a folder
- •Getting ready
- •How it works...
- •Creating a query from a database
- •Getting ready
- •How it works...
- •Creating a query from a website
- •Getting ready
- •How it works...
- •Technical requirements
- •Exploring Power Query Editor
- •Getting ready
- •Managing columns
- •Getting ready
- •Using data profiling tools
- •Getting ready
- •Using Queries pane shortcuts
- •Getting ready
- •Using Query Settings pane shortcuts
- •Getting ready
- •Using Schema view and Diagram view
- •Getting ready
- •Technical requirements
- •Formatting data types
- •Getting ready
- •Using first rows as headers
- •Getting ready
- •Grouping data
- •Getting ready
- •Unpivoting and pivoting columns
- •Getting ready
- •Filling empty rows
- •Getting ready
- •Splitting columns
- •Getting ready
- •Extracting data
- •Getting ready
- •Parsing JSON or XML
- •Getting ready
- •Exploring artificial intelligence insights
- •Getting ready
- •Technical requirements
- •Merging queries
- •Getting ready
- •Joining methods
- •Getting ready
- •Appending queries
- •Getting ready
- •Combining multiple files
- •Getting ready
- •Using the Query Dependencies view
- •Getting ready
- •Technical requirements
- •Setting up parameters
- •Getting ready
- •Filtering with parameters
- •Getting ready
- •Folding queries
- •Getting ready
- •Leveraging incremental refresh and folding
- •Getting ready
- •Disabling query load
- •Getting ready
- •Technical requirements
- •Using M syntax and the Advanced Editor
- •Getting ready
- •Using M and DAX – differences
- •Getting ready
- •Using M on existing queries
- •Getting ready
- •Writing queries with M
- •Getting ready
- •Creating tables in M
- •Getting ready
- •Leveraging M – tips and tricks
- •Getting ready
- •Technical requirements
- •Adding columns from examples
- •Getting ready
- •Adding conditional columns
- •Getting ready
- •Adding custom columns
- •Getting ready
- •Invoking custom functions
- •Getting ready
- •Clustering values
- •Getting ready
- •Technical requirements
- •Using Power BI dataflows
- •Getting ready
- •Centralizing ETL with dataflows
- •Getting ready
- •Building dataflows with Power BI Premium capabilities
- •Getting ready
- •Understanding dataflow best practices
- •Getting ready
- •Technical requirements
- •Exploring diagnostics options
- •Getting ready
- •Managing a diagnostics session
- •Getting ready
- •Designing a report with diagnostics results
- •Getting ready
- •There's more…
- •Using Diagnose as a Power Query step
- •Getting ready
- •Other Books You May Enjoy
- •Index

124 Reshaping Your Data
By leveraging these options, it is easy to simplify data structure and build aggregation tables ready to be used for the next steps.
Unpivoting and pivoting columns
You may need to change how data is displayed and turn a selected row into a column or vice versa. You may also need to create a new matrix view that can be easily used while developing reporting and custom views on data. In this recipe, we will see how to leverage pivot and unpivot features that allow us to respectively turn rows to columns or to transform columns into rows.
Getting ready
For this recipe, you need to have Power BI Desktop running on your machine. You need to download the following file in a local folder:
• FactInternetSales CSV file
In this example, we will refer to the C:\Data folder.
How to do it
Once you open your Power BI Desktop application, you are ready to perform the following steps:
1.Click on Get Data and select the Text/CSV connector.
2.Browse to your local folder where you downloaded the FactInternetSales CSV file and open it. A window with a preview of the data will pop up; click on
Transform Data.
3.Click on Choose Columns to restrict the number of columns we are working with:
Figure 4.27 – Choose Columns

Unpivoting and pivoting columns 125
4.Select the ProductKey, TotalProductCost, SalesAmount, and TaxAmt columns and click on OK.
5.Select the TotalProductCost, SalesAmount, and TaxAmt columns by pressing Ctrl on your keyboard and clicking on the columns, browse to the
Transform tab, and click on Unpivot Columns and then on Unpivot Only Selected Columns:
Figure 4.28 – Unpivot Columns
6.You can see that two columns were produced:
a)Attribute: A column with information contained before in headers
b)Value: Values that were under the previous column headers
In the following screenshot, you can see the two new added columns and the steps applied:
Figure 4.29 – Unpivoted values

126 Reshaping Your Data
We also have the possibility to perform the opposite. We can transform columns into rows as in the following example:
1.Remove the Unpivoted Only Selected Columns step from the APPLIED STEPS pane:
Figure 4.30 – Remove step
2.Double-click on Removed Other Columns and select the ProductKey,
SalesAmount, and OrderDate columns:
Figure 4.31 – Column selection

Unpivoting and pivoting columns 127
3. Change type to Date for the OrderDate column:
Figure 4.32 – Change data type
4. Select the OrderDate column, then click on Pivot Column:
Figure 4.33 – Pivot OrderDate table

128Reshaping Your Data
5.The Pivot Column window will pop up. Set Values Column as SalesAmount and
Aggregated Value Function as Sum, and click on OK:
Figure 4.34 – Pivot Column window
6.You can observe how the values from the previously selected column are now column headers and the SalesAmount column values are now summed according to a by date logic:
Figure 4.35 – Pivoted values