Can I create a COUNTIF calculated column in SharePoint?
Asked Answered
P

5

8

Is there a way to create a SharePoint calculated column that returns a count of the number of entries in a list? So If I have 3 customers in my list with the company "Starbucks" I'd like the field to return "3"

Persse answered 10/6, 2009 at 20:46 Comment(0)
S
14

(Edited some wording for clarity per suggestion from dariom).

You may be able to get what you want with another list using a not-so-well-known variation of a lookup column.

Let's say you have a list called Companies with values in the title column like "Starbucks", "Peets", etc. Now you also have the Customers list you refer to, but the "Company" column is a lookup column pointing to the title column in the Companies list.

You can add a count very similar to what you described to your Companies list. Go to your Companies list, add a column of type "Lookup" referring to the Customers list and you'll notice that in the drop-down area where you define the lookup if you point back to the Customers list, you'll have a new option called "Count Related". This is here automatically because it recognizes that the Customers list has a lookup pointing back to this one. Select that Count Related option and now your Companies list will have a column counting how many customers are associated with that company.

No coding, Javascript hacks, or anything. Just hidden SharePoint auto-magic. Column Related

Shalandashale answered 11/6, 2009 at 5:41 Comment(5)
Sam, this is perfect (+1) - I didn't know this was possible! I was a bit confused at first when I read your instructions. Suggest editing 'Go add a column of type "Lookup" and you'll notice...' to 'Go to your Companies list, add a column of type "Lookup" referring to the Customers list and you'll notice...' to be more explicit.Alodie
Neat! +1! (>15 character comment limit? Really?)Everglades
Is there a way to filter this by another column in the Customers list?Bioscope
Here is a link: pawansatope.blogspot.de/2012/01/… (Not my blog)Tammietammuz
Just curious, does this require the Companies > Title column values (that are firstly 'looked up' in the Customers > Company column) to be unique?Jordanna
A
2

No, I don't think there's a way to do this using the out-of-the-box calculated column.

Some other ways you could accomplish this are:

  • Create a view for your list that with a group by on the company field and include the total count. This is easiest, but might not be exactly what you're looking for.
  • Create a custom column type that executes a CAML query to find items that you're interested in. There is a learning curve if you've not done it before and if the list that you're adding this custom column to has lots of rows, you'll be executing a query for each row which is inefficient - it'll be OK for a small number of rows.
  • Use an event handler on the list that updates a column value each time a new item is added or removed from a list. This is easier, but can also be inefficient if you have a large number of items in your list.
Alodie answered 10/6, 2009 at 21:7 Comment(2)
Actually what I'm trying to do is filter a list so that the only items listed are where there is 2 or more items from that company. I assume I need to create a calculated column to do this. So my calculate column counts how many times each company is in my list...and then I can filter by saying When (my calculated column > 1). Is there another way to do this?Persse
Sam Yate's answer is the easiest way to do this. Accept his answer!Alodie
E
2

As dariom said (damn my slow typing skills, +1!), only the current row can be operated on with calculated columns by default in SharePoint. There are a couple of documented workarounds involving SharePoint Designer or jQuery, though.

Everglades answered 10/6, 2009 at 21:13 Comment(0)
I
2

You can get a Count of specific list items in an XSLT Data View

To do this you will need SharePoint Designer.

Right click on your SharePoint List view (ensure the list view contains the field you want to filter by) select convert to XSLT Data View. Then in the Data Source Windows select Data Source Tab and drag and drop the field you want to get a total on for the specific items into where you want it displayed in your XSLT Data View. Click on the numerical value that is showing you should get a lightening bolt icon, select the drop down and choose Count, then select again and choose Filter. Select "Click here to add a new clause" then choose your field name again and enter your unique value as Starbucks and click OK, you can repeat this process for other fields you want the totals on. You will now see the total number of Starbucks items in the list.

Intestate answered 25/8, 2009 at 15:59 Comment(0)
T
1

I got something similar to work in a way similar to Niall. Basically, I:

  1. Based on the source list, created a Data View Web Part (DVWP) on a "test" web part page.
  2. Added the footer column, which gives a count.
  3. Set the filter for my conditions (i.e., the items I want to count).
  4. In the code, deleted the recurring items row.

I was left with just the footer, which displayed a filtered count for all the list items. I further customized the footer by taking out the shaded background. Finally, I exported this web part and imported it onto the page where I wanted users to see a total of items in the list (which met the criteria).

Tazza answered 1/9, 2009 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.