Blog
Formula - Transposing/converting rows to columns- an almost impossible task?
September 23. 2021
A common requirement is to transpose records in Power Apps - in other words, to convert rows to columns or columns to rows. This post discusses this topic and provides an example of how to carry out this task.
A long-standing limitation with Power Apps is that it's very difficult to convert rows to columns, or columns to rows. Part of the problem is that there's no way to specify column names dynamically when we build output collections or tables. Therefore, the techniques available to carry out this task are always convoluted and often rely on hardcoding column and row names.
Example scenario - A use case scenario of transposing data
To give a typical use case scenario, let's take the example of the following sales data.
The formula beneath builds the collection (called SalesByRegion) for this example.
ClearCollect(SalesByRegion,
{Quarter:"Q1", Europe:42804, Asia:16774 ,NorthAmerica:24094},
{Quarter:"Q2", Europe:32987, Asia:24214 ,NorthAmerica:20873},
{Quarter:"Q3", Europe:38485, Asia:28356 ,NorthAmerica:30689},
{Quarter:"Q4", Europe:44567, Asia:30763 ,NorthAmerica:34456}
)
The example requirement is to transpose this data like so
How to transpose data based on hardcoded column/row names
For this example, each row in the source table specifies the sales figures for a quarter. The columns in the source table correspond to the regions.
The target output requires us to produce a table with the column names Q1,Q2,Q3, and Q4. Since it's not possible to generate these column names dynamically, a possible solution is to construct formula that carries out the transposition, based on hard-coded values.
The formula beneath highlights the formula to carry out the task.
ForAll(
["Asia", "Europe", "NorthAmerica"] As Regions,
{
Region: Regions[@Value],
Q1: With({Q1Row:LookUp(SalesByRegion,Quarter="Q1")},Switch(Regions[@Value],
"Asia", Q1Row.Asia,
"Europe", Q1Row.Europe,
"NorthAmerica", Q1Row.NorthAmerica
)
),
Q2: With({Q2Row:LookUp(SalesByRegion,Quarter="Q2")},Switch(Regions[@Value],
"Asia", Q2Row.Asia,
"Europe", Q2Row.Europe,
"NorthAmerica", Q2Row.NorthAmerica
)
),
Q3: With({Q3Row:LookUp(SalesByRegion,Quarter="Q3")},Switch(Regions[@Value],
"Asia", Q3Row.Asia,
"Europe", Q3Row.Europe,
"NorthAmerica", Q3Row.NorthAmerica
)
),
Q4: With({Q4Row:LookUp(SalesByRegion,Quarter="Q4")},Switch(Regions[@Value],
"Asia", Q4Row.Asia,
"Europe", Q4Row.Europe,
"NorthAmerica", Q4Row.NorthAmerica
)
)
}
)
This produces an output table, and we apply this directly to a data control, or we could collect the results to a collection by calling the ClearCollect or Collect functions.
My Proposal - Power Apps Transpose Function
In many cases, the rows that we want to transpose into columns are unknown or can vary. Because of this, building formula with hardcoded values is not a viable solution. The only available options are to carry out the transposition outside of Power Apps, or to build complex formula based on string manipulation.
Therefore, I propose the addition of a Transpose function in Power Apps. The aim of this function is to mimic the Transpose function in Excel, and the usage would look like this:
Transpose(SalesByRegion)
The example above would return an output table with column names that derive from data.
If you would like to see this feature added to Power Apps, please vote for my idea here.
Conclusion
Transposing rows and columns in Power Apps is a difficult task. It's possible to transpose data with hardcoded column/row names and this post demonstrated a formula to carry out this task. If Power Apps provided a transpose function, this task would be much easier, and you can vote for this idea through the link in this post.
- Categories:
- data
Previous
Related posts
- Data - How to find the common rows from 3 or more collections
- Data - How to show the distinct rows from 2 data sources or collections
- Data - How to implement circular rotational date sorting
- Bug - What to do when the data section of the Power Apps Maker portal doesn't work
- Data - Combine columns from separate tables into a single table
- Data - How to rename field names in a record
- Data - How to hide duplicate rows in a gallery / show distinct multiple columns in a gallery
- Data - Retrieving news/forum/blog articles with RSS
- Data - How to sort by partial numbers in a text field
- Data - How to return the last record from a table
- Data - How to create bulk test/dummy records with random values
- Data - 3 things you should know before using the MySQL or PostgreSQL connectors
- Data - A walkthrough of how to migrate the data source of an app from Excel to Sharepoint
- Data - How to enforce unique values (or prevent duplicate values) in one or more columns
- Data - How much mobile data does Power Apps consume? What ways can we minimise this?
- Data - How to save and retrieve Google calendar entries
- Data - How to save and retrieve Google contacts
- SQL - Caution! This is how users can hack shared SQL connections
- SharePoint – 2 Mistakes to avoid when importing Excel data
- SQL - Don't let this DateTime bug catch you out!
- Settings - What's the purpose of the "Explicit Column Selection" Setting?
- SQL Server for Beginners Part 3 - Installing On-Premises Gateway
- SQL Server for Beginners Part 2 - Installing Management Studio
- SQL Server for Beginners Part 1 - Installing SQL Server
- Searching data–What you need to know about case sensitivity
- Images - How to create images that can change depending on data
- Excel - Reasons NOT to use Excel as a data source
- SharePoint - What you need to know about Filtering Data
- Formulas - Generating Row Numbers