You are on page 1of 7

DSUM() function –The Art of Writing a Criteria : 3

Posted on January 2, 2014 | 10 Comments


In this post, you’ll learn to work with DSUM() function with date as a criteria. It is very similar to the
way you create the text criteria with the only exception being the delimiters used. In case of dates,
you’ll use hash (#) as a delimiter instead of a single quote (‘ ‘) that you use in case of text values.

The date criteria enables you to extract data within a particular time period or before/after a certain
date as explained in this post…

Work Scenario
You are analysing the inventory data of your company and you want to know the following details:

 Scenario 1 : The total inventory sold on a particular date (say 03/22/2006)


 Scenario 2 : The total inventory sold across different days.
You want to solve the above two scenarios using DSUM() function. Now, that leaves you thinking..

DSUM function with Date Criteria


Since you are working on inventory, you’ll access the Inventory Transactions table from the
Northwind database.
In this table, you would require the following fields:  

 Transaction Date – This field indicates the date on which the inventory transaction took place.
 Quantity – This field indicates the total quantity of inventory being handled.
 Transaction Type – This field indicates whether the inventory was sold, purchased or whether it was
on-hold.
Before you start creating any query, it is always a good practice to go through the table and its fields
that you’ll be analysing and have a look at the type of the data that they are holding.

In the above mentioned table “Inventory Transaction”, though the [Transaction Modified Date]
appears to be of the format mm/dd/yyyy, it is actually of the format mm/dd/yyyy hh:mm:ss
AM/PM.

Now, since you want to know the total inventory sold on a particular date, the time component is
irrelevant to your analysis. So, you will have to disregard the time  by using the following functions:

 format(“[]”, “mm/dd/yyyy”). But the output will be a a string of the format “mm/dd/yyyy” and you
need a date format.
 Hence you’ll further use the cdate function over the format function: 
cdate( format(“[]”, “mm/dd/yyyy”))
So, lets create a new query, to get the desired result as explained above:
On running this query, you get the following output:

Now that you have the dataset in the desired format, you can start working on the scenarios:

Scenario 1
In this scenario, you know outright that you need the result for one specific date only.

1. So, from the from the given dataset, you might want to filter the inventory transactions of that
particular date. (You might see the benefit of this step if you are dealing with thousands of records  of
inventory transactions across hundreds of different dates.)
2. Then you group all the transactions on the required date (03/22/2006 in this case) and apply a dsum()
function to sum the field [Quantity] based on the criteria of date as well as transaction type (i.e.
inventory that was sold).
The criteria for DSUM() function will be written as follows:

The query looks like as follows:

And the output is :


Other possible conditions for dates are as follows:

Description Date Conditions


Before a particular date ([Mdate] < #03/22/2006#)
After a particular ([Mdate]> #03/22/2006#)
On or before a particular date ([Mdate]<= #03/22/2006#)
On or After a particular date ([Mdate]>= #03/22/2006#)
Between two dates :  
Inv: DSum(“[Quantity]”,”dsumdata”,”[Mdate] between #04/03/2006# and
Both Dates Included #04/04/2006# AND [Transaction Type]=2″)
Inv: DSum(“[Quantity]”,”dsumdata”,”[Mdate] >#04/03/2006# and [Mdate] <
Both Dates Excluded #04/04/2006# AND [Transaction Type]=2″)

Scenario 2
In order to find the total quantities across all the dates, you’ll provide a direct reference to the field
[Mdate] containing the date values while constructing the DSUM() function. The criteria in the
dsum() function will be written as follows:

You’ll construct the query exactly the same way you did in scenario 1 except that in this scenario you
would be referencing to the field instead of specifying a direct value:

The query for scenario 2 would look like :


And on running this query, the output is :

In this way, you can get the desired result as required in scenario 2.

Summary
So, you  have seen how a DSUM() function can be created and more importantly how a criteria can
be constructed based on the following three data types:

1. Text  – DSUM() function –The Art of Writing a Criteria : 1


2. Number – DSUM() function –The Art of Writing a Criteria : 2
3. Date – DSUM() function –The Art of Writing a Criteria : 3
Just to refresh your memory, the criteria for all the three data types can be constructed as follows:

Also, in this post you have learnt the following two functions:

1. Format () – This function converts values in specified string format


2. Cdate() – This function converts values into dates provided the value passed as an argument is a valid
date expression.
It is recommended that you read, practice and use these functions and share with us any interesting
results.

You might also like