Dynamic Table Headers in Power Query (SAP ByDesign, Odata)

Another good example of how awesome is Power Query. And again I reference to one of well-known bloggers, Ken Puls’s post describes situation when we have pivoted data and need to unpivot them then rename columns. Fortunalety, in this example not so many columns used and unpivoting + join “rename table” is a solution.
However, in common case when we have table with 20 columns and 100 000 rows (or 2 million) unpivoting will be a very bad decision.
I faced with this challenge when I started to work with Odata protocol in SAP Business ByDesign.

Using Odata we have to provide string with list of fields ID in $select, for example query of Account Contact Data:


or Invoice Volume query


In json response we have no names of fields, but work with IDs quite inconvenient. In case of extension fields simply impossible. Who can remember what stored in field Ts1ANs3C22103A3ABA41?
First idea that come in mind – rename all columns using $metadata of corresponding report.
Let’s consider formalized task.

Rename Columns Challenge

For column renaming opeartion in tables we have only

Table.RenameColumns(table as table, renames as list, optional missingField as nullable number) as table

Example in documentation tell us

Table.RenameColumns(Table.FromRecords({[A=1, B=2]}), {{“A”, “B”},{“B”, “A”}})

so we need to get list of pairs “A->New_A_Name” for second argument.
Method of trials and errors brought me to formula

Table.Transpose(table as table, optional columns as any) as table

Which applied to “Rename Table” (Table2) gives table

Assassin Barbarian Crusader

then such table can be transformed by

Table.ToColumns(table as table) as list

// Returns a list of nested lists each representing a column of values in the input table.

Table1 – initial table with data
Table2 – table containing list of IDs and Names

rename_list = Table.ToColumns(Table.Transpose(Table2)),
result = Table.RenameColumns(Table1, rename_list, MissingField.Ignore)

MissingField.Ignore is needed to avoid problems when somehow unexcpected column appeared in response.

M code to get IDs and Names of SAP ByDesign report. Just provide TenantId and ReportId to following function

names =
Source = OData.Feed(“https://my” & TenantId & “.sapbydesign.com/sap/byd/odata/cc_home_analytics.svc/$metadata?entityset=RP” & ReportId & “QueryResults”),
Content = Xml.Tables(Source),
DataServices = Content{0}[DataServices],
#”http://schemas microsoft com/ado/2008/09/edm” = DataServices{0}[#”http://schemas.microsoft.com/ado/2008/09/edm”%5D,
#”Expand Schema” = Table.ExpandTableColumn(#”http://schemas microsoft com/ado/2008/09/edm”, “Schema”, {“EntityType”}, {“Schema.EntityType”}),
#”Expand Schema.EntityType2″ = Table.ExpandTableColumn(#”Expand Schema”, “Schema.EntityType”, {“Property”}, {“Property”}),
#”Expand Property” = Table.ExpandTableColumn(#”Expand Schema.EntityType2″, “Property”, {“Attribute:Name”, “http://www.sap.com/Protocols/SAPData”}, {“Attribute:Name”, “http://www.sap.com/Protocols/SAPData”}),
#”Expand http://www.sap.com/Protocols/SAPData” = Table.ExpandTableColumn(#”Expand Property”, “http://www.sap.com/Protocols/SAPData”, {“Attribute:label”}, {“Attribute:label”}),
#”Renamed Columns” = Table.RenameColumns(#”Expand http://www.sap.com/Protocols/SAPData”,{{“Attribute:Name”, “Id”}, {“Attribute:label”, “Name”}}),
res = Table.SelectRows(#”Renamed Columns”, each ([Id] “ID” and [Id] “TotaledProperties”))

8 thoughts on “Dynamic Table Headers in Power Query (SAP ByDesign, Odata)

  1. Ivan Bondarenko 2015-10-07 / 23:09

    In Bonus part I would change “OData.Feed” on “Web.Contents”, as I noticed that Web.Contents works faster.

  2. Emanuel 2016-08-05 / 19:48

    Hey thanks a lot for that approach. I have been looking for a way to do this for a while. I am however running into some problems and I was wondering if you knew how to solve this. When I try your approach I always get this error message:

    Expression.Error: We expected a RenameOperations value.

    • Ivan Bondarenko 2016-08-05 / 20:02

      Hi Emanuel, thanks for contact me and for usage my advice. Could you please share piece of your code and line where you get error? I don’t remember that I saw such issue. Usually in Power Query even small mistake can lead to error, developers have to be very accurate.
      And out of curiosity, do you also use SAP ByD?
      Thanks, Ivan

  3. Jeff 2017-03-13 / 16:11

    Ivan Thank you. This helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s