So, I have an inspection form in an app. For each inspection item where a deficiency is found, I want to add supplemental information to a report that I then email to the user.
I order to do this, I have two tables serving as the back-end data source for my app. I pull these tables into collections for use in the app. One table (Inspections) has one record per inspection and many fields (representing the inspection questions) with y/n values. Another table is the SupplementaryInfo where there is a record for each inspection question with best practices, citation info etc.
I would like to use a Switch-like function to reference the SupplementaryInfo record for each inspection field containing a "no" value.
I need to reference records in one collection (SupplementaryInfo) based on field values in another collection (Inspections). So far I've tried this:
Filter(SupplementaryInfo, Switch( ThisItem.Q2 = "no", Q="2", ThisItem.Q3 = "no", Q="3", ThisItem.Q2 = "no" And ThisItem.Q3 = "no", Q= "2" Or Q="3"))
This code exists in a gallery displaying Inspections records, so ThisItem is referring to a single record in Inspections. Q# refers to a field in the Inspection collection (such as Inspection.Q2) and Q is a field in the SupplementaryInfo collection indicating what Inspection.field the record is referring to (the record where Q = 1 in SupplmentaryInfo has info related to field Inspections.Q1).
The above works, except for the case where Q2 and Q3 are both equal to "no". In this case, the Switch function sees that Q2 is "no" and stops and never gets to Q2 and Q3 = "no". I know that this case is trivial as I could put the Q2 and Q3 = "no" switch first, but my inspection form has many questions and the issue becomes exponentially more complicated with more fields to manage.
How can I choose multiple SupplementaryInfo records based on the values of the fields in my Inspection collection?
Did you use this formula in the Items property of the gallery? If so, the items inside the gallery is from data source "SupplementaryInfo" not "Inspections". Because the you used the syntax Filter( Table, Formula1 [, Formula2, ... ] ) and the table is SupplementaryInfo. One Gallery can only recognize one data source. That is to say, if you want to filter Inspections by records in SupplementaryInfo, then you will have to put records of SupplementaryInfo in another control, and filter Inspections using that control, not using the table directly.
Also, the syntax of Switch function should be:
Switch( Formula, Match1, Result1 [, Match2, Result2, ... [, DefaultResult ] ] )
That is to say, the formula should be something like:
Switch( ThisItem.Q2, "no", "Result")
I also cannot say that I have fully understand what you want to achieve, could you please give some data examples with these to tables and explain with the data?
Thanks, Mona. My Switch function was incorrect! The test report generates with the correct data now. I've attached an image of the test app I generated to play and solve this problem.
The Items property of my gallery is "Inspections". Inside of the gallery I have an html text box with my report. The report has two tables in it. One is the inspection data selected by - ThisItem.InspectionNumber. The other is the supplementary info table which should show certain records based on the fields in ThisItem.Q1 and ThisItem.Q2. I hope this explanation helps.
The way the supplementary table is populated is by a switch function containing all possible combinations of answers for Q1 and Q2. PowerApps searches through the switch functions until the single match is found and displays the relevant records from the supplementary info table. This is fine in my test case where I have 2 questions with 2 possible answers (yes or no), but impossible with any realistic number of inspection questions. 10 yes or no questions would require me to add 2^10 possible combinations of answers in my switch function. Ha!
Here is the portion of the html code which creates the supplementary info table in my report. The first block of code creates tables headers (columns) and the second populates those columns with data from my collection. Note the line beginning with "Concat". This line filters records from SupplementaryInfo (Ss) by only displaying info related to questions answered "no". "ThisItem" refers to a single record in the Inspections collection, as that is the data source for the gallery in which the html text is embedded. So, when an inspection has "no" for an answer to Question 1 (Q1), supplementary info about Q1 is displayed and so on.
<p style='font-weight:bold'>Supplementary Info:</p> <table style='width:100%'> <tr> <th style='width:5em'>Q</th> <th style='width:5em'>S1</th> <th style='width:5em'>S2</th> </tr> "& Concat(Filter(Ss, Switch( ThisItem.Q1&" "&ThisItem.Q2, "no no", Q="1" Or Q="2", "no yes", Q="1", "yes no", Q="2")), "<tr> <td style='text-align:center'>"&Q&"</td> <td style='text-align:center'>"&S1&"</td> <td style='text-align:center'>"&S2&"</td> </tr> ") &" </table>
What I want is for a Switch-like function that evaluates all possible answers and selects all which are true, instead of only selecting one match. then I could simply write:
Concat(Filter(Ss, Switch( ThisItem.Q1 = "no", Q="1", ThisItem.Q2 = "no", Q="2", ... ThisItem.Qn = "no", Q = "n"))
Is there a function like that? Or a way to work around to get to a much simpler code than hard-coding every possible combination of answers into my report?
Edited to add image
Check out our new profile badges recognizing authored solutions!
We are excited to announce the Power Apps Super Users!
Did you miss the call? Check out the Power Apps Community Call here.
Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!