Search This Blog

Sunday 17 December 2017

COUNT DISTINCT VALUES USING PIVOT TABLE


I have a column "Product Category" in my dataset having 23 entries including duplicates.


I want to know the distinct number of categories. There are different ways of doing it. In this post, I am going to do it using pivot table.

Step-1: Select your data > Go to Insert Tab > Click on PivotTable or You can use keyboard shortcut Alt + NV. A dialogue box will appear to create pivot table.

Step-2: After selecting the range and worksheet where you want to place the pivot table, check "Add this data to the Data Model" (as shown below) and click OK.



This process takes a few seconds as it loads the data into data model.

Step-3: Drag your field onto the Values area in the Pivot Table list. It will display the count of Product Category in the table (overall count including duplicates).




Now, we will perform two more steps to get the distinct count.

Step-4: Go to Value Field Settings by right clicking on the field in the pivot table.


Step-5: In the appeared dialogue box, select Distinct Count as type of calculation and click OK. You will get the distinct count of product categories.


Note: "Distinct Count" will not appear as type of calculation if you don't check "Add this data to the Data Model" while creating the pivot table.

By formula : Count unique entries from a list of duplicates

Is this post helpful?

Put your valuable comments in the comment box:)

Sunday 10 December 2017

WORKING WITH DATES IN VBA


This post is dedicated on handling the dates in VBA by using some in-built functions. I have explained some functions in short, used to work with dates.

Assigning a datevalue to a variable.

MyDate = "10-12-2017"                       'Directly passing the date
MyDate = DateSerial(2017, 12, 10)      'DateSerial function to assign a date
MyDate = Date                                      'Passing today's date using "date" function

Adding an interval to a date like years, months, quarters etc. We have an in-built function DATEADD for it which takes three arguments Interval, Number, Date.

Example:
DateAdd("yyyy", 1, "10-12-2017")     'Adding one year to a date; new date is 10-12-2018
DateAdd("m", 2, "10-12-2017")          'Adding 2 months to a date; new date is 10-02-2018
DateAdd("d", 3, "10-12-2017")           'Adding 3 days to a date; new date is 13-12-2017
DateAdd("ww", 1, "10-12-2017")       'Adding a week to a date; new date is 17-12-2017
DateAdd("q", 1, "10-12-2017")           'Adding a quarter to a date; new date is 10-03-2018

Similarly, we have other intervals to add hours, minutes and seconds to a date. Strings used to specifiy the others intervals:
"h" - hours, "n" - minutes, "s" - seconds

Fetching or extracting a part of the date. We have DATEPART function in VBA to do the job. It takes four arguments Interval, Date, FirstDayofWeek, FirstWeekofYear. Last two arguments are optional.

FirstDayofWeek - Specifies the weekday that should be used as the first day of the week. Default is vbSunday.
FirstWeekofYear - Specifies the week that should be used as the first week of the year. Default is vbFirstJan1.

Example:
DatePart("d", "10-12-2017")          'Returns day of month (1-31) i.e. 10
DatePart("m", "10-12-2017")         'Returns month i.e. 12
DatePart("yyyy", "10-12-2017")    'Returns year i.e. 2017
DatePart("q", "10-12-2017")          'Returns quarter i.e. 4
DatePart("ww", "10-12-2017")      'Returns week of year (1-53) i.e. 50
DatePart("w", "10-12-2017")         'Returns day of week (1-7) i.e. 1

Likewise DATEADD function, we have other intervals in DATEPART function also. Strings used to specify other intevals:
"h" - hours, "n" - minutes, "s" - seconds, "y" - day of year (1-366)

We also have MONTH and MONTHNAME function to return the month number and name of the month respectively. MONTH function takes a date only as an argument. MONTHNAME function takes two arguments Month, Abbreviation. Second argument is optional which takes boolean value TRUE or FALSE. TRUE to abbreviate the month name like Jan, Feb, Mar etc.

Example:
Month("10-12-2017")                                            'Returns 12
MonthName(Month("10-12-2017"), False)           'Returns "December "
MonthName(Month("10-12-2017"), True)            'Returns "Dec"

Likewise MONTH and MONTHNAME function, we have WEEKDAY and WEEKDAYNAME function to return the day of the week and name of the weekday respectively. WEEKDAY takes two arguments Date and FirstDayofWeek. Second argument is optional which specifies the weekday that should be used as first day of the week. Default is vbSunday if nothing specifies.

WEEKDAYNAME takes three arguments Weekday, Abbreviation and FirstDayofWeek. Last two arguments are optional. Abbreviation takes boolean value TRUE or FALSE. TRUE to abbreviate weekday name like Sun, Mon etc. FirstDayofWeek specifies the weekday that should be used as first day of the week. Default is vbSunday if nothing specifies.

Example:
Weekday("10-12-2017", vbMonday)    'Returns the day of the week i.e. 7
WeekdayName(Weekday("10-12-2017", vbSunday), False, vbSunday)    'Returns "Sunday"
WeekdayName(Weekday("10-12-2017", vbMonday), True, vbSunday)    'Returns "Sat"

Formatting dates. VBA has an in-built function FORMATDATETIME to assign a format to the given date. This function takes two arguments Expression, NameFormat. Second argument is optional.

Example:
FormatDateTime("10-12-2017")                                         'Returns 10-12-2017
FormatDateTime("10-12-2017", vbLongDate)                   'Returns 10 December 2017
FormatDateTime("10-12-2017", vbShortDate)                   'Returns 10-12-2017
FormatDateTime("10-12-2017 09:30:00", vbLongTime)   'Returns 09:30:00
FormatDateTime("10-12-2017 09:30:00", vbShortTime)   'Returns 09:30

Finally, wrapping up this post with ISDATE function used to check whether the passed value is a valid date or not. It takes only one arguement i.e. Expression. It returns TRUE if the passed value is a proper date, time or or a text representation of date or time and FALSE for all non-date strings and numbers.

IsDate("10-12-2017")               'Returns TRUE
IsDate(43079)                           'Returns FALSE
IsDate("Excel VBA Tips")       'Returns FALSE
IsDate(#9:30:00 AM#)             'Returns TRUE

Is this post helpful?

Kindly post your valuable comments or suggestions.

Thanks!