cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mickell2030
Level: Powered On

Syntax for joining tables

I am having a heck of a time trying to understand the syntax of powerapps.

 

I have a background in SQL and I am very comfortable with it. This is not SQL :(.

 

I have an SQL database. My SQL database was built correctly, with the min amount of repeating data. It seems like Powerapps wants me to have 1 giant table with everything on it instead of multiple tables that are related and joined.

 

Here is what I need to understand:

 

Example - I have 3 tables with information I need from each in a gallery.

 

Table 1 - UPC_Details

Columns (UID) UPC_ID, Brand_ID, Cost, Created_Date

 

Table 2 - Brand_ID

Coulmns (need to join to UPC_Details Brand_ID) (UID) Brand_ID, Brand_Name, Company_Contact_ID...

 

Table 3 - UPC_received

Columns (UID) UPC_Received_ID, (need to join to table UPC_Details so users know what item they are looking at when the UPC is scanned) UPC_ID, Cases_received, Date_received...

 

 

I am sure you can see where this is going.

I need my users to be able to see specific data that I normally would use a join to reference. I do not know if I should be building a collection onvisble, or something else entire. Nor do I undertand the syntax Powerapps uses. I am extremely dissapointed with the resources for Powerapps, as I do not feel these questions are well answered and I am sure I am not the only user with this problem.

 

Thank you in advance to anyone willing to help me out with this.

1 ACCEPTED SOLUTION

Accepted Solutions
Power Apps Staff CarlosFigueira
Power Apps Staff

Re: Syntax for joining tables

You can use the functions AddColumns and LookUp to perform joins in PowerApps. Taking the example from W3Schools, where we have two tables:

Orders

OrderID  CustomerID  OrderDate
10308     2          1996-09-18
10309    37          1996-09-19
10310    77          1996-09-20

Customers

CustomerID  CustomerName              ContactName      Country
1           Alfreds Futterkiste	      Maria Anders     Germany
2           Ana Trujillo Emparedados  Ana Trujillo     Mexico
3           Antonio Moreno Taquería   Antonio Moreno   Mexico

To generate the result of the JOIN listed in that page:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID

This would be a similar expression in PowerApps:

 

ShowColumns(
    AddColumns(
        Orders,
        "CustomerName",
        LookUp(Customers, CustomerId = Orders[@CustomerId], CustomerName)),
    "OrderId", "CustomerName", "OrderDate")

You can also join the entire customer record, instead of only choosing a single column from that table. For example, if you have this expression in a button's OnSelect property:

ClearCollect(myCollection,
    AddColumns(
        Orders,
        "Customer",
        LookUp(Customers, CustomerId = Orders[@CustomerId])))

Then you can access the name of customer for the first order using:

First(myCollection).Customer.CustomerName

Or if you set the 'myCollection' to the Items property of a gallery, you can have a label within the gallery with the Text property set to

ThisItem.Customer.CustomerName

And it would show the customer name for that row in the (augmented) orders table.

 

Granted, I agree that a SQL join with all other niceties (grouping, native filtering, etc.) would be nice to have. There is an existing idea to support SQL queries (instead of simple tables) directly in the PowerApps Ideas board, please consider voting on it!

View solution in original post

12 REPLIES 12
Power Apps Staff CarlosFigueira
Power Apps Staff

Re: Syntax for joining tables

You can use the functions AddColumns and LookUp to perform joins in PowerApps. Taking the example from W3Schools, where we have two tables:

Orders

OrderID  CustomerID  OrderDate
10308     2          1996-09-18
10309    37          1996-09-19
10310    77          1996-09-20

Customers

CustomerID  CustomerName              ContactName      Country
1           Alfreds Futterkiste	      Maria Anders     Germany
2           Ana Trujillo Emparedados  Ana Trujillo     Mexico
3           Antonio Moreno Taquería   Antonio Moreno   Mexico

To generate the result of the JOIN listed in that page:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID

This would be a similar expression in PowerApps:

 

ShowColumns(
    AddColumns(
        Orders,
        "CustomerName",
        LookUp(Customers, CustomerId = Orders[@CustomerId], CustomerName)),
    "OrderId", "CustomerName", "OrderDate")

You can also join the entire customer record, instead of only choosing a single column from that table. For example, if you have this expression in a button's OnSelect property:

ClearCollect(myCollection,
    AddColumns(
        Orders,
        "Customer",
        LookUp(Customers, CustomerId = Orders[@CustomerId])))

Then you can access the name of customer for the first order using:

First(myCollection).Customer.CustomerName

Or if you set the 'myCollection' to the Items property of a gallery, you can have a label within the gallery with the Text property set to

ThisItem.Customer.CustomerName

And it would show the customer name for that row in the (augmented) orders table.

 

Granted, I agree that a SQL join with all other niceties (grouping, native filtering, etc.) would be nice to have. There is an existing idea to support SQL queries (instead of simple tables) directly in the PowerApps Ideas board, please consider voting on it!

View solution in original post

Meneghino
Level 10

Re: Syntax for joining tables

Hi @Mickell2030

Fully sympathise, it takes a few days to understand how PowerApps works, but then it will all be clear.

@CarlosFigueirais right in using LookUp, and another key to joining tables is the 'in' operator.

Please see this post of mine for example, just ignore the intro and go to the PowerApps expressions:

https://baizini-it.com/blog/index.php/2017/10/10/powerapps-101-many-to-many-relationships-between-ta...

Other posts on my blog may also be of help, for examplein improving performance of lookups.

Comments/questions welcome.

Mickell2030
Level: Powered On

Re: Syntax for joining tables

thank you. You took the time to really detail it out and I get it. I want to love Powerapps because so many other things are simpler from a programming prospective. I sure do hope this will become a bit cleaner with time and that SQL can become integrated language. Until then, your explanation was the best I could have hoped for.

Delid4ve
Level 10

Re: Syntax for joining tables

The nightmare starts when you need to use the 'in' operator and join 3 tables on many to many relationships. I do have solutions if you get really stuck.
MigChatter
Level: Powered On

Re: Syntax for joining tables

Hi

 

 

 

 

 

 

Delid4ve
Level 10

Re: Syntax for joining tables

Collect(List1Collection,AddColumns(List1,"Group",First(Filter(List2,SalesPerson=SalesPerson)).Group)

This will put it into a collection.

Havnt tested but you may need to use @ before 2nd salesperson or List1.SalesPerson, im not on the system at present and its been a while.

 

if you want to do it live within a gallery for instance you can set the gallery items to List1, then have a text box that just does: First(Filter(List2,SalesPerson=ThisItem.SalesPerson)).Group within each gallery item

 

I find First(Filter)) works faster than Lookup() but you can also use: Lookup(List2,SalesPerson=ThisItem.SalesPerson,Group)

 

 

MigChatter
Level: Powered On

Re: Syntax for joining tables

Thank you for the response.

I tried with the above but still i have no luck.

 

AlesonITC
Level: Powered On

Re: Syntax for joining tables

Hi all.

 

I've got 2 joined tables with that syntax:

 

ShowColumns(AddColumns('[SIE].[DatosGenerales]';"Denominacion";LookUp('[Dim].[DimSIE]'; DimSIEId = '[SIE].[DatosGenerales]'[@DimSIEId]; Denominacion));"Denominacion";"DimSIEId";"ModificadosMesActual";"PresupuestoInicialMesActual";"TrabajosATercerosMesActual";"RevisionDePreciosMesActual";"ComplementariosMesActual";"OtrosMesActual";"PresupuestoVigenteMesActual";"Mes")

 

It's just a table SIE.DatosGenerales and the column "Denominacion from DIM.DimSIE,but I want tomake a "Where" there to show only the rows I want. How can I do it?

 

Hope it can helps you.

marcoselias
Level: Powered On

Re: Syntax for joining tables

LookUp only returns the first record that is matched. What if there are multiple records to match? I'm guessing a Filter would do the trick. Is that right or is there a more efficient way?

Helpful resources

Announcements
New Ranks and Rank Icons in April

'New Ranks and Rank Icons in April

Read the announcement for more information!

Better Together’ Contest Finalists Announced!

'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

thirdimage

Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (9,800)