简体   繁体   中英

Excel - Count Unique and Filtered Values

I'm trying to create a report to count the number of events based on a few conditions. This is the formula I've came up with so far:

=SUM(COUNTIFS('Sheet1'!B:B,"202204",'Sheet1'!M:M,{"This is text 1","This is text 2","This is text 3","This is text 4"}))

The formula above works well counting the total number of events but it doesn't remove the duplicates.

The first condition is to select the period 202204 and then match the events with the text description. Each event has a unique number but it comes with various asset numbers in the next column in Sheet1 that I don't need for the report I'm creating. The reason why I only need to know/count the total number of events for each month based on the period and description.

This report is pulling data from another tab which is importing filtered data from a different spreadsheet (image below). The final goal here is to allow my team to simply click Refresh All data to automatically update the report.

The data in this spreadsheet has been edited for privacy reasons and the dataset is a lot bigger than what I'm showing.

dataset

And this is the outcome that I need:

report - final outcome

Thanks!

Excel's unique function doesn't like to be counted does it! This task is probably better suited to using PowerQuery, but if you're looking for a one-liner then you can concatenate the columns, filter using unique and use concat to create one long string of your unique elements.

From here you want to count unique elements for a certain period, denoted by a substring. One of the easier ways to search the longer string for a substring's occurrences is to note the difference in the length before and after removing the substring using the substitute function, and then dividing that difference by the length of the substring. This is cleaner now that the let function exists in Office365.

Putting all this together gives;

=LET(str,CONCAT(UNIQUE(CONCATENATE(B:B,M:M))),(LEN(str)-LEN(SUBSTITUTE(str,"202204","")))/LEN("220204"))

The CONCAT(UNIQUE(CONCATENATE creates the long string, and LET assigns it the name str , and then we use the difference in the length of str before and after removing the substring and divide to count how many times 202204 occurs.

If you don't have Office 365, then you need to create the longer string twice, so the formula is a little longer (and slower);

=(LEN(CONCAT(UNIQUE(CONCATENATE(B:B,M:M))))-LEN(SUBSTITUTE(CONCAT(UNIQUE(CONCATENATE(B:B,M:M))),"202204","")))/LEN("220204")

I like to "externalize" conditions - in this case the valid descriptions.

You can use the following formula:

=LET(filteredList, FILTER(tblData,(COUNTIF(tblFilter[valid Description],tblData[Description]))*(tblData[Date]=[@Date])),
resizedList,FILTER(filteredList,{1,1,1,0}),
uniqueList,UNIQUE(resizedList),
ROWS(uniqueList))

filteredList returns a list for the according date and the valid descriptions

resizedList removes the description column

uniqueList can then return the unique values for Date, event, assest (as description is omitted)

Then the number of unique rows is returned.

This is my setting:

在此处输入图像描述

I found a solution at Excel Forum for my own problem that I would like to share here, although I tried both above which didn't work or were complex to my current skills. I'm thankful for you taking the time to help me.

The formula is:

=SUM(SIGN(COUNTIFS('Sheet1'!$B$2:$B$2000,TEXT('Column Name A'!D2,"yyyymm"),'Column Name B'!$C$2:$C$2000,UNIQUE('Column Name B'!$C$2:$C$2000),'Column Name C'!$M$2:$M$2000,{"This is text 1","This is text 2","This is text 3","This is text 4"})))

The column names and other details were changed for privacy reasons but this is the syntax that made more sense to me and has been working well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM