Cross Tab Report in Fabric Data Warehouse using PIVOT Operator

Welcome to Excel Jet Consult blog post. In this blog, I am going to walk you through how to create a cross-tab report in Fabric Warehouse using the Pivot Operator. Let’s get started.

Video

Pivot Operator

The PIVOT operator in SQL Server and Fabric Data Warehouse allows you to transform rows into columns and perform aggregations on your data. One of of the benefit of using the operator is that it improves the readability and clarity of the data by creating more focused views that display the relevant information in a matrix format. It also simplifies the reporting and analysis of the data by reducing the query complexity and the need for multiple joins or subqueries. In addition, it streamlines the ETL processes by enabling data transformation and summarization in a single step.

Cross Tab Report in Fabric

We want to focus on how to use the operator to perform cross-tab report in Fabric Data Warehouse. We’re going to use a table called data in the Data Warehouse and by writing a simple SELECT * FROM data, we returned all the records in the Data table as seen in the screenshot below

We want to write a SQL query that will display the Units by Year, Products (Speaker, Laptop and Camera) and Payment Type that equals to PayPal. The three products will be displayed as columns by the Year, as rows.

To do the above, execute the query:

SELECT * FROM
(SELECT Year, Product, Units
FROM data
WHERE PaymentType =’PayPal’) AS SelectedColumns
PIVOT(SUM(Units) FOR Product IN ([Speaker],[Laptop],[Camera])) AS PVT
ORDER BY Year DESC
Query Explanation
Inner Query
SELECT Year, Product, Units
FROM data
WHERE PaymentType =’PayPal’) AS SelectedColumns
The above query selects the columns Year, Product, and Units from the data table. IN addition, it filters the results to include only those rows where the PaymentType is “PayPal”. The query output of the inner query is given the alias SelectedColumns.
Outer Query
SELECT * FROM SelectedColumns
The outer query selects all columns (*) from the result set of the inner query which is referred to as SelectedColumns
Pivot Operator
PIVOT
(
SUM(Units) FOR Product IN ([Speaker], [Laptop], [Camera])
) AS PVT
The PIVOT operation is used to transform the data by taking the columns Product from the inner query and pivots them to become new columns in the result set. The SUM(Units) function is applied to aggregate the values in the Units column for each combination of Year and Product. The FOR Product IN ([Speaker], [Laptop], [Camera]) clause specifies the distinct values of Product that will become the new columns.
Sorting Result
ORDER BY Year DESC
The final calculation set is sorted in descending order based on the Year column.
Final Result

The screenshot below, the result is presented in a pivoted format, where each product category has its own column, and the values represent the total units sold in each year.

Posted in Blog.

Leave a Reply

Your email address will not be published. Required fields are marked *