cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JScherff
Frequent Visitor

CountRows in data table with multiple comboboxes

Hello friends!

 

I'm new to PowerApps but am pretty code savvy (heavy Microsoft VBA background).  I have an app sheet with 10 comboboxes that help to filter data in a data table lower on the screen (source is 'tblMAINDATA' - a SP list).  The filter is working fine to update the data in the table, but I've created a label to show "XX records out of 115" and the XX is just not updating as expected.  If I select 1 combobox item, it's accurate, if I select a second one, it seems to be an "additive" answer and increase the count even when the table shows less matching results, rather than a count of the actual records that meet ALL criteria.  I seems to be pretty close, but I must be not accounting for an AND or OR condition to correctly get the total rows (or records) that match.

 

I've tried the second part of the code with both an AND and OR condition with no success.  

 

Here's what I have in the label as the "ITEM" formula:

 

CountRows(
Filter(
tblMAINDATA,
If(
!IsBlank(cboApproved.Selected.Result) &&
!IsBlank(cboActive.Selected.Result) &&
!IsBlank(cboFranchise.Selected.Result) &&
!IsBlank(cboCustomerFocusedArea.Selected.Result) &&
!IsBlank(cboDeviceTechnologyFunction.Selected.Result) &&
!IsBlank(cboOriginalManufacturer.Selected.Result) &&
!IsBlank(cboFDAProdCode.Selected.Result) &&
!IsBlank(cboFDAProdDescription.Selected.Result) &&
!IsBlank(cboDHFCode.Selected.Result) &&
!IsBlank(cboDeviceSpecificTradeName.Selected.Result),
"0",
cboApproved.Selected.Result = Approved,
cboActive.Selected.Result = Active,
cboFranchise.Selected.Result = Franchise,
cboCustomerFocusedArea.Selected.Result = CustomerFocusedArea,
cboDeviceTechnologyFunction.Selected.Result = DeviceTechnologyFunction,
cboOriginalManufacturer.Selected.Result = OriginalManufacturerOM,
cboFDAProdCode.Selected.Result = FDAProductCode,
cboFDAProdDescription.Selected.Result = FDAProductDescription,
cboDHFCode.Selected.Result = DHFCode,
cboDeviceSpecificTradeName.Selected.Result = DeviceSpecificTradeName
)
)
)

& " records out of " & CountRows(tblMAINDATA)

 

 

Thank you all in advance.

John

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@JScherff 

Okay...there is work to be done!!  If you go with the Gallery, then you need to add all the controls and format as you want. The good news then is that your final count formula will just CountRows(yourGallery.AllItems).

 

If you stick with the DataTable, then you need to use what I call a dynamic table variable.

Here is how you would do it...

1.  Add a new screen to your app - choose that new screen to be a Scrollable Screen (very important)

2.  Once the new screen is created, find the Canvas control in the screen (most likely named Canvas1) and select it.

3.  Resize the Canvas control.  You can make it quite small.  It will not be seen, so tiny is fine.  Also, resizing the control will break its relationship with the other controls that come with a scrollable screen...which is good!

4. Now - Cut the Canvas Control (Ctrl-X)

5. You can now delete the screen you just added as it is not needed.

6. Return to the screen with your DataTable control and then Paste (Ctrl-V) the Canvas control into that screen.  You can set the visible to False on that canvas as again, we don't need to see it, we just need the DataCard control that came with it.

7.  Select the DataCard (most likely DataCard1) that is IN the Canvas control.  At this point it is good to rename it to be meaningful - Let's call it dtcTableData

 

Datacards have a VERY little unknown property on them called Update.  That Update property is very universal.  It is a Record, and you can make it ANY type of record you want.

 

SO...

8.  Select the Update property of your DataCard and put in the following formula:

{TableData:
  Filter(
    tblMAINDATA,
    (IsBlank(cboApproved.Selected.Result) || Approved in cboApproved.SelectedItems.Result),
    (IsBlank(cboActive.Selected.Result) || Active in cboActive.SelectedItems.Result),
    (IsBlank(cboFranchise.Selected.Result) || Franchise in cboFranchise.SelectedItems.Result),
    (IsBlank(cboCustomerFocusedArea.Selected.Result) || CustomerFocusedArea in cboCustomerFocusedArea.SelectedItems.Result),
    (IsBlank(cboDeviceTechnologyFunction.Selected.Result) || DeviceTechnologyFunction in cboDeviceTechnologyFunction.SelectedItems.Result),
    (IsBlank(cboOriginalManufacturer.Selected.Result) || OriginalManufacturerOM in cboOriginalManufacturer.SelectedItems.Result),
    (IsBlank(cboFDAProdCode.Selected.Result) || FDAProductCode in cboFDAProdCode.SelectedItems.Result),
    (IsBlank(cboFDAProdDescription.Selected.Result) || FDAProductDescription in cboFDAProdDescription.SelectedItems.Result),
    (IsBlank(cboDHFCode.SelectedItems.Result) || DHFCode in cboDHFCode.SelectedItems.Result),
    (IsBlank(cboDeviceSpecificTradeName.Selected.Result) || DeviceSpecificTradeName in cboDeviceSpecificTradeName.SelectedItems.Result)
  )
}

The above is creating a Record in the Update property that has a column called TableData.  That column will have the table of all the data you need for your datatable (and now you can access it anywhere).

 

So, your Items property on the DataTable would then become:  dtcTableData.Update.TableData

And, your Label text property would become:

With({_count: CountRows(dtcTableData.Update.TableData)},

    _count & " record" & If(_count<>1, "s") & " out of " & CountRows(tblMAINDATA)

)

 

This will give you the DataTable still and now you are essentially accessing the "Items" property as the real formula for that is outside of the Items property.

The table will all update dynamically as the users change their selections.

 

 

Also, unless you have Multi-Selection needed on those comboboxes, I would allow the user to just select one choice and then change your Update property to:

{TableData:
  Filter(
    tblMAINDATA,
    (IsBlank(cboApproved.Selected.Result) || Approved = cboApproved.Selected.Result),
    (IsBlank(cboActive.Selected.Result) || Active = cboActive.Selected.Result),
    (IsBlank(cboFranchise.Selected.Result) || Franchise = cboFranchise.Selected.Result),
    (IsBlank(cboCustomerFocusedArea.Selected.Result) || CustomerFocusedArea = cboCustomerFocusedArea.Selected.Result),
    (IsBlank(cboDeviceTechnologyFunction.Selected.Result) || DeviceTechnologyFunction = cboDeviceTechnologyFunction.Selected.Result),
    (IsBlank(cboOriginalManufacturer.Selected.Result) || OriginalManufacturerOM = cboOriginalManufacturer.Selected.Result),
    (IsBlank(cboFDAProdCode.Selected.Result) || FDAProductCode = cboFDAProdCode.Selected.Result),
    (IsBlank(cboFDAProdDescription.Selected.Result) || FDAProductDescription = cboFDAProdDescription.Selected.Result),
    (IsBlank(cboDHFCode.SelectedItems.Result) || DHFCode = cboDHFCode.Selected.Result),
    (IsBlank(cboDeviceSpecificTradeName.Selected.Result) || DeviceSpecificTradeName = cboDeviceSpecificTradeName.Selected.Result)
  )
}

The above will be delegable and you will not run into record limit issues.

 

Of course, if you do need them to select multiple choices, then leave as-is, but realize that the formula would not be delegable.

_____________________________________________________________________________________
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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
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

11 REPLIES 11
Nogueira1306
Super User
Super User

Not sure if the problem is because of your condition that does not "count" with all options because you have a ton of a options...

I am assuming that when you choose some combobox you show the itens in a gallery and if yes, I am assuming that it works well (the filter).

 

If yes, why dont you do something like this:

 

CountRows(Gallery.AllItems) & " records out of " & CountRows(tblMAINDATA)

 

If not, I think that you should check every possibility and do the filter based on each possibility... You do that using a switch

 

If you need additional help please tag me in your reply and please like my reply.
If my reply provided you with a solution, pleased mark it as a solution ✔️!

Best regards,

Gonçalo Nogueira

Check my LinkedIn!

Buy me a coffee!

Check my User Group (pt-PT)!

Last Post on Community

My website!

RandyHayes
Super User
Super User

@JScherff 

Luckily this is PowerApps...a non-development platform, so no coding knowledge required (helpful in some ways, but disruptive in others!)

 

Your If statement is not making much sense.  An If statement takes a few parameters.  IN your case, the syntax is:

If(Condition1, result1,
   Condition2, result2,
   Condition3, results3,
   ...etc...,
   else
)

In your If statement, the first Condition1 is all of your ANDed IsBlanks (which you really don't need).  The result1 of your formula is "0".

Your second condition (condition2) is: cboApproved.Selected.Result = Approved 

and the Result2 is: cboActive.Selected.Result = Active

That makes no sense!!

 

What are Approved and Active and the other values you have in that formula and how do they relate to the data?

 

But, what you really want to be doing is counting (with the Count functions) the records of your table that meet criteria.  Not using If statements to derive your values.

 

_____________________________________________________________________________________
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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

Thank you for the reply.  I'm using a data table to display the data as this provided the best functionality for my needs.

Hi Randy,

 

My formula is basically a mix of several different formulas that I've come across while searching for an answer, so it may very well "make no sense".  

 

The combo boxes allow the user to fine tune the data (in my case devices) in the list.  Each combo box are basically filters that are frequently needed to narrow down the data.  Sometimes a user will only need to use 1 or 2 and other times they will use several.  Rarely would anyone ever need to use them all, so some may stay "blank" or "empty" during the filtering process.

 

cboApproved.Selected.Result = Approved  (the "Approved" in this case is a column with Yes, No or Other)  So I'm checking to see that the combo box value matches the data in the column.

 

Same for all the other combo boxes - it's checking to see if the data matches the selected items in the data versus the combo box.

 

I can very well be way off on how to properly count the items with my formula, as I said I've tried several iterations of items found while searching.  If there is an easier way to just count the rows returned in a data table after each combo box is changed, please let me know.

 

I do appreciate your time.


John  

RandyHayes
Super User
Super User

@JScherff 

Yes, so then you are using the comboboxes to filter data.  I am getting from your description that you are using a DataTable control.  I would firstly consider using a Gallery rather than a datatable control as the Gallery exposes the AllItems property - which can then be counted as mentioned by @Nogueira1306 

 

However, if you are using a DataTable control, then you will need to count the rows of the Items property (which is not exposed by the Datatable).  So, what is your Items property for that control?  And, is a Gallery an option for you instead?

_____________________________________________________________________________________
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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

Here's the Items property for my data table:

Filter(
    tblMAINDATA,
    If(
        IsBlank(cboApproved.Selected.Result),
        true,
        Approved in cboApproved.SelectedItems.Result
    ),
    If(
        IsBlank(cboActive.Selected.Result),
        true,
        Active in cboActive.SelectedItems.Result
    ),
    If(
        IsBlank(cboFranchise.Selected.Result),
        true,
        Franchise in cboFranchise.SelectedItems.Result
    ),
    If(
        IsBlank(cboCustomerFocusedArea.SelectedItems.Result),
        true,
        CustomerFocusedArea in cboCustomerFocusedArea.SelectedItems.Result
    ),
    If(
        IsBlank(cboDeviceTechnologyFunction.SelectedItems.Result),
        true,
        DeviceTechnologyFunction in cboDeviceTechnologyFunction.SelectedItems.Result
    ),
    If(
        IsBlank(cboOriginalManufacturer.SelectedItems.Result),
        true,
        OriginalManufacturerOM in cboOriginalManufacturer.SelectedItems.Result
    ),
    If(
        IsBlank(cboFDAProdCode.SelectedItems.Result),
        true,
        FDAProductCode in cboFDAProdCode.SelectedItems.Result
    ),
    If(
        IsBlank(cboFDAProdDescription.SelectedItems.Result),
        true,
        FDAProductDescription in cboFDAProdDescription.SelectedItems.Result
    ),
    If(
        IsBlank(cboDHFCode.SelectedItems.Result),
        true,
        DHFCode in cboDHFCode.SelectedItems.Result
    ),
    If(
        IsBlank(cboDeviceSpecificTradeName.SelectedItems.Result),
        true,
        DeviceSpecificTradeName in cboDeviceSpecificTradeName.SelectedItems.Result
    )
)

 

This code works great to filter the results from the combo boxes.  A gallery isn't totally a no for me, the users are just used to seeing the data in a table format, which I know a gallery can do as well - just more formatting involved.

 

So, yes, I would be willing to give the gallery a try as well.  But if we can, could we exhaust the data table route first?

 

John

RandyHayes
Super User
Super User

@JScherff 

Okay...there is work to be done!!  If you go with the Gallery, then you need to add all the controls and format as you want. The good news then is that your final count formula will just CountRows(yourGallery.AllItems).

 

If you stick with the DataTable, then you need to use what I call a dynamic table variable.

Here is how you would do it...

1.  Add a new screen to your app - choose that new screen to be a Scrollable Screen (very important)

2.  Once the new screen is created, find the Canvas control in the screen (most likely named Canvas1) and select it.

3.  Resize the Canvas control.  You can make it quite small.  It will not be seen, so tiny is fine.  Also, resizing the control will break its relationship with the other controls that come with a scrollable screen...which is good!

4. Now - Cut the Canvas Control (Ctrl-X)

5. You can now delete the screen you just added as it is not needed.

6. Return to the screen with your DataTable control and then Paste (Ctrl-V) the Canvas control into that screen.  You can set the visible to False on that canvas as again, we don't need to see it, we just need the DataCard control that came with it.

7.  Select the DataCard (most likely DataCard1) that is IN the Canvas control.  At this point it is good to rename it to be meaningful - Let's call it dtcTableData

 

Datacards have a VERY little unknown property on them called Update.  That Update property is very universal.  It is a Record, and you can make it ANY type of record you want.

 

SO...

8.  Select the Update property of your DataCard and put in the following formula:

{TableData:
  Filter(
    tblMAINDATA,
    (IsBlank(cboApproved.Selected.Result) || Approved in cboApproved.SelectedItems.Result),
    (IsBlank(cboActive.Selected.Result) || Active in cboActive.SelectedItems.Result),
    (IsBlank(cboFranchise.Selected.Result) || Franchise in cboFranchise.SelectedItems.Result),
    (IsBlank(cboCustomerFocusedArea.Selected.Result) || CustomerFocusedArea in cboCustomerFocusedArea.SelectedItems.Result),
    (IsBlank(cboDeviceTechnologyFunction.Selected.Result) || DeviceTechnologyFunction in cboDeviceTechnologyFunction.SelectedItems.Result),
    (IsBlank(cboOriginalManufacturer.Selected.Result) || OriginalManufacturerOM in cboOriginalManufacturer.SelectedItems.Result),
    (IsBlank(cboFDAProdCode.Selected.Result) || FDAProductCode in cboFDAProdCode.SelectedItems.Result),
    (IsBlank(cboFDAProdDescription.Selected.Result) || FDAProductDescription in cboFDAProdDescription.SelectedItems.Result),
    (IsBlank(cboDHFCode.SelectedItems.Result) || DHFCode in cboDHFCode.SelectedItems.Result),
    (IsBlank(cboDeviceSpecificTradeName.Selected.Result) || DeviceSpecificTradeName in cboDeviceSpecificTradeName.SelectedItems.Result)
  )
}

The above is creating a Record in the Update property that has a column called TableData.  That column will have the table of all the data you need for your datatable (and now you can access it anywhere).

 

So, your Items property on the DataTable would then become:  dtcTableData.Update.TableData

And, your Label text property would become:

With({_count: CountRows(dtcTableData.Update.TableData)},

    _count & " record" & If(_count<>1, "s") & " out of " & CountRows(tblMAINDATA)

)

 

This will give you the DataTable still and now you are essentially accessing the "Items" property as the real formula for that is outside of the Items property.

The table will all update dynamically as the users change their selections.

 

 

Also, unless you have Multi-Selection needed on those comboboxes, I would allow the user to just select one choice and then change your Update property to:

{TableData:
  Filter(
    tblMAINDATA,
    (IsBlank(cboApproved.Selected.Result) || Approved = cboApproved.Selected.Result),
    (IsBlank(cboActive.Selected.Result) || Active = cboActive.Selected.Result),
    (IsBlank(cboFranchise.Selected.Result) || Franchise = cboFranchise.Selected.Result),
    (IsBlank(cboCustomerFocusedArea.Selected.Result) || CustomerFocusedArea = cboCustomerFocusedArea.Selected.Result),
    (IsBlank(cboDeviceTechnologyFunction.Selected.Result) || DeviceTechnologyFunction = cboDeviceTechnologyFunction.Selected.Result),
    (IsBlank(cboOriginalManufacturer.Selected.Result) || OriginalManufacturerOM = cboOriginalManufacturer.Selected.Result),
    (IsBlank(cboFDAProdCode.Selected.Result) || FDAProductCode = cboFDAProdCode.Selected.Result),
    (IsBlank(cboFDAProdDescription.Selected.Result) || FDAProductDescription = cboFDAProdDescription.Selected.Result),
    (IsBlank(cboDHFCode.SelectedItems.Result) || DHFCode = cboDHFCode.Selected.Result),
    (IsBlank(cboDeviceSpecificTradeName.Selected.Result) || DeviceSpecificTradeName = cboDeviceSpecificTradeName.Selected.Result)
  )
}

The above will be delegable and you will not run into record limit issues.

 

Of course, if you do need them to select multiple choices, then leave as-is, but realize that the formula would not be delegable.

_____________________________________________________________________________________
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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

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

Ouch!  Ok, I knew it wasn't going to be easy so off I go.

 

Give me a day or so to get this sorted and try it out.  Other than the basic googling of what the benefits are of a gallery versus a data table, can you add any other items that might stop me in my tracks now to just convert over?  I'll have other tables that need to be populated and filtered, so I guess now is the time to "rethink" it.

 

I really appreciate the time you took to provide such a detailed answer - I will definitely try it out.

 

 

Well, as expected, I ran right off and tried it.  It works PERFECTLY!  Thank you so much again for your time.  I truly appreciate it.

 

Look forward to your feedback on gallery vs. data tables so I can plan my attack for the rest of the app.

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Top Solution Authors
Top Kudoed Authors
Users online (2,010)