Blog
Data - Combine columns from separate tables into a single table
October 13. 2022
This post describes the formula to merge or combine separate columns from different tables/collections into a single table.
When working with data, there are occasions where it's necessary to combine or merge columns from separate lists, tables, or collections into a single structure. The columns in the source tables would contain the same number of rows and the requirement would be to match on ordinal or row index.
The image beneath illustrates this requirement. Here, there are two separate lists of region codes and region descriptions. The aim is to create a combined collection of codes and descriptions.
The image beneath illustrates this requirement. Here, there are two separate lists of region codes and region descriptions. The aim is to create a combined collection of codes and descriptions.
Where we often see this requirement is in cases where we retrieve data from web services. There are often times where the return data is split into separate tables. At this point, there will then be the subsequent need to merge the columns into a single structure.
Demonstration - Creating 2 separate collections
To demonstrate this scenario, the formula beneath creates two collections with an identical number of rows. The first collection contains region codes and the second collection includes region descriptions.ClearCollect(
colRegionCodes,
{'RegionCode':"E12000002"},
{'RegionCode':"E12000005"},
{'RegionCode':"E12000006"},
{'RegionCode':"S92000003"},
{'RegionCode':"E12000007"},
{'RegionCode':"E12000001"},
{'RegionCode':"N92000002"},
{'RegionCode':"W92000004"},
{'RegionCode':"E12000003"},
{'RegionCode':"E12000004"},
{'RegionCode':"E12000009"},
{'RegionCode':"E12000008"}
)
ClearCollect(
colRegionDescriptions,
{'RegionDesc':"North West"},
{'RegionDesc':"West Midlands"},
{'RegionDesc':"East of England"},
{'RegionDesc':"Scotland"},
{'RegionDesc':"London"},
{'RegionDesc':"North East"},
{'RegionDesc':"Northern Ireland"},
{'RegionDesc':"Wales"},
{'RegionDesc':"Yorkshire and The Humber"},
{'RegionDesc':"East Midlands"},
{'RegionDesc':"South West"},
{'RegionDesc':"South East"}
)
How to merge columns from separate collections into a single table
The formula below shows how to combine the columns from the separate collections into a single table.
ForAll(
Sequence(CountRows(colRegionCodes)),
{
RegionCode:Index(colRegionCodes, Value).RegionCode,
RegionDesc:Index(colRegionDescriptions, Value).RegionDesc
}
)
This formula creates a sequence of numbers based on the number of rows in colRegionCodes. The call to ForAll iterates over this sequence and for each number in the sequence, it retrieves the corresponding row from each collection by calling the Index function. It then retrieves the RegionCode and RegionDesc from the referenced rows.
Here's an illustration of what we see when we set the Items property of a data table control to this formula.
Conclusion
This post demonstrated the formula to merge or combine separate columns from different tables/collections into a single table by creating a numeric sequence and merging by index number.
- Categories:
- data
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
- Formula - Transposing/converting rows to columns- an almost impossible task?
- 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