Flattening SAP Dimension Hierarchy using a Table Function

In this blog, we will understand the approach of utilizing SAP Hierarchies in Visualization tools like Power BI, Tableau, and so on. To make use of the SAP Dimension Hierarchies, maintained in a parent-child structure for Visualizations, we need to flatten these hierarchies. A common parent-child hierarchy is the Country Dimension Hierarchy.

SAP BPC Country Hierarchy:

ALLCOUNTRY : All Countries                                       

                AMERICAS : Americas                    

                                LTAM : Latin America

                                                CU : Cuba

PA : Panama

NRAM : North America 

                                                CA : Canada

                                                US : United States

                                APAC : Asia Pacific           

                                GRCH : Greater China

                                SEAO : Southeast Asia & Oceania

                                                BT : Bhutan

                                                FJ : Fiji  

                EMEAI : Europe, Middle East, Africa & India

                                CEUR : Central & Eastern Europe

                                                AL : Albania

                                                GE : Georgia      

INDI : India

MEAT : Middle East, Africa & Turkey

                                                AF : Afghanistan

                                                BH : Bahrain GB : Great Britain            

These will be maintained in a hierarchy table as (all columns are not shown):

HIEIDNODEIDIOBJNMNODENAMEPARENTID
TKW5HD9UWAGP9MMEL8JBCL6W51/CPMB/L5DRD35ALLCOUNTRY0
TKW5HD9UWAGP9MMEL8JBCL6W521/CPMB/L5DRD36AMERICAS1
TKW5HD9UWAGP9MMEL8JBCL6W522/CPMB/L5DRD37APAC1
TKW5HD9UWAGP9MMEL8JBCL6W523/CPMB/L5DRD38EMEAI1
TKW5HD9UWAGP9MMEL8JBCL6W527/CPMB/L5DRD39LTAM21
TKW5HD9UWAGP9MMEL8JBCL6W528/CPMB/L5DRD40NRAM21
TKW5HD9UWAGP9MMEL8JBCL6W579/CPMB/L5DRD41CU27
TKW5HD9UWAGP9MMEL8JBCL6W567/CPMB/L5DRD42PA27
TKW5HD9UWAGP9MMEL8JBCL6W5107/CPMB/L5DRD43CA28
TKW5HD9UWAGP9MMEL8JBCL6W5108/CPMB/L5DRD44US28
TKW5HD9UWAGP9MMEL8JBCL6W529/CPMB/L5DRD45GRCH22
TKW5HD9UWAGP9MMEL8JBCL6W531/CPMB/L5DRD46SEAO22
TKW5HD9UWAGP9MMEL8JBCL6W5128/CPMB/L5DRD49BT31
TKW5HD9UWAGP9MMEL8JBCL6W5133/CPMB/L5DRD50FJ31
TKW5HD9UWAGP9MMEL8JBCL6W532/CPMB/L5DRD51INDI23
TKW5HD9UWAGP9MMEL8JBCL6W534/CPMB/L5DRD52CEUR23
TKW5HD9UWAGP9MMEL8JBCL6W535/CPMB/L5DRD53MEAT23
TKW5HD9UWAGP9MMEL8JBCL6W5176/CPMB/L5DRD55AL34
TKW5HD9UWAGP9MMEL8JBCL6W5184/CPMB/L5DRD56GE34
TKW5HD9UWAGP9MMEL8JBCL6W5196/CPMB/L5DRD58AF35
TKW5HD9UWAGP9MMEL8JBCL6W5199/CPMB/L5DRD59BH35
TKW5HD9UWAGP9MMEL8JBCL6W5276/CPMB/L5DRD60GB38

To flatten the SAP BPC Country Hierarchy using table function

  • Step 1 – The first step is to determine the maximum number of levels our hierarchy can contain. Here, we will assume that the Country Hierarchy will go up to four levels. Define a Table Function that will return the flattened hierarchy as follows:

FUNCTION “<System>”.”<Package>::<Table Function>” ()

   RETURNS TABLE (NODE1 VARCHAR(32),

                   NODE2 VARCHAR(32),

                   NODE3 VARCHAR(32),

                   BASELEVEL VARCHAR(32))

   LANGUAGE SQLSCRIPT

       SQL SECURITY INVOKER AS

BEGIN

——-code explained in subsequent steps go here——-

END;

Here, NODE1, NODE2, and NODE3 will contain the parents at hierarchy levels 1,2, and 3 respectively . BASELEVEL will contain the leaf nodes.

  • Step 2 – Select the following fields from the hierarchy table restricting to the Hierarchy to flatten using HIEID:

lt_hier_table = SELECT

NODEID,

                        NODENAME,

                        PARENTID

FROM <Hierarchy Table>

WHERE OBJVERS = ‘A’ AND

              HIEID = <Hierarchy ID>;

  • Step 3 – Now, we need to link each node with its parent node:

lt_ini_hier =   SELECT

LV01.”NODENAME” AS NODE1,

       LV02.”NODENAME” AS NODE2,

       LV03.”NODENAME” AS NODE3,              

BASELEVEL.”NODENAME” AS BASELEVEL                     

FROM ( SELECT

              “NODEID”,

              “NODENAME”,

              “PARENTID”

       FROM :lt_hier_table

       WHERE “NODEID” NOT IN (SELECT DISTINCT “PARENTID”

                               FROM :lt_hier_table)) AS BASELEVEL

LEFT OUTER JOIN :lt_hier_table LV03

ON BASELEVEL.”PARENTID” = LV03.”NODEID”

LEFT JOIN :lt_hier_table LV02

ON LV03.”PARENTID” = LV02.”NODEID”

LEFT JOIN :lt_hier_table LV01

ON LV02.”PARENTID” = LV01.”NODEID”;

It_ini_hier looks like:

NODE1NODE2NODE3BASELEVEL
ALLCOUNTRYAMERICASLTAMCU
ALLCOUNTRYAMERICASLTAMPA
ALLCOUNTRYAMERICASNRAMCA
ALLCOUNTRYAMERICASNRAMUS
ALLCOUNTRYAPACGRCH
ALLCOUNTRYAPACSEAOBT
ALLCOUNTRYAPACSEAOFJ
ALLCOUNTRYEMEAICEURAL
ALLCOUNTRYEMEAICEURGE
ALLCOUNTRYEMEAIINDI
ALLCOUNTRYEMEAIMEATAF
ALLCOUNTRYEMEAIMEATBH
ALLCOUNTRYEMEAINEU

It is not very useful format. For instance, if we require a filter to show only Region: EMEAI, which column should we apply a filter on? So, we need to bring all occurrences of EMEAI in a single column. In other words, each node should exist in only one column.

  • Step 4 – If we shift all the nodes except base nodes towards the left to fill the blanks, we will obtain a format befitting our purpose.
NODE1NODE2NODE3BASELEVEL
ALLCOUNTRYAMERICASLTAMCU
ALLCOUNTRYAMERICASLTAMPA
ALLCOUNTRYAMERICASNRAMCA
ALLCOUNTRYAMERICASNRAMUS
ALLCOUNTRYAPACGRCH
ALLCOUNTRYAPACSEAOBT
ALLCOUNTRYAPACSEAOFJ
ALLCOUNTRYEMEAICEURAL
ALLCOUNTRYEMEAICEURGE
ALLCOUNTRYEMEAIINDI
ALLCOUNTRYEMEAIMEATAF
ALLCOUNTRYEMEAIMEATBH
ALLCOUNTRYEMEAINEU

Now, our filters will work alright. To shift the nodes to the left, we will use coalesce function:

lv_fin_hier =        SELECT

                            COALESCE(NODE1,

                                    NODE2,

                                    NODE3) AS NODE1,

                           CASE

                                  WHEN NODE1 IS NOT NULL

                                  THEN

                                  COALESCE(NODE2,

                                           NODE3)

WHEN NODE2 IS NOT NULL

                                  THEN NODE3

END AS NODE2,

CASE

                                  WHEN NODE1 IS NOT NULL AND

            NODE2 IS NOT NULL

                                  THEN NODE3

END AS NODE3,

BASELEVEL

FROM :lv_ini_hier;

RETURN :lv_fin_hier;

The table function can then be called in a calculation view to combine the hierarchy with final data which can then be used as a source for visualizations.

Conclusion:

It is a common requirement to bring in SAP Dimension Hierarchy to Power BI. While it is possible to fetch data directly from SAP BW/BPC to Power BI, using an SAP HANA Calculation View as an intermediate step is one of the best approaches. We have seen a tried and tested method of pulling in SAP BW/BPC hierarchy to Power BI, with SAP HANA as an intermediary.

However, this method comes with a few limitations:

  • As a table function requires a fixed return type, we can only incorporate a fixed number of hierarchy levels. It will not dynamically expand if new hierarchy levels are introduced.
  • As the number of levels in the hierarchy increases, so will the complexity of coalesce logic in Step 4.

References:

Table Functions in SAP HANA – Step by Step Guide: https://blogs.sap.com/2018/11/05/table-functions-in-sap-hana-step-by-step-guide/

Connect to SAP HANA Databases in Power BI Desktop: https://docs.microsoft.com/en-us/power-bi/connect-data/desktop-sap-hana

Appendix:

Below is a generic code that can be used for the transformation of a four leveled hierarchy.

FUNCTION “<System>”.”<Package>::<Table Function>” ()

   RETURNS TABLE (NODE1 VARCHAR(32),

                   NODE2 VARCHAR(32),

                   NODE3 VARCHAR(32),

                   BASELEVEL VARCHAR(32))

   LANGUAGE SQLSCRIPT

       SQL SECURITY INVOKER AS

BEGIN

lt_hier_table = SELECT

NODEID,

                            NODENAME,

                            PARENTID

FROM <Hierarchy Table>

WHERE OBJVERS = ‘A’ AND

                 HIEID = <Hierarchy ID>;

        lt_ini_hier =       SELECT

LV01.”NODENAME” AS NODE1,

       LV02.”NODENAME” AS NODE2,

       LV03.”NODENAME” AS NODE3,              

BASELEVEL.”NODENAME” AS BASELEVEL                     

FROM ( SELECT

              “NODEID”,

              “NODENAME”,

              “PARENTID”

       FROM :lt_hier_table

       WHERE “NODEID” NOT IN (SELECT DISTINCT “PARENTID”

                              FROM :lt_hier_table)) AS BASELEVEL

LEFT OUTER JOIN :lt_hier_table LV03

ON BASELEVEL.”PARENTID” = LV03.”NODEID”

LEFT JOIN :lt_hier_table LV02

ON LV03.”PARENTID” = LV02.”NODEID”

LEFT JOIN :lt_hier_table LV01

ON LV02.”PARENTID” = LV01.”NODEID”;

lv_fin_hier =       SELECT

                           COALESCE(NODE1,

                                    NODE2,

                                    NODE3) AS NODE1,

                           CASE

                                  WHEN NODE1 IS NOT NULL

                                  THEN

                                  COALESCE(NODE2,

                                           NODE3)

WHEN NODE2 IS NOT NULL

                                  THEN NODE3

END AS NODE2,

CASE

                                  WHEN NODE1 IS NOT NULL AND

            NODE2 IS NOT NULL

                                  THEN NODE3

END AS NODE3,

BASELEVEL

FROM :lv_ini_hier;

RETURN :lv_fin_hier;              

END;

“TekLink’s team exceeded Kellogg Latin America’s expectations with the implementation of Anaplan. Not only their diligence and technical mastery were evident, but also provided critical and out-of-the-box solutions to meet the project’s criteria and expand its scope.”
Francisco Ibarra
Francisco Ibarra

Sr. Manager, Kellogg Company

“TekLink provided worry free BEx to AO Migration by analyzing and converting our 500+ BEx workbooks to Analysis for Office while also increasing adoption by running power user workshops.”
Lakshmi Thota
Lakshmi Thota

Sr. Manager, Rust-Oleum Company

"TekLink has exceeded our expectations and I strongly endorse their capabilities and would happily recommend them to other customers/prospects”
Assoc. Director, IT
Assoc. Director, IT

CSM Bakery Solutions