1

I have a SAP BW cube and I can access it using power query with the "SAP Business Warehouse Message Server" Power BI connector,

The power M code is something like:

let
    Source = SapBusinessWarehouse.Cubes("My BW Ip", "00", "001", [Implementation="2.0"]),
    ZCOP = Source{[Name="ZCOP"]}[Data],
    #"ZCOP/ZQ_COP_2" = ZCOP{[Id="ZCOP/ZQ_COP_2"]}[Data],
    #"Added Items" = Cube.Transform(#"ZCOP/ZQ_COP_2",
        {
            {Cube.AddAndExpandDimensionColumn, "[0CALMONTH]", {"[0CALMONTH].[LEVEL01]"}, {"Año/Mes natural.Año/Mes natural Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0PLANT]", {"[0PLANT].[LEVEL01]"}, {"Centro.Centro Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0COSTDATE]", {"[0COSTDATE].[LEVEL01]"}, {"Fecha cálculo coste.Fecha cálculo coste Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0MATERIAL]", {"[0MATERIAL].[LEVEL01]"}, {"Material.Material Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0COMP_CODE]", {"[0COMP_CODE].[LEVEL01]"}, {"Sociedad.Sociedad Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0COSTSTATUS]", {"[0COSTSTATUS].[LEVEL01]"}, {"Status cálculo coste.Status cálculo coste Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0CURTYPE]", {"[0CURTYPE].[LEVEL01]"}, {"Tipo de moneda.Tipo de moneda Nivel 01"}},
            {Cube.AddAndExpandDimensionColumn, "[0VALUATION]", {"[0VALUATION].[LEVEL01]"}, {"Vista valoración.Vista valoración Nivel 01"}},
            {Cube.AddMeasureColumn, " Análisis Calidad", "[Measures].[00O2TS3ZGQX4VVAT463GW2O53]"},
            {Cube.AddMeasureColumn, " Costos Ind. Fabricación", "[Measures].[00O2TS3ZGQX4VVAT463GW2HTJ]"},
            {Cube.AddMeasureColumn, " Gastos Admin", "[Measures].[00O2TS3ZGQX4VVAT8TY9H77HZ]"},
            {Cube.AddMeasureColumn, " Mano de Obra Directa", "[Measures].[00O2TS3ZGQX4VVAT0KHEJIEUV]"},
            {Cube.AddMeasureColumn, "Materia Prima", "[Measures].[00O2TS3ZGQX4VVASYNHA84OTZ]"},
            {Cube.AddMeasureColumn, "Material de Empaque", "[Measures].[00O2TS3ZGQX4VVASYNHA84V5J]"},
            {Cube.AddMeasureColumn, "Subcontratación", "[Measures].[00O2TS3ZGQX4VVAT8TY9H716F]"}
        })
in
    #"Added Items"

And I'm trying to do the same in python with the following code:

from pyrfc import Connection
import pandas as pd

# Define connection parameters
conn_params = {
    'user': 'user',
    'passwd': 'pass',
    'ashost': 'My IP', 
    'sysnr': '00',
    'client': '001',
    'lang': 'EN'
}

# Establish connection
conn = Connection(**conn_params)

# Read data from SAP BW cube
cube_name = 'ZCOP/ZQ_COP_2'
result = conn.call('RFC_READ_TABLE', QUERY_TABLE=cube_name)

# Close connection
conn.close()

# Convert result to DataFrame
df = pd.DataFrame(result['DATA'])

# Extract column names from result
columns = [col['FIELDNAME'] for col in result['FIELDS']]

# Set DataFrame column names
df.columns = columns

# Perform transformations similar to Power Query
# Add dimension columns
df['Año/Mes natural.Año/Mes natural Nivel 01'] = df['[0CALMONTH].[LEVEL01]']
df['Centro.Centro Nivel 01'] = df['[0PLANT].[LEVEL01]']
df['Fecha cálculo coste.Fecha cálculo coste Nivel 01'] = df['[0COSTDATE].[LEVEL01]']
df['Material.Material Nivel 01'] = df['[0MATERIAL].[LEVEL01]']
df['Sociedad.Sociedad Nivel 01'] = df['[0COMP_CODE].[LEVEL01]']
df['Status cálculo coste.Status cálculo coste Nivel 01'] = df['[0COSTSTATUS].[LEVEL01]']
df['Tipo de moneda.Tipo de moneda Nivel 01'] = df['[0CURTYPE].[LEVEL01]']
df['Vista valoración.Vista valoración Nivel 01'] = df['[0VALUATION].[LEVEL01]']

# Add measure columns
df['Análisis Calidad'] = df['[Measures].[00O2TS3ZGQX4VVAT463GW2O53]']
df['Costos Ind. Fabricación'] = df['[Measures].[00O2TS3ZGQX4VVAT463GW2HTJ]']
df['Gastos Admin'] = df['[Measures].[00O2TS3ZGQX4VVAT8TY9H77HZ]']
df['Mano de Obra Directa'] = df['[Measures].[00O2TS3ZGQX4VVAT0KHEJIEUV]']
df['Materia Prima'] = df['[Measures].[00O2TS3ZGQX4VVASYNHA84OTZ]']
df['Material de Empaque'] = df['[Measures].[00O2TS3ZGQX4VVASYNHA84V5J]']
df['Subcontratación'] = df['[Measures].[00O2TS3ZGQX4VVAT8TY9H716F]']

# Drop unnecessary columns
df.drop(columns=['[0CALMONTH].[LEVEL01]', '[0PLANT].[LEVEL01]', '[0COSTDATE].[LEVEL01]', '[0MATERIAL].[LEVEL01]',
                  '[0COMP_CODE].[LEVEL01]', '[0COSTSTATUS].[LEVEL01]', '[0CURTYPE].[LEVEL01]', '[0VALUATION].[LEVEL01]',
                  '[Measures].[00O2TS3ZGQX4VVAT463GW2O53]', '[Measures].[00O2TS3ZGQX4VVAT463GW2HTJ]',
                  '[Measures].[00O2TS3ZGQX4VVAT8TY9H77HZ]', '[Measures].[00O2TS3ZGQX4VVAT0KHEJIEUV]',
                  '[Measures].[00O2TS3ZGQX4VVASYNHA84OTZ]', '[Measures].[00O2TS3ZGQX4VVASYNHA84V5J]',
                  '[Measures].[00O2TS3ZGQX4VVAT8TY9H716F]'], inplace=True)

# Display DataFrame
print(df)

But in this line:

result = conn.call('RFC_READ_TABLE', QUERY_TABLE=cube_name)

I'm getting this error message:

ABAPApplicationError: 5 (rc=5): key=TABLE_NOT_AVAILABLE, message=ID:SV Type:E Number:029 ZCOP/ZQ_COP_2 [MSG: class=SV, type=E, number=029, v1-4:=ZCOP/ZQ_COP_2;;;]

Any idea what could be happening?

Thanks in advance!

2
  • A cube is not a table. You'd better ask a question like how to extract a SAP BW cube as tables in a database as SQL (RFC is just a protocol, it's not what matters here). Commented Apr 25 at 17:34
  • Hi @SandraRossi, thank you for your answer. But how can Power BI to transform a cube into data in table form? There is no a way to do something similar in python? Commented Apr 29 at 17:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.