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

Filter records from Groupby collection based on number of records or record properties

Hello Power Users,

 

I have a Collection which was created with "GroupBy" by Hospitals and Date of arrival:

 

Sample Data:

Hospital1, 29.01.2021, {Here I have nested table with patients who arrived on this day} 3patients

Hospital1, 30.01.2021, 2 patients

Hospital2, 28.01.2021, 1 patient

Hospital3, 27.01.2021, 2 patients

I want to count rows(patients) on each of those Results, so It look like this above (patients aren't counted yet) and Filter Collection if that Count is <>2 , only keeping results with 2 rows,

Example output:

Hospital1, 30.01.2021, 2 patients,

Hospital3, 27.01.2021, 2 patients,

 

That's one problem, 

 

And then I want to check if those patients have a property which I have stored in my dropdown.selected, lets assume I have 10 possible properties and I want to only keep records with propertyId.1 and propertyId.2 and each of those properties cannot be duplicated. So If patient A have property1, Patient B can have only property2 or both patients need to be filtered out.

 

@WarrenBelz  I saw that you solved similar case, but without using collections (pardon me, I've lost the topic), so maybe You could look at this one if not in a hurry 🙂 ?  

Best Regards

Zbigniew

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User III
Super User III

@Z_Lukowski 

Sorry for the delay in response...are you still working on this?

 

For your formula, consider the following:

ClearCollect(colMainCollection,
    Filter(
        AddColumns(
            GroupBy(
                AddColumns(
                    Filter(
                        Visits,
                        'Visit Status' = Dropdown1_1.Selected.Value,
                        'TypeOfDisease'.name = Dropdown4.Selected.Name1 || 'TypeOfDisease'.name = Dropdown4.Selected.Name2,
                        'RandomUselessNumber' >= Value(TextInput1_2.Text) && 'RandomUselessNumber' <= Value(TextInput1_3.Text),
                        !StartsWith('AlreadyAdded'.'IdentifierOfDoctor', "Dr.")
                    ),
                    "Hospital Name", 'Hospital'.'Name '
                ),
                "Hospital Name", "DateOfPatientArrival", "AggregatedOrdersByHospitalNameAndDateOfPatientArrival"
            ),
            "Number of records", CountRows(AggregatedOrdersByHospitalNameAndDateOfPatientArrival),
            "_distinctRecords", CountRows(Distinct(AggregatedOrdersByHospitalNameAndDateOfPatientArrival, 'TypeOfDisease'.name))
        ),
        ('Number of records' = 2 ) && ('_distinctRecords' <> 2)
    )
)    

This will count the number of distinct Disease types.  Then in the filter that takes out the number of records not equaling 2 (note, I've taken this into the formula here rather than reprocessing the collection with a removeIf) we just add in to check that the _distinctRecords is not 2.

This should give you what you need (if I am reading your descriptions right)

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

3 REPLIES 3
RandyHayes
Super User III
Super User III

@Z_Lukowski 

For your first issue:

Your formula should resemble this:

Filter(
    AddColumns(
        <yourCurrentGroupByFormulaHere>,
        "_patientCount", CountRows(<yourPatientTableColumnFromYourGroupBy>)
    ),
    _patientCount = 2
)

 

For the second (yes, you need no collections for this - and practically nothing else):

I would need to get a little clarification on your scenario.  Can you explain more about the Dropdown and the items it would have and how that relates to the properties with the patient?  Also, when you say "keep records with..." are you referring to the main group or the patients in the patient table in your group?  I believe it is the last one, so, I'll also need to see your GroupBy formula that you have now (not sure if this is the one we worked on in your other thread).

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Hey @RandyHayes 

First point checked, based on the code you provided I was able to build working record counter and filter out the records:

Z_Lukowski_0-1613059755849.png

This is text version of my working code:

(btw how to make gray background and format this code, like You did in the above example)

 
ClearCollect(colMainCollection,
AddColumns(
GroupBy(
AddColumns(
Filter(
Visits,
'Visit Status' = Dropdown1_1.Selected.Value,
'TypeOfDisease'.name = Dropdown4.Selected.Name1 || 'TypeOfDisease'.name = Dropdown4.Selected.Name2,
'RandomUselessNumber' >= Value(TextInput1_2.Text) && 'RandomUselessNumber' <= Value(TextInput1_3.Text),
!StartsWith(
'AlreadyAdded'.'IdentifierOfDoctor',
"Dr."
)
),
"Hospital Name",
'Hospital'.'Name '
),
"Hospital Name",
"DateOfPatientArrival",
"AggregatedOrdersByHospitalNameAndDateOfPatientArrival"
),
"Number of records",
CountRows(AggregatedOrdersByHospitalNameAndDateOfPatientArrival)
));
RemoveIf(colMainCollection, 'Number of records' <>2);
 
Now I just need to access each TypeOfDisease'.name of collection and check If its not same as the paired record based on hospital and date of arrival (this groupby collection is showing those results aggregated already, and patients are nested table in which TypeOfDisease'.name (lookup record) exists.
 
I've started coding three weeks ago, so this code may look overcomplicated, any hints are warmly welcome 🙂
 
Best Regards
Zbigniew
RandyHayes
Super User III
Super User III

@Z_Lukowski 

Sorry for the delay in response...are you still working on this?

 

For your formula, consider the following:

ClearCollect(colMainCollection,
    Filter(
        AddColumns(
            GroupBy(
                AddColumns(
                    Filter(
                        Visits,
                        'Visit Status' = Dropdown1_1.Selected.Value,
                        'TypeOfDisease'.name = Dropdown4.Selected.Name1 || 'TypeOfDisease'.name = Dropdown4.Selected.Name2,
                        'RandomUselessNumber' >= Value(TextInput1_2.Text) && 'RandomUselessNumber' <= Value(TextInput1_3.Text),
                        !StartsWith('AlreadyAdded'.'IdentifierOfDoctor', "Dr.")
                    ),
                    "Hospital Name", 'Hospital'.'Name '
                ),
                "Hospital Name", "DateOfPatientArrival", "AggregatedOrdersByHospitalNameAndDateOfPatientArrival"
            ),
            "Number of records", CountRows(AggregatedOrdersByHospitalNameAndDateOfPatientArrival),
            "_distinctRecords", CountRows(Distinct(AggregatedOrdersByHospitalNameAndDateOfPatientArrival, 'TypeOfDisease'.name))
        ),
        ('Number of records' = 2 ) && ('_distinctRecords' <> 2)
    )
)    

This will count the number of distinct Disease types.  Then in the filter that takes out the number of records not equaling 2 (note, I've taken this into the formula here rather than reprocessing the collection with a removeIf) we just add in to check that the _distinctRecords is not 2.

This should give you what you need (if I am reading your descriptions right)

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

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