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

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (1,126)