Quantcast
Channel: SCN : Document List - SAP HANA and In-Memory Computing
Viewing all articles
Browse latest Browse all 1183

Modelling: Column to Row Transpose using Matrix in HANA

$
0
0

Contributors:

Abani Pattanayak, HANA COE (abani.pattanayak@sap.com)

Tony Cheung, Business Analytics Services (tony.cheung@sap.com)

 

 

Background:

 

In almost every project, some form of data transformation is required. Most of these transformations are some combination of Aggregation, Projection, Union or Join steps and these transformation can be easily done in the HANA models.

 

Where possible, It may be a good idea to push the complex transformation to the data acquisition layer using ETL tools like data services.

In certain scenarios, we may need additional data transformation on top of transformed data (say. you want to transform the output of an Analytic or Calculation view), in this scenario ETL tool can't be used.

 

 

Problem Statement:

 

There are two types of transformation

  • Row to Column Transpose
  • Column to Row Transpose 

 

1. Row to Column Transpose: This can be easily done using Restricted Measure (available in Analytic View) and Calculated Measures (available in both Analytic View and Calculation View)

 

2. Column to Row Transpose:


Let's start with an example. The following is output of Analytic View (AN_DATA)

DATA.PNG

 

We'd like to transform the dataset (from column to row) as shown below

 

ROW_TO_COLUMN.PNG

 

Solution Options:

 

We'll use matrix transpose to achieve this result. Refer to the Wikipedia link for detail of this concept Transpose - Wikipedia, the free encyclopedia and Elementary matrix - Wikipedia, the free encyclopedia

 

Matrix.PNG

 

1. Create Matrix table DATA_TRANSFORM as below. 3 columns for 3 measures in our source data and an extra column "dummy".

DATA_TRANSFORM.PNG

2. Insert the dataset as below to DATA_TRANSFORM table

 

DATA_TRANSFORM_data.PNG

3. Build the calculation view as below.

 

CA_VIEW.PNG

  • Create a Projection (proj_DATA) on the Analytic View (AN_DATA). Create a calculated column "DUMMY" with value 1
  • Create a Projection (proj_MATRIX) on the DATA_TRANSFORM table
  • Join proj_DATA and proj_MATRIX on the DUMMY field. Create two calculated column AMT_TYPE and AMT as shown below
  • Expose the DOCID, COUNTRY, AMT_TYPE and AMT in the Aggregation node.

 

Calc_1.PNGCalc_2.PNG

 

Result:

 

Now execute the following Query for the desired output

 

Output.PNG


Viewing all articles
Browse latest Browse all 1183

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>