Chủ Nhật, 10 tháng 1, 2016

Introduction To The DATEDIF Function

The DATEDIF function computes the difference between two dates in a variety of different intervals, such as the number of years, months, or days between the dates. This function is available in all versions of Excel since at least version 5/95, but is documented in the help file only for Excel 2000. For some reason, Microsoft has decided not to document this function in any other versions. DATEDIF is treated as the drunk cousin of the Formula family. Excel knows it lives a happy and useful life, but will not speak of it in polite conversation. Do not confuse the DATEDIF worksheet function with the DateDiff VBA function.
The syntax for DATEDIF is as follows:

=DATEDIF(Date1, Date2, Interval)

Where:
Date1 is the first date,
Date2 is the second date,
Interval is the interval type to return.

If Date1 is later than Date2, DATEDIF will return a #NUM! error. If either Date1 or Date2 is not a valid date, DATEDIF will return a #VALUE error.
The Interval value should be one of

Interval Meaning Description
m Months Complete calendar months between the dates.
d Days Number of days between the dates.
y Years Complete calendar years between the dates.
ym Months Excluding Years Complete calendar months between the dates as if they were of the same year.
yd Days Excluding Years Complete calendar days between the dates as if they were of the same year.
md Days Excluding Years And Months Complete calendar days between the dates as if they were of the same month and same year.

If Interval is not one of the items listed in above, DATEDIF will return a #NUM error.

If you are including the Interval string directly within the formula, you must enclose it in double quotes:

=DATEDIF(Date1,Date2,"m")

If you have the interval in another cell referenced by the formula, that cell should not have quotes around the interval string. For example, with the formula

=DATEDIF(Date1,Date2,A1)

cell A1 should contain m not "m".
Supressing Zero Value Components
In typical usage to calculate the number of years, months, and days between two dates, you would use a formula such as the following, where A1 is the start date and B1 is the end date:

=DATEDIF(A1,B1,"y")&" years "&DATEDIF(A1,B1,"ym")&" months "&DATEDIF(A1,B1,"md")
&" days"


This will return a string such as

12 years 8 months 14 days

However, if the number of years and/or months is 0, you'll get a string like

0 years 0 months 14 days

0 years 3 months 14 days

If you want to suppress the 0 values, and return a result such as

8 months 14 days
or
14 days

where the 0-valued components are not displayed, use a formula like the following:

=IF(DATEDIF(A1,B1,"y")=0,"",DATEDIF(A1,B1,"y")&" years ")&IF(DATEDIF(A1,B1,"ym")=0,"",
DATEDIF(A1,B1,"ym")&" months ")&DATEDIF(A1,B1,"md")&" days"


This will display only the date components whose value is greater than 0. The day value will always be displayed, but the year and/or the month value may be suppresed.
SectionBreak
Examples
The follow are some examples of the DATEDIF function.

Date1: 1-Jan-2007
Date2: 10-Jan-2007
Interval: d
Result: 9
Explanation:
There are 9, not 10, calendar days between these two dates.

Date1: 1-Jan-2007
Date2: 31-Jan-2007
Interval: m
Result: 0
Explanation:
There 0 complete calendar months between the two dates.

Date1: 1-Jan-2007
Date2: 1-Feb-2007
Interval: m
Result: 1
Explanation:
There are 1 complete months between the two dates.

Date1: 1-Jan-2007
Date2: 28-Feb-2007
Interval: m
Result: 1
Explanation:
There are 1 complete months between the two dates.

Date1: 1-Jan-2007
Date2: 31-Dec-2007
Interval: d
Result: 364
Explanation:
There are 364 days between the two dates.

Date1: 1-Jan-2007
Date2: 31-Jan-2007
Interval: y
Result: 0
Explanation:
There are 0 complete years between the dates

Date1: 1-Jan-2007
Date2: 1-July-2008
Interval: d
Result: 547
Explanation:
There are 547 days between the two dates.

Date1: 1-Jan-2007
Date2: 1-July-2008
Interval: m
Result: 18
Explanation:
There are 18 months between the two dates.

Date1: 1-Jan-2007
Date2: 1-July-2008
Interval: ym
Result: 6
Explanation:
There are 6 months between the two dates if the dates are considered to have the same year. The year it taken from Date1, not Date2. This makes a difference when one year is a leap year. Since 2007 is not a leap year, 29-Feb is not counted. See DATEDIF And Leap Years below.

Date1: 1-Jan-2007
Date2: 1-July-2008
Interval: yd
Result: 181
Explanation:
There are 181 days between the dates if the dates are considered to have the same year. The year it taken from Date1, not Date2. This makes a difference when one year is a leap year. Since 2007 is not a leap year, 29-Feb is not counted. See DATEDIF And Leap Years below.

Date1: 1-Jan-2008
Date2: 1-July-2009
Interval: yd
Result: 182
Explanation:
There are 182 days between the dates if the dates are considered to have the same year. This result is 182 not 181 since Date1 is a leap year and thus 29-Feb is counted.

Date1: 1-Jan-2007
Date2: 31-Jan-2007
Interval: md
Result: 30
Explanation:
There are 30 days between the 1st and 31st of the dates when both dates' month and years are considered to be the same.
SectionBreak
DATEDIF And Leap Years
When calculating date intervals, DATEDIF uses the year of Date1, not Date2 when calculating the yd, ym and md intervals. For example,

=DATEDIF(Date1,Date2,"md")

returns 28 for Date1 = 1-Feb-2007 and Date2 = 1-March-2009. Since Date1 is not a leap year, the date 29-Feb is not counted. But the same formula with Date1 = 1-Feb-2008 returns 29, since Date1 is a leap year and therefore the date 29-Feb is counted.
SectionBreak
Calculating Age
You can use the DATEDIF to calculate a person's age. For example, the formula below will calculate the age of a person as of the current date, where BirthDate is the person's date of birth.

=DATEDIF(BirthDate,TODAY(),"y")&" years "&DATEDIF(BirthDate,TODAY(),"ym")&" months "&DATEDIF(BirthDate,TODAY(),"md")&" days"

The Imposing INDEX

From my perspective, the Excel INDEX function is the single most important in the roster of Microsoft Excel functions.
Now that might be surprising considering the function's humdrum name, but please pay close attention, because INDEX is one of the magical secrets of how to use Excel! So what's so great about the INDEX function? It's nonvolatile, sprightly, agile, and versatile. Excel INDEX can return one value or an array of values; it can return a reference to one cell or to a range of cells. INDEX works well on either side of the three Reference Operators - the colon, the space, and the comma.

index_function_parameters_excelhero.png
 
The idea with INDEX is that you give it a range (or an array) and then specify an element to return. 
So,
=INDEX(A1:A5,2)
...returns a reference to cell A2.
And,
=INDEX({95,96,97,98,99},5)
...returns the value of 99.


Note:  If you use an international version of Excel, some of the array constants may not work as presented.  Please see the instructions at the end of this article.


Nothing extraordinary thus far. But it is crucial to understand and so I'll reiterate that the first example above returns a REFERENCE, while the second returns a VALUE. 
Also, note that both of these examples demonstrate how the Excel INDEX function behaves when passed a vector. A vector is a one-dimensional array or range. When passed a vector, INDEX does not care whether that vector is vertical or horizontal. The second parameter of INDEX identifies itself as row number. But this is not correct, when a vector is passed in. Instead of being the row number, the second input becomes the element number for a vector - and as such, horizontal vectors work just fine with this notation. In the second example above, five is not the row number, it is the element number!

But we can force INDEX to behave in its normal two-dimensional fashion:

=INDEX({95,96,97,98,99},1,5)

...also returns the value of 99.

And it is with this two-dimensional behavior that INDEX begins to really shine. Obviously,
=INDEX(A1:C5,1,3)
...returns a reference to C1. But what is surprising to most users is that both of the following do as well:
{=INDEX(A1:C5,0,3)}
{=INDEX(A1:C5,,3)}


Return Whole Rows or Columns
In fact, until the user understands what is happening here, they might think that the function has a bug because the overriding theme with INDEX is that unlike OFFSET for example, it must return an element from within the range or array passed to it - and row zero (or column zero) is outside. So what does ZERO or a null value mean in this setting?
If the row_num parameter is zero or missing, this directs INDEX to return the entire column specified by the column_num parameter!
{=INDEX(A1:C5,,2)}
...returns a reference to the range, B1:B5.
And the converse is also true - specifying a zero or missing column_num will return an entire row. The catch here is that the missing column_num MUST include the comma, like so:
{=INDEX(A1:C5,2,)}
...which returns a reference to the range, A2:C2.

But remember that when a particular cell is given a reference to a multi-cell range or that cell is set equal to an array, just the first element shows in the output cell - so most of the row or column returned by the two above INDEX formulas will be hidden. But you can array-enter one of these Microsoft Excel formulas over a range to see the entire output. You do this by selecting the output range first, typing the formula in the formula bar, and entering it by Control-Shift-Enter.
So yes the technique works with returning entire rows or columns of a two-dimensional array as well:
=SUM(INDEX({1,2,3,4,5;6,7,8,9,10;11,12,13,14,15},3,))
...results in 65, which is the summation of the entire third row of the input array.

Lookups
This ability to return entire columns or rows from a larger range or array is extraordinarily useful.  
Suppose we have a table or list of metrics for countries in the range of A1:M200. We can create a named formula that refers to this range - let's call it simply, d, for data. Then we can name individual columns of the data painlessly. If the country names are in the first column, we can create a named formula:
Country:   =INDEX(d,,1)
And if population is in the 4th column, we can create a named formula:
Population:   =INDEX(d,,4)
Now suppose we wanted to lookup the population of Scotland, all we need to do is:
=INDEX(Population,MATCH("Scotland",Country,0))
The Excel MATCH function simply produces a row number for INDEX.
This method of lookup is on par in terms of speed as doing a VLOOKUP, if you are doing just one lookup. But it has advantages over VLOOKUP. It is not restricted to looking up a column to the right like VLOOKUP is. Excel INDEX MATCH is significantly quicker than VLOOKUP if we are doing lookups for a list of countries, and we array-enter the one formula over the entire output column.
If we need to return multiple columns of metrics for a list of countries, the speed benefit is even greater. Suppose that GDP is the 2nd column of the data and that Capital was the 11th column.
If on a new worksheet, we wanted to output three columns of data for each country, Population, GDP, and Capital, the most efficient way to do so is to dedicate one column to create a common index, and then array-enter the INDEX formula over the entire three columns of output.
In this scenario, column B would be the list of countries. Column A would be dedicated to the common index. In A2, we would enter:
=MATCH(B2,Country,0)
...and then copy this formula down as far as the countries are listed in column B.
And then in columns, C, D, and E we would array-enter over the entire range that extends as far down as the countries:
{=INDEX(d,A2:A100,{4,2,11})}
To be clear, this means that if we had 99 countries in column B, we would select C2:E100, and then in the formula bar we would type the above formula, and then enter it by Control-Shift-Enter.
The end result is that the one formula is executed just once (in array fashion) and is brutally fast leaving a very light, non-volatile footprint in your model.

Sums
This usage of INDEX is a great way to power SUMPROPDUCT as well. For example:
=SUMPRODUCT( (Left(Country,1)="U")*Population )
...which returns the total population for all countries that start with the letter, U.
And while named formulas are self-documenting, this works just as well:
=SUMPRODUCT( (Left(INDEX(d,,1),1)="U")*INDEX(d,,4) )

The Dynamic Range
Building on this idea, we can alter the named formula, d, so that it results in a dynamic range instead of a fixed range. And here INDEX reigns supreme. 
A Dynamic range is often constructed with OFFSET or INDIRECT. Unfortunately, both of these Microsoft Excel functions are volatile, which simply means that all formulas that include these functions will recalculate every single time anything on the worksheet changes. For example, adding a value to any blank cell will cause all volatile formulas (and any other formulas that depend on them) to recalculate. Depending on your model design and size, this can have a major impact on the fluidity and responsiveness of the model.
INDEX can be used to create a dynamic range, and not only is it nonvolatile, it is way faster than either OFFSET or INDIRECT. In fact, the improvement in performance is so great that INDEX should be the foundation of all dynamic ranges in professional models.
The approach to creating the dynamic range is different than the approach used for OFFSET or INDIRECT. For those functions, the range is created within the function. With INDEX on the other hand, the dynamic range is produced by using INDEX on one side (or sometimes both) of the Range Operator, which is the colon.
For example, consider this normal looking range reference:
=A2:A100
By itself, this reference could be a hard reference to the country names in our list of countries. Assuming no blanks in our list and that there is a column header in A1, perhaps "Country", the following formula would return the name of the last country in the list:
=INDEX(A:A,COUNTA(A:A))
But the INDEX function here is really returning the reference to the last cell in column A with a country name, and then the equal sign forces the value of that cell to be returned. In the scenario that we have painted, this INDEX formula is really returning the reference, A100.
So the following two formulas point to the exact same range:
=A2:A100
=A2:INDEX(A:A,COUNTA(A:A))
But there is a significant difference. The first is a hard coded, static reference. The second is a nonvolatile, dynamic range that will expand or contract as the number of countries in the list changes. Please note that before we replace the Refers To value of our named formula, Country, with this INDEX based dynamic range, we need to make the references absolute. It would then look like this:
Country:   =$A$2:INDEX($A:$A,COUNTA($A:$A))
We can use the same techniques to create a two-dimensional dynamic range so that the number of columns is also dynamic:
d:   =$A$2:INDEX($1:$65535,COUNTA($A:$A),COUNTA($1:$1))
And with d defined in this dynamic manner, we can still do all of the wonderful row and column referencing from above, such as:
{=INDEX(d,$A2,{4,2,11})}

As a footnote to this section on dynamic ranges, I want to point out that when you use a function on either side (or both sides) of any of the three Reference Operators, the resulting formula always recalculates when the workbook is opened. So while INDEX is nonvolatile, it becomes what I term quasi-volatile when used for dynamic ranges - but this is orders of magnitude better than volatile, and so it remains the best dynamic range foundation by far.
And as a post script to this section on the dynamic range, you may very well ask should I not just use the new Structured Table References available since Excel 2007? While STR is a robust option, it is heavy. If you need speed, nothing beats the alacrity of INDEX.


Noncontiguous Areas
When working with ranges, INDEX offers a fourth parameter to select the AREA to work with from a noncontiguous input range. The areas are referenced by integer in the order that they appear in the input range. For example,
=INDEX((data1,data2,data3),,,2)
...returns a reference to data2. And
{=INDEX((data1,data2,data3),,1,3)}
...returns the entire first column of data3. With a little ingenuity, this can be extraordinarily useful for charting... and many other activities. And you should note that the number of areas specified has no hard limit, and those noncontiguous areas need not be of the same size!

More
All of this just scratches the surface of what is possible with this essential member of the Microsoft Excel functions. It can be used in countless situations, even to compare Excel worksheets, etc.
Some of the synonyms for the word, "imposing", in my thesaurus are impressive, august, commanding, effective, exciting, magnificent, and mind-blowing. The INDEX worksheet function is truly imposing!


International Versions of Excel
Throughout this article, I have used array constants within formulas.  This is a powerful technique.  In the English Language version of Excel, the symbol used to separate the columns in an array constant is the comma.  The symbol used to separate rows is the semicolon.
Other language versions of Excel use different symbols for column and row separation.  However it is easy to discover what symbols are used in your version of Excel.
Open the VBEditor (ALT-F11), and place the cursor in the Immediate Pane (CTRL-G).
Enter the following two lines in the the Immediate Pane (it's OK to use the clipboard):

?Application.International(xlColumnSeparator)
?Application.International(xlRowSeparator)

Click on each in turn and press ENTER on the keyboard.
Uses the printed characters to separate columns and rows in array constants.

The Venerable SUMPRODUCT

Mastering the venerable SUMPRODUCT function is a requirement for any Excel Hero.

It's one of the most useful in the roster of Microsoft Excel functions.

From my perspective, Excel would be a very different application if the SUMPRODUCT function had never been developed to the degree it is today. It's an amazing function that can be marshaled to solve a dizzying array of problems. It's an Array Function in that it operates on arrays, but it does not need to be entered with the Control-Shift-Enter key combination, and so it does not need the fancy parenthesis that adorn normal Array Formulas. However, it is a function that works on arrays, and is actually slightly quicker than an equivalent CSE Array Function.

One would never guess from the innocuous name the shear power and versatility embedded within the SUMPRODUCT function. Not only is it well worth your time to examine this function in detail, it is quite literally a prerequisite to honing your Excel abilities to the Excel Hero level. Rarely do I build a model that does not make extensive use of the SUMPRODUCT function.

From a maths perspective, SUMPRODUCT calculates the dot product of two vectors (arrays), but realize that it can be used with many more than two arrays.  A better name might have been the SumOfProducts function, as this in a nutshell is what it does.  Consider the following image:

sumproduct.png

Notice that Column D is the product of the three columns to its left. Cell D2 contains the formula =A2*B2*C2 and that formula is copied down as far as the columns go (Row 30 in this case). The entire purpose for Column D is to get intermediary values so that we can later sum them. Cell F4 shows this final calculation. For some spreadsheets this intermediary column approach is wanted for presentation purposes. However, sometimes all we want is the ultimate total, the sum of all those products. And that's exactly what the SUMPRODUCT function does. Notice the formula in the Formula Bar. It results in the same total.

There are some points to note here. 

Firstly, notice that the arrays I've used are the entire columns (A:A, etc.). I've done this on purpose to point out that doing so is a bad idea.  This was not even possible prior to Excel 2007 and would result in an error. Excel 2007 is happy to accommodate the request, but it puts a massive hit on the instance of Excel that workbook is running in. Always try to limit the number of cells that Microsoft Excel functions or formulas must process. A better formula would have been:

=SUMPRODUCT(A2:A30,B2:B30,C2:C30)

The SUMPRODUCT function multiplies elements of different arrays that hold the same position in those arrays and when done, sums all those products. This trivial example made this obvious by showing the function at work on three columns. But understand that those arrays could have just as easily been rows instead of columns, or they could have been rectangular ranges of any arbitrary dimension. They could have been array constants, i.e. {2,3,4,1,9,8} or Named Ranges, or the results of a boolean comparison (simple or complex), a custom mixture of many native Microsoft Excel functions, an Array Function, or even a User Defined Function. The one overriding requirement is that the arrays must be rectangular, contiguous, and of the same dimension. So if Array 1 is a column of 29 elements (as in the above image) then so must be all of the other ranges.

The next point is that this simple example had just three columns (arrays). The SUMPRODUCT function is just as happy with 1 array or up to 30 arrays, each separated by a comma. Thirty is the maximum number of arguments that Microsoft Excel functions can support. 

Ok, so SUMPRODUCT is adept at summing the products of equivalently placed elements in arrays. Why is that so powerful? Because it can be used in many non-obvious ways to calculate totals and mine data.

One way that it shines is calculating sums, counts, averages, and other metrics of list data based on criteria. The result here is basically analogous to a SQL database query that produces a total with a WHERE clause. For example the following SQL query: 

SELECT SUM(OrderPrice) FROM Orders WHERE Customer='Hansen'

could be emulated in Excel with any one of these SUMPRODUCT formulas:

=SUMPRODUCT((OrderPrice),--(Customer="Hansen"))
=SUMPRODUCT((OrderPrice),1*(Customer="Hansen"))
=SUMPRODUCT((OrderPrice),(Customer="Hansen")*1)
=SUMPRODUCT((OrderPrice),(Customer="Hansen")+0)
=SUMPRODUCT((OrderPrice),(Customer="Hansen")^1)
=SUMPRODUCT((OrderPrice),SIGN(Customer="Hansen"))
=SUMPRODUCT((OrderPrice),N(Customer="Hansen"))
=SUMPRODUCT((OrderPrice),(Customer="Hansen")*True)
=SUMPRODUCT((OrderPrice)*(Customer="Hansen"))

Some notes are in order here. 

The terms OrderPrice and Customer could be Named Ranges (they would basically be defined as columns in a list or table and would each be of the same length). Those Named Ranges (I actually prefer to refer to them as Named Formulas) could be dynamic ranges. Or conversely the terms could be replaced with direct cell references, such as C2:C1000 for OderPrice and B2:B1000 for Customer. Or the terms could be replaced by almost any combination of Microsoft Excel functions that returns an array. Please note that for some reason, SUMPRODUCT always fails when using an IF function on any of the terms inside the SUMPRODUCT, which is OK because we can use boolean logic to eliminate the need for branching. Please read I Heart IF for details.

The order of the terms is irrelevant. In any of the above examples, the answer is the same, whether the OrderPrice term or the Customer term comes first.

The first eight variations above all have two things in common. They use a comma to separate the terms, and each is employing a slightly different technique to coerce a lone boolean value (True or False) into their numeric equivalents, 1 or 0. If we fail to do this coercion on LONE boolean values, the SUMPRODUCT function always returns the value of 0. Of the eight different coercion methods, the double unary method (--) is the quickest. 

The ninth variation (the last one) above is the syntax I personally use most often. The entire calculation is done in the first argument so no commas are required and since the boolean term is being multiplied by the OderPrice term, there is no need to further coerce the boolean into a number. The multiplication has already done. 

This method is shortest in punctuation and therefore more concise. But it is imperceptibly slower. However, the trade off is greater flexibility. Since all of the work is being done in the first argument to the SUMPRODUCT function, there is no longer a limit of 30 terms. Using this syntax you can combine as many terms as you need. And most important of all, this method allows for more robust logic in the criteria. Specifically, we can specify OR clauses. For example the following SQL query: 

SELECT SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jones'

could be emulated in Excel with this SUMPRODUCT formula:

=SUMPRODUCT((OrderPrice) * ((Customer="Hansen")+(Customer="Jones")))

When combining the boolean terms (the ones that evaluate to True or False), they are easy to read if you remember that the plus symbol means OR, while the multiplication symbol means AND. Using this technique, you can tabulate very specific records indeed. For example, consider the following SQL query:

SELECT SUM(OrderPrice) FROM Orders WHERE (Customer='Hansen' OR Customer='Jones') AND State='CA' AND SalesDate>=#1/1/2010# And SalesDate<=#1/31/2010#

This query just says to total all of the orders in January of 2010 from California for the customers Hansen or Jones. It could be achieved in Excel using this SUMPRODUCT formula:

=SUMPRODUCT((OrderPrice) * ((Customer="Hansen")+(Customer="Jones")) * (State="CA")*(SalesDate>="1/1/2010") * (SalesDate<="1/31/2010"))

For me this is even more "readable" than the SQL database query. 

No matter how precise your criteria, which basically means no matter how many criteria terms you need to combine, the SUMPRODUCT function stands ready to tally the specified records, when used with my preferred syntax of combining everything into the first argument.

Another advantage (in my opinion) to using this syntax style is that it acts differently than the comma separated form when there is text in the summing array (i.e. OrderPrice in our examples) in any record returned by the criteria. Sometimes erroneous text can be in a column of data. The comma separated method will treat such text as a 0, combining everything in the first term will result in an error when the text is found in criteria specific records. I prefer to be notified with the error rather than silently calculating an incorrect result.


You'll notice that in all of these examples, we are summing the OrderPrice column. If instead we wanted to count the number of records that satisfied the criteria, all we need to do is drop the OrderPrice term from the formula. So if we wanted to know how many orders there were in January of 2010 from California for the customer Hansen or Jones, we would use this formula:

=SUMPRODUCT(((Customer="Hansen")+(Customer="Jones")) * (State="CA") * (SalesDate>="1/1/2010") * (SalesDate<="1/31/2010"))

Notice that the only difference here is that the OrderPrice term is gone. And finally if we wanted to know the Average amount of those orders we would just divide the total amount by the number of orders. This makes for a long formula, but now that you understand the syntax, it's easy to follow. This very specific average would be calculated with this formula:

=SUMPRODUCT((OrderPrice) * ((Customer="Hansen")+(Customer="Jones")) * (State="CA") * (SalesDate>="1/1/2010") * (SalesDate<="1/31/2010"))    /    SUMPRODUCT(((Customer="Hansen")+(Customer="Jones")) * (State="CA") * (SalesDate>="1/1/2010") * (SalesDate<="1/31/2010"))

It should be noted here that sometimes when the formulas grow this large, it can be helpful to name them (or parts of them) in the Name Manager.

These type of SUMPRODUCT calcs with multiple criteria can become very interesting when the individual terms are used with OFFSET or INDEX or INDIRECT or MOD/ROW. These enhancements go beyond the scope of this post, but they enable such refinements as considering only every nth record in the tabulations, changing the scope of the array, and many other fascinating feats.


Experienced users may point out that most of the above can be done by using the SUMIF or COUNTIF (or in Excel 2007 SUMIFS and COUNTIFS) functions. This is only partially true. The SUMIF and COUNTIF functions can only work with one criterion. The SUMIFS, COUNTIFS, and AVERAGEIFS function of Excel 2007 can work with up to 29 criteria, but they are limited to AND Logic between the criteria with no way to specify OR Logic.

Really experienced users might point out that all of the above can be done with Array Formulas. While this is true, the SUMPRODUCT function is optimized and is roughly 10% faster than an equivalent Array Formula, and there is no need for the Control-Shift-Enter formula entry.

All of this just scratches the surface of what the SUMPRODUCT function can accomplish. It can be used in countless situations that have nothing to do with database calculations. For example, if you wanted to know if the number in A1 was a prime number, you could use this non-Array Formula:

=SUMPRODUCT(1*(MOD(A1,ROW(INDIRECT("1:"&INT(A1^0.5))))=0))=1

SUMPRODUCT can be used to do a multi-column sort by formula.

When a SUMPRODUCT function is combined with Conditional Formatting some very interesting results can be achieved.

Another extremely important use of the SUMPRODUCT function which opens an entire universe of new possibilities is to use it to conduct bitwise logical operations such as Logical AND, OR, XOR, NOT, IMP, or EQ. This is the direct manipulation of the bits inside of an integer. There are countless uses of bit manipulation and this post can get you started.

Finally, amongst Excel gurus there is considerable debate about the best way to do database type calculations in Excel. While SUMPRODUCT as demonstrated in this post is very capable, there are alternatives. No one solution is the perfect answer to every situation. I have been conducting timing trials and will soon share detailed results for many different techniques. In the meantime it should be enough to know that SUMPRODUCT is perfectly fine when it is scanning several thousand records, even tens of thousands. However, if you are using it for hundreds of thousands of records than an alternative is likely in order.

Thứ Tư, 9 tháng 9, 2015

TA SẼ CẦN CHI VÀO LÚC CUỐI NGÀY

TA SẼ CẦN CHI VÀO LÚC CUỐI NGÀY


Khi những mệt mỏi in hằn lên khuôn mặt
Và nỗi cô đơn hiện nguyên hình nơi khóe mắt
Tâm hồn cạn khô, tiếng thở dài leo lắt…
Ta sẽ cần chi vào lúc cuối ngày?

Dòng thời gian đã tô màu trái tim những lớp bụi dày
Ta nhìn lại mình, đâu còn quá trẻ để sống với thơ ngây
Bình yên mong manh chẳng thể xoa dịu đi những nỗi đau
Tuổi hồn nhiên ngày nào giờ nát nhàu trong lo toan cơm áo
Cứ vội vã bước đi trên đường đời nhộn nhạo
Ta sẽ cần chi vào lúc cuối ngày?

Ta cần những gương mặt, cần những vòng tay
Một tiếng yêu thương, một câu săn sóc
Những cái ôm chặt làm vơi bao khó nhọc
Những tối bên nhau lặng lẽ ngắm sao trời
Những lời hỏi han khi đi lạc giữa dòng đời
Vài câu chân thành, đôi lời tâm sự
Ánh mắt hiền từ thấu suốt tâm can
Câu ca bản đàn trải lòng chia sẻ
Suy nghĩ chất chồng khi người ta trẻ
Hoàng hôn xuống dần, ngoài phố mưa bay
Câu hỏi vu vơ đọa đày tâm trí
Ta sẽ cần chi vào lúc cuối ngày…!

 - Le Duc - (st)

Thứ Sáu, 27 tháng 2, 2015

Một con voi ở trong phòng

Một con voi ở trong phòng

Nhìn qua bảng số liệu dưới đây (số liệu của World Bank 2013) chúng ta dễ dàng thấy sức mua tương đương (PPP) của VN đứng vào hàng thấp nhất, sau các nước Singapore, Mã Lai, Thái Lan, Nam Dương, Phi Luật Tân, và Brunei:



GDP (tỉ USD)
PPP (USD)
Singapore
297.9
78,763
Brunei
16.1
71,776
Mã Lai
313.2
23,338
Thái Lan
387.3
14,393
Nam Dương
868.3
9,561
Phi Luật Tân
272.1
6,536
Việt Nam
171.4
5,294

Là người Việt còn quan tâm đến đất nước, ai mà không chạnh lòng, thậm chí tức tối, trước tình hình trên. Chính Thủ tướng Chính phủ cũng từng đặt câu hỏi phải làm gì để không đứng cuối bảng? Do đó, câu hỏi đầu năm được đặt ra là VN phải làm gì để theo kịp các nước chung quanh. Các vị "thinkers" như ông Vũ Ngọc Hoàng thì nói “phải đổi mới, đổi mới là con đường duy nhất. Cần phải thoáng mở đầu óc, thoáng mở tư duy và quyết tâm cao trong hành động." Còn bà Khuất Thu Hồng nhấn mạnh thêm: "Nếu chúng ta không phát triển được, không tận dụng được tiềm năng, không tận dụng được cơ hội để phát triển lên rõ ràng là có tội với dân tộc" (1).

Tôi tự hỏi "dân tộc" là ai? Nói ra thì nghe có vẻ hay hay, nhưng trong thực tế câu nói đó không có ý nghĩa gì đáng chú ý. Còn nói chúng ta có tội với tiền nhân thì cần phải minh định "chúng ta" là ai. Ai làm cho đất nước này nghèo? Chắc chắn không phải "nhân dân", mà phải là những người có quyền thế và lèo lái con thuyền quốc gia. Nếu nhận "có tội với tiền nhân" rồi sao nữa? Chẳng lẽ chỉ nói suông nhận tội là xong sao? Đúng là cách nói rhetoric, nói mà không nói.

Chỉ có Tiến sĩ Nguyễn Quang A là nói thẳng và dễ hiểu. Ông nói là cần phải dẹp bỏ vai trò chủ đạo của các tập đoàn thuộc Nhà nước và giúp nền kinh tế sở hữu tư nhân: "Cái thứ nhất là phải triệt để xóa bỏ đường lối sai lầm của Đảng CSVN là: Kinh tế Nhà nước giữ vai trò chủ đạo. Trên cơ sở đó có thể vạch ra các chính sách kinh tế thúc đẩy cho sự phát triển của khu vực kinh tế Tư nhân trong nước và tạo điều kiện cho chúng hoạt động ngang ngửa, sòng phẳng và bình đẳng với các thành phần kinh tế khác" (2). Ông nói thêm: “Nền Kinh tế thuộc sở hữu tư nhân phải đóng vai trò trung tâm, dẫn dắt và vai trò chủ đạo trong nền kinh tế. Chỉ có nền kinh tế tư nhân nó mới có những động lực để thúc đẩy nền kinh tế chung phát triển. Kinh tế Nhà nước không thể giữ vai trò chủ đạo trong một nền kinh tế được, nó chỉ giữ một vai trò có tính chất phù trợ nào đấy mà thôi. Tôi nghĩ biện pháp quan trọng nhất là phải xóa bỏ tất cả những cái ưu ái, trước hết là các ưu ái đối với DN Nhà nước, thậm chí cả những ưu ái đối với DN Đầu tư nước ngoài. Tôi không nghĩ các DN Tư nhân cần có sự hướng dẫn hay hỗ trợ của Nhà nước, tôi tin họ sẽ tự lo lấy của họ. Song cái quan trọng là phải để cho nó có một môi trường thông thoáng và không bị ai chèn ép.”

Đồng ý với TS Quang A. Người phương Tây có câu an elephant in the room (một con voi ở trong phòng) để chỉ một vấn đề / giải pháp mà ai cũng thấy nhưng chẳng ai muốn/dám nói đến. (Cũng như con voi nó rất bự ở trong phòng, nhưng không ai dám nói đến nó mà chỉ nói xa gần, bóng bẩy). Hai vị thinkers kia thừa biết và thấy "con voi" làm cho VN nghèo nàn và tụt hậu, nhưng không dám nói đến nó, chỉ có TS Quang A nói thẳng và dễ hiểu. Chứng kiến sai trái mà không nói cũng là có tội vậy.  

====

http://tuanvannguyen.blogspot.com/2015/02/mot-con-voi-o-trong-phong.html

Chủ Nhật, 22 tháng 2, 2015

Plan for year 2015

- New swimming skill;
- Get 1 year experience for new duty in the department;
- Chief accountant certification;
- Improve writing E skill;
- Find another position;
- Find a girl friend.

Nước Đức như và không như tôi nghĩ...

Nước Đức như và không như tôi nghĩ...

Huy Nam
Thứ Sáu,  20/2/2015, 23:47 (GMT+7)
Phóng to 

Thu nhỏ 

Add to Favorites 

In bài 

Gửi cho bạn bè
Di tích dinh Hoàng Gia (Kaiserpfalz) tại cố đô Keiserswerth. Ảnh: HUY NAM

(TBKTSG) - Dù có thời gian dài tiếp xúc và làm việc nhiều với người Đức, tôi thường nghĩ Đức cũng giống như các xứ châu Âu khác, đều là Tây cả... Chỉ sau lần thứ ba sang làm việc lâu hơn tại nước này, có điều kiện tiếp cận sâu vào đời sống của họ, tôi mới hiểu thêm được nhiều điều thú vị.
Đi làm sớm, và gắn bó với công việc...
Cứ nghĩ dân Âu Mỹ ăn trễ, ngủ muộn, sáng ra đi làm muộn. Nhưng không, trong thời gian làm việc tại Haltern (gần Dusseldorf) vào giữa năm 2014, tôi có chút bất ngờ khi nghe nhân viên ở đây bắt đầu làm việc từ 7 giờ sáng. Điều này có nghĩa họ phải thức rất sớm để đến sở, bởi có khi mất cả tiếng đồng hồ lái xe.
Tuy là nước công nghiệp hàng đầu thế giới, Đức có diện tích đất nông nghiệp, mặt nước và rừng chiếm gần 85% (lần lượt là 53,5%; 1,8%; và 29,5%), nhưng số dân làm canh nông thì chỉ hơn 2%. Có lẽ do vậy nên giờ làm việc của doanh nghiệp tại các vùng nông thôn thường bắt đầu sớm để nhân viên chiều về có thể tranh thủ phụ gia đình chăn nuôi, canh tác.
Ta cũng có thể đã quen nghe dân Tây thường di chuyển, hay thay đổi việc làm và nơi làm việc, ít khi ở lâu một chỗ. Điều này lại không hẳn đúng với Đức. Ở Đức yếu tố quần cư theo gia đình là khá cao và việc sở hữu một căn nhà vẫn là điều mơ ước. Sự ổn định đối với họ là tiêu chí sống ưu tiên. Chính vì vậy mà họ cần cù “cày” từ lúc rời ghế nhà trường, và thường gắn lâu dài với nơi làm việc, ít bay nhảy. Số nhân viên bám trụ ba, bốn chục năm hay hơn tại cùng một doanh nghiệp ở Đức rất phổ biến. Tại công ty nơi tôi đến làm việc, cả nhân viên và lãnh đạo đều hãnh diện về điều này và cho rằng doanh nghiệp của họ chẳng khác của người Nhật...
Ai cũng biết máy móc thiết bị Đức tốt, tinh thần Đức bền bỉ. Nhưng có lẽ phải cần đi thăm Bảo tàng Krupp ở Essen thì mới tận mắt thấy đâu là cội rễ của kỹ thuật Đức và tại sao ý chí Đức là “thép”. Ngày nay, Đức là nước giàu nhất châu Âu, mạnh hàng đầu thế giới, nhưng đường đến giàu có của họ đã không dễ dàng. Thật vậy, đến những năm 1950 Đức vẫn còn rất khó khăn. Đó là một đất nước bị chia cắt, hoang tàn sau Thế chiến thứ 2, nhiều đô thị bị san bằng.
Ý chí quật cường
Riêng tại Cologne, chỉ có 32.000 người sống sót từ con số 750.000. Phần lớn trai tráng từ 17-35 tuổi hoặc đã thiệt mạng hoặc bị thương tật. Cả nước Đức có trên 20% hộ thiếu nhà, tiêu chuẩn ăn của dân chỉ còn 1.000-1.500 calo/ngày, và họ phải mất trung bình 9,5 giờ làm việc mỗi tuần để “xếp hàng mua gạo”. Chưa nói thiên tai bão lụt cũng hoành hành xứ này chẳng kém Việt Nam. Nước Đức và người dân Đức lúc ấy đã cơ cực như ở mình một thời...
Để cứu vãn nền kinh tế rệu rã lúc đó, Ludwig Erhard đã xây dựng một kế hoạch cải cách “tiền tệ, tài chính, thuế” táo bạo, gây tranh cãi sóng gió. Nhưng nhờ sự ủng hộ và hưởng ứng nồng nhiệt của người dân, nền kinh tế Đức đã được vực dậy diệu kỳ rồi thăng hoa nhanh chóng, làm nên phép màu Đức. (German economic miracle)
Rất may, Đức đã có những cái đầu kinh tế tốt như Walter Eucken. May mắn hơn, vào lúc cùng cực đó đã nổi lên một Ludwig Erhard thức thời và quyết đoán, mở ra cơ hội làm nên phép màu Đức (German economic miracle). Để cứu vãn nền kinh tế rệu rã lúc đó, Erhard đã xây dựng một kế hoạch cải cách “tiền tệ, tài chính, thuế” táo bạo, gây tranh cãi sóng gió trong nội bộ và suýt bị các “thầy dùi” bảo thủ ngáng cẳng. Nhưng nhờ sự ủng hộ và hưởng ứng nồng nhiệt của người dân, nền kinh tế Đức đã được vực dậy diệu kỳ rồi thăng hoa nhanh chóng. Quá trình này thể hiện sự kiên trì, tận tụy, kỷ luật của dân Đức, và họ đã được đền bù.
Bên cạnh nỗ lực xoay chuyển đem lại thịnh vượng từ đổ nát như vậy, không ít nơi tươi đẹp ngày nay cũng đã lột xác từ các công trường hầm mỏ đầy khói bụi. Ta có thể bất ngờ về sự đổi thay này nếu có dịp đi thăm khu nhàn du và trung tâm nghệ thuật Gasometer ở Oberhausen, hay cảnh trí nên thơ dọc hai bờ sông Lippe vùng Westphalia...
Đức là đất nước có sự kết hợp hài hòa giữa công nghiệp hiện đại và thiên nhiên tươi đẹp. Ai đã qua lại nhiều nước châu Âu chắc biết làng xóm ở Đức trù phú và xanh tươi hơn một số nước lân cận. Lên miền Bắc và so với Hà Lan sẽ càng rõ hơn, bởi Đức không có quá trình chặt cây đóng tàu đi chinh phục. Nhưng sự gìn giữ các giá trị truyền thống cộng đồng thôn làng ở Đức mới đáng nói...
Coi trọng tình thân
Khi được Joerg, một giám đốc điều hành tại Haltern, đưa đi thăm khu làng nhà anh, tôi thầm phục tình làng xóm ở đây qua lời kể về bữa ăn sáng chung cả làng mỗi năm. Nghe đâu cộng đồng người Đức tại các nước khác (như Úc) cũng còn giữ tập quán sinh hoạt này. Làng là vậy, còn gia đình thì sao? Phải sống mới biết... Dịp nọ, được một người bạn Đức khá thân khác có nhà ở Hattinggen mời về nghỉ lại cuối tuần, tôi đã ngộ thêm rằng tình cảm gia đình của người Đức thật ra không quá Tây như mình nghĩ: họ cũng bắt con lễ phép chào khách như ta, người mẹ cũng thường học cùng con, cũng sốt ruột chờ cửa đêm mỗi khi con về muộn...
Joerg cũng đã kể tôi nghe chuyện nhà mình. Hai hôm trước ngày đưa tôi đi thăm Gasometer, con trai anh có tiệc liên hoan ở một nông trang xa và vì đêm khuya sợ khó đón taxi, anh đã lái xe đi đón con về. Con anh đã hơn 30 tuổi, đã có vợ có con, nhưng với anh nó vẫn cứ là thằng nhỏ! Quá 12 giờ hôm đó lại là Father’s Day ở Đức, hai cha con lại rủ nhau đi uống mừng...
Father’s Day ở Đức thật ra là ngày của đàn ông. Theo tập tục, dịp này đàn ông có “quyền” bỏ bê gia đình, chất đầy trên các xe rờ-moóc để lông nhông uống bia và hò hét... Lượng bia tiêu thụ trong dịp này thường vượt xa Oktoberfest. Nhắc đến Oktoberfest mà quên nhà nấu bia Hofbrauhaus là thiếu sót, do cả hai đều có chung quê Munich. Ai đến Munich mà chưa thử bia hảo hạng tại Hofbrauhaus thì có thể sẽ tiếc... Munich tuy bị tàn phá nặng nề sau Thế chiến thứ 2, nay lại là thành phố xinh đẹp và phồn thịnh nhất châu Âu với các công viên nổi tiếng thế giới, như Englische Garten (Vườn Anh) hay công viên lâu đài Nymphenburg.
Xem đá bóng, dù chỉ thoáng qua, ta đã biết ý chí Đức qua khát khao chiến thắng và bản lĩnh lội ngược dòng. Trong kinh tế và đời sống, khát khao và bản lĩnh ấy bền bỉ hơn nhiều. Đó là sự hun đúc từ đức tính tận tụy, kỷ luật, từ tinh thần không buông xuôi, vội tự mãn, dễ bỏ cuộc... Tất cả được nuôi dưỡng bởi những giá trị cao đẹp tạo nên tính cách hay cốt cách con người, được gọi là phẩm giá (dignity). Phẩm giá tuy cũng chuẩn mực, nhưng không là học thuyết hay cứng như chủ thuyết và không riêng người Đức mới có, cho dù đã có khái niệm germanism.
Nhờ có thời gian làm việc chung và đi lại nhiều nên tôi may mắn có chút cảm nhận gần và sâu hơn về đất nước này. Với tôi, nước Đức mà tôi thấy ngày nay không như nước Đức tôi được kể lại, người Đức mà tôi nghe kể lại cũng khác với người Đức tôi gặp trên đường...