cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper I
Helper I

Join the same table

Hello,

 

I have one table friendship that has two columns: person_id_1  and person_id_2. 

The relation is unidirectional, so person A cound be a friend of person B even if person B is no friend of person A. 

Now I want to filter only "true friends" (where the friendship is bidirectional)  in my Powerapps gallery. 

In SQL I would do something like this: 

 

SELECT a.person_id_1, a.person_id_2

FROM friendship a, (SELECT person_id_2, person_id_1 FROM friendship) b

WHERE a.person_id_1=b.person_id_2 and a.person_id_2=a.person_id_1;

 

I'm not sure how to do this in Powerapps. I think I need a "join" (or in Powerapps AddColumns) on the same table. 

Has anyone a idea how to do this? 

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Community Support
Community Support

Hi @sandra1 ,

Based on the needs that you mentioned, I think the ForAll function and Collect function could achieve your needs. I have made a test on my side, please consider take a try with the following workaround:

Add a button in your app, set the OnSelect property to following:

Clear(TrueFriends);
ForAll(
       RenameColumns('[dbo].[friendship]', "person_id_1", "personA", "person_id_2", "personB"),
       If(
          !IsBlank(LookUp('[dbo].[friendship]', person_id_1 = personB && person_id_2 = personA)),
          Collect(
                   TrueFriends, 
                   {
                       PersonA: person_id_1, 
                       PersonB: person_id_2
                   }
         )
       )
)

Then you could add a Data Table control within your app, set the Items property to following:

TrueFriends

then enable the PersonA field and PersonB field within the Data Table, after you press the button control, then you could see the true friends within your friendship table.

 

Please take a try with above solution, check if the issue is solved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Hi @sandra1 ,

Yeah, you are right. The ForAll function is not a delegable function in PowerApps currently. Based on the needs that you mentioned, if you want to achieve your needs using Delegable functions, I afraid that there is no way to achieve your needs.

 

As an alternative solution, if you want to improve your formula's performace in your app, I think the collection could achieve your needs. You could consider save your SQL Table records into a collection, then use the collection as data source in your app.

 

I have made a test on my side, please take a try with the following workaround:

Set the OnStart property of App to following:

ClearCollect(SQLTableRecords, '[dbo].[friendship]')

Add a button in your app, set the OnSelect property to following:

Clear(TrueFriends);
ForAll(
       RenameColumns(SQLTableRecords, "person_id_1", "personA", "person_id_2", "personB"),
       If(
          !IsBlank(LookUp('[dbo].[friendship]', person_id_1 = personB && person_id_2 = personA)),
          Collect(
                   TrueFriends, 
                   {
                       PersonA: person_id_1, 
                       PersonB: person_id_2
                   }
         )
       )
)

Please take a try with above solution, then re-load your app (fire the OnStart property of App), then check if the issue is improved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Dual Super User III
Dual Super User III

Hey @sandra1 

 

You can use the below expression as:

AddColumns(Filter(GroupBy(AddColumns(Friendship,"Columns",Concat(Sort([person_id_1,person_id_2],Value,Ascending),Value)),"Columns","Grouped"),CountRows(Grouped) = 2),"Person_1",Last(Grouped).person_id_1,"Person_2",Last(Grouped).person_id_2)

Here, a custom column "Columns" is created to check the same value for the Person1 and Person2 column. As part of this query, you can access Person1 name using ThisItem.Person_1 and Person 2 as ThisItem.Person_2.

 

Hope this Helps!

 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up. THANKS!

Community Support
Community Support

Hi @sandra1 ,

Based on the needs that you mentioned, I think the ForAll function and Collect function could achieve your needs. I have made a test on my side, please consider take a try with the following workaround:

Add a button in your app, set the OnSelect property to following:

Clear(TrueFriends);
ForAll(
       RenameColumns('[dbo].[friendship]', "person_id_1", "personA", "person_id_2", "personB"),
       If(
          !IsBlank(LookUp('[dbo].[friendship]', person_id_1 = personB && person_id_2 = personA)),
          Collect(
                   TrueFriends, 
                   {
                       PersonA: person_id_1, 
                       PersonB: person_id_2
                   }
         )
       )
)

Then you could add a Data Table control within your app, set the Items property to following:

TrueFriends

then enable the PersonA field and PersonB field within the Data Table, after you press the button control, then you could see the true friends within your friendship table.

 

Please take a try with above solution, check if the issue is solved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Hey @v-xida-msft,

Thank you very much for your answer. Your solution works, but it's really slow. I have a list of 5 people and 3 "real friends" and it took about 2 seconds for the data to load. 

Yesterday I tried the same with a database view and it was much faster. 

So I think your solution is not delegable (I think the for all statement is executed by powerapps. Isn't it? ) 

Do you know a solution that is delegable? 

@yashag2255 I tried your solution, but I got a lot of bugs and couldn't get it to work. 

 

Hi @sandra1 ,

Yeah, you are right. The ForAll function is not a delegable function in PowerApps currently. Based on the needs that you mentioned, if you want to achieve your needs using Delegable functions, I afraid that there is no way to achieve your needs.

 

As an alternative solution, if you want to improve your formula's performace in your app, I think the collection could achieve your needs. You could consider save your SQL Table records into a collection, then use the collection as data source in your app.

 

I have made a test on my side, please take a try with the following workaround:

Set the OnStart property of App to following:

ClearCollect(SQLTableRecords, '[dbo].[friendship]')

Add a button in your app, set the OnSelect property to following:

Clear(TrueFriends);
ForAll(
       RenameColumns(SQLTableRecords, "person_id_1", "personA", "person_id_2", "personB"),
       If(
          !IsBlank(LookUp('[dbo].[friendship]', person_id_1 = personB && person_id_2 = personA)),
          Collect(
                   TrueFriends, 
                   {
                       PersonA: person_id_1, 
                       PersonB: person_id_2
                   }
         )
       )
)

Please take a try with above solution, then re-load your app (fire the OnStart property of App), then check if the issue is improved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Community Highlights

Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (4,479)