Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Power Query Cookbook Use effective and powerful queries in Power BI Desktop and Dataflows to prepare and transform your data (Janicijevic, Andrea) (z-lib.org).pdf
Скачиваний:
153
Добавлен:
14.08.2022
Размер:
25.9 Mб
Скачать

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