Bài viết này hướng dẫn các bạn cách pivot dữ liệu sử dụng các công cụ Tableau Prep Builder, Power Query and Excel. Từ đó, có thể so sánh các tính năng rõ ràng hơn.
This post is to introduce how to pivot data using Tableau Prep Builder, Power Query and Excel. Then we can make a comparasion between them more obviously.
Cùng pivot dữ liệu thô như hình bên dưới sang một dạng khác dễ nhìn hơn nhé:
Let’s pivot the below data to another pattern which makes us easily to observe:

1. Tableau Prep Builder
1.1. Connect to data (Microsoft Excel)

1.2. Process data
Click vào dấu + bên phải ký hiệu Sheet 1 ➝ Pivot
Click on the plus + sign at the right of Sheet 1 ➝ Pivot


Xuất hiện bước Pivot 1.
Pivot 1 step appears.
1.3. Pivot data

Bước 1: Thay Pivoted Fields từ ‘Columns to Rows’ sang ‘Rows to Columns’
Step 1: Change Pivoted Fields from ‘Columns to Rows’ to ‘Rows to Columns‘

Bước 2: Kéo Product vào Pivoted Fields
Step 2: Drag and drop Product column to Pivot Fields
Bước 3: Kéo Sale vào Field to aggreagate for new columns
Step 3: Drag and drop Sale to Field to aggregate for new columns.

Xem thành quả:
Your result is at the right pane:

Thay đổi vị trí của cột Date bằng cách kéo thả cột Date ra trước cột A.
Change the order of Data column by dragging it and dropping it at the left of column A.

Xuất dữ liệu thành file nếu cần thiết:
Export pivoted data if needed:

Chọn định dạng file muốn xuất:
Choose file extension of your exported file:

2. Power Query
2.1. Import data to Power Query Editor

2.2. Process data
Bước 1: Chọn hai cột Product và Sale
Step 1: Select both Product and Sale columns

Bước 2: Chọn Pivot trên tab Transform và xem kết quả
Step 2: Sleect Pivot on Transfrom tab and review the result

Hộp thoại Pivot Column hiện lên, chọn cột muốn hiển thị giá trị trong mỗi cột mà tên cột được lấy từ các giá trị trong cột Product. Dùng Sum hoặc các phép tính thống kê khác theo nhu cầu.
Pivot Colun dialogue box appears, please select a column what you want to display in each column created unique values in Product column. Use Sum function or other formulas as you want.

Xem thành quả:
Your result as:

3. Excel
3.1. Import data to Pivot Table

3.2. Custom Pivot Table
Kéo Date vào Rows, Product vào Columns và Sale vào Values.
Drag Date into Rows, Product into Columns and Sale into Values.

4. Python Pandas
4.1. Import Pandas and data

4.2. Pivot data
Sử dụng chức năng pivot từ thư viện pandas. Cấu trúc: pandas.pivot_table
Use pivot functions from pandas library. Syntax: pandas.pivot_table

Xuất dữ liệu thành file nếu cần thiết, sử dụng chức năng xuất dữ liệu thành file từ thư viện pandas. Cấu trúc: pandas.to_excel
Export pivoted data if needed, use exporting file function from pandas library. Syntax: pandas.to_excel

Trên đây mình đã trình bày cho các bạn các cách pivot dữ liệu mà một Data Analyst hay dùng. Mong rằng bài viết sẽ giúp ích cho các bạn!