Calculating Dates in SAP NetWeaver MDM
Applies to
Master Data Management 5.5 SP05
Summary
This article contains an assortment of tips and examples of calculated expressions within SAP NetWeaver
MDM which are related to dates but may be applied to other types of expressions.
Author: Kristin Patterson
Company: SAP America
Created on: 15 May 2007
Author Bio
Kristin Patterson is based in SAP America’s Palo Alto office in Northern California. She has been working
with SAP NetWeaver MDM since January 2006 at numerous customer sites. With SAP NetWeaver MDM
Development, Ms. Patterson has also been testing the newest MDM releases. Most recently she has been
designing and delivering MDM training course for customers.
Table of Contents
Introduction ...................................................................................................................................... 3
Comparison of Calculated Field and Assignment ........................................................................... 4
Basics Tips....................................................................................................................................... 4
Different Field Types........................................................................................................................ 5
Which Field Type to Use.................................................................................................................. 6
Literal Date Field ...................................................................................................................... 6
Text Field .................................................................................................................................. 6
Integer and Real Fields ............................................................................................................ 7
Ways of Entering Dates ................................................................................................................... 8
System Time .................................................................................................................................... 9
Calculating a Date ........................................................................................................................... 9
Calculating a Numerical Value....................................................................................................... 10
Related Content............................................................................................................................. 11
Copyright........................................................................................................................................ 12
Introduction
SAP NetWeaver MDM has the capability to perform calculations on date fields in the MDM repository thereby
producing either a date or a value. The calculations can be performed when a new record is created either
manually, during import, or using APis by creating a calculated field. Another option is to apply a calculation
to a specific field using an assignment.
Both calculations and assignments use the expression editor tool (see Figure 1) which allows excel like
expressions to be used to create values and conditional statements. The expression editor tool is used
exclusively to create Calculated Fields, Assignments, and Validations. Each of these is described in great
detail in the MDM Console and Data Manager Reference Guides. It is assumed for the purpose of this article
that all readers have reviewed and are familiar with these guides’ references to the expression editor.
Figure 1: Expression editor dialog box for creating formulas.
Comparison of Calculated Field, Assignment, and Validation
Let’s begin by comparing a calculated field with an assignment and validation of a specific field.
Calculation Types
Calculated Field
Assignment
Validation
Formula Creation
Console
Data Manager
Data Manager
Repository Loaded
No
Yes
Yes
Read Only
Yes
No
No
Field Types Applied
to
Boolean, GM Time, Integer,
Literal Date, Literal Time,
Real, Text, and Text Large.
Boolean, GM Time, Integer,
Literal Date, Literal Time,
Real, Text, and Text Large.
Can be called by a
calculated field or an
assignment.
Output
Value
Value
True or False
As a quick review the Calculated Fields and Assignments will produce a single value. Whereas a Validation
creates a true or false response and can be called by other expressions and thereby be used in a Free-Form
Search. For the purpose of this article, the concentration will be on assignments using date values. The
reason for using assignments instead of a calculated field is described under Basic Tips #2. The article will
describe both the fields that support date formats and the functions that can be used in conjunction with date
values.
Basic Tips
As with all tasks, there are many ways to do things, but the following list comes from past experience and is
highly recommended.
1. When using the expression editor never type in a field or function if it is available in the expression
editor pull down menus. This will eliminate typing errors and the possibility of the editor
misinterpreting the textual characters.
2. Calculated fields can only be created and edited when the repository is unloaded. Thereby making it
impossible to test your formula as the repository must be loaded. So when creating a calculated field
use assignments to test the expression and only after the assignment works paste the expression
into a calculated field. The copy and paste function does work between expression editor windows.
3. Carriage returns can be used anywhere in the expression editor without having any effect on the
formula. Using returns can improve the legibility of the final formula.
4. If a text field is used, make sure that the field width is set large enough to hold all the characters that
will be created by the formulas. If the width is too small, the formula will not execute.
Disclaimer
Great effort went into verifying each formula before describing it here. But since it is not possible to copy a
working expression from the expression editor to a text editor, the formulas in this article had to be manually
retyped. There is the possibility that the formulas may have a typing error.
Different Field Types
SAP NetWeaver MDM comes with 8 predefined field types that can display date and/or time formats or
formulas. The field type selected may be based on the display format required. The following list shows the
field types and examples of their display for the date May 6, 2007 at 9:43:04:000 am PDT using a 24 hour
format.
Field Type
Description
Display Format
Create Stamp
Write Once field which stores the creation date of the record.
05/06/2007 09:43:04
GM Time
Date and time displayed with GMT at the end. Can not be
populated manually in the Data Manager but can be populated
as a calculated field or with an assignment.
05/06/2007 16:43:04 GMT
Literal Date
Displays only the date in a preformatted field that shows the
month, date, and year using a “/” separator.
05/06/2007
Literal Time
Displays only the time in a preformatted field that shows the
hour, minutes, and seconds using a “:” separator.
09:43:04
Text
Static text with “:” separators. The date is now a string and
allows for alphanumeric sorting.
2007:05:06:09:43:04:000
Time Stamp
This is a Read Only field that is automatically populated with
the date and time a specified field or fields is changed. This
field cannot be manually written to.
05/06/2007 09:43:04
Integer
May be used when whole numbers are requested. The number
is not rounded.
148,413
Real
This field will display the numeric equivalent of the date field to
a maximum of 6 decimal places but will store the entire value.
Which Field Type to Use
The data and what will be done with the data will ultimately determine what type of field to use. Another
important factor is how the data will be inputted into the repository either through import, manual entry, or
use of APIs. These different ways will be discussed in the next section and will be demonstrated with real
examples for each of the cases.
Let’s go into more detail of the most commonly used field types: Literal Date, Text, and Integer/Real. Then in
later sections examples of literal, text, and numeric fields will be used. In many cases, the same functions
may be used in multiple field types and is not limited to just the one introduced here.
Literal Date Field
) that must be followed when
The Literal Date field provides a predefined formatted layout (
entering data. It also does not allow month entries over 12 nor date entries over the number allowed for that
specific month and year. This is ideal for controlling manual entry. The Literal Date fields work well with
calculating dates for both the past and future. If the time is also important, then a Literal Time field should
also be created.
Text Field
A text field can contain text, numbers, and special characters. If a formatted date is imported into a text field
it will now be a text string and can be manipulated with the expression editor’s string functions. The string
functions reside under the pull down Functions tab of the expression editor dialog box. Shown below is a list
of those string functions.
Pull Down
¦
Function
Description
Formula (F_name is the name of a field F)
Systime
Returns the system time
SYSTIME(x/24)
X is the number of hours before GMT
Modulus
Returns the remainder after
a number is divided by a
divisor
MOD(F_name, d)
F_name is the name of a field F representing a number
d is the number by which you want to divide F
String Length
Returns the number of
characters in a text string
LEN(F_name)
String Left
Returns the specified
number of characters from
the start of a string
LEFT(F_name, n)
n specifies how many characters to return from F
String Right
Returns the specified
number of characters from
the end of a string
RIGHT(F_name, n)
n specifies how many characters to return from F
String Mid
Returns the characters from
the middle of a string, given
a starting position and a
given length
MID(F_name, p, l)
p specifies the position of the first character to extract
from F_name
l specifies how many characters to return
String Find
Returns the starting position
of a specified string within a
string given
FIND(F_name, s, p)
s specifies the characters to search within the string F
p specifies the position of the character to start the
search within the string F
String Trim All
Returns the specified string
without spaces
TRIM_ALL(“F_name”)
String Trim Left
Returns the specified string
without the leading spaces
TRIM_LEFT(“F_name”)
String Trim Right
Returns the specified string
without the ending spaces
Integer and Real Fields
A Real or Integer Field can only contain numbers and is an ideal field for calculating differences in dates and
time. If the difference should be displayed with character separators then the field must be text. Shown below
are commonly used operators available in the expression editor dialog box.
Pull Down
Function
Description
Formula (F1_name is the name of a field F1)
+
Adds two numerical fields
F1_name + F2_name
both fields representing numbers
-
Subtracts two numerical fields
F1_name - F2_name
both fields representing numbers
*
Multiply two numerical fields
F1_name * F2_name
both fields representing numbers
/
Divide two numerical fields
F1_name / F2_name
both fields representing numbers
&
Concatenate the specified strings
S1 & S2
S1 and S2 are text string
()
Separate expressions or operations
““
Surrounds a text string of characters
=
Checks that two values are equals
F1_name = F2_name
<>
Checks that two values are different
F1_name <> F2_name
>
Checks that a value is strictly greater
than another value
Ways of Entering Dates
There are three ways to enter data into a MDM repository: manual, import, and using APIs. The application
programming interface (API) which is available in both Java and ABAP will not be discussed in this article,
but many of the points made here may be of value when using APIs. Manual entry of data is the least
preferred way of entering data since it allows for human errors to be replicated. Where as the importing of
prescreened data, provides more accurate data integrity. This article will primarily concentrate on importing
of data but will also discuss the manual entry when this information is pertinent.
SAP NetWeaver MDM can import data from many types of files: flat files, excel, access, XML, SQL, Oracle,
and ODBC. The different types of import files may be interpreted differently by the Import Manager (IM). As
an example, if the imported source data file is excel and a date is kept in a MM/DD/YYYY format then only a
Literal Date field can be used. When importing the same date from XML then Literal Date or Text Field can
be used. The reason for this is that the IM is using Microsoft’s parser to interpret the data and when the data
is imported from Excel it recognizes the MM/DD/YYYY (and D-mmm) format as a date and will only allow
import into a Literal Date field.
Dates can be imported into MDM as MM/DD/YYYY, YYYYMMDD, or DD.MM.YYYY. The date format is set
under the IM’s Configuration > Options…
This setting applies to all dates being imported into Literal Date fields. If you need to use different date
formats for different fields then separate imports must be performed.
System Time
One of the most recently added functions in the expression editor is the System Time function which gives
the current date and time to the millisecond. The function is SYSTIME(x) where x is the time zone. If
SYSTIME(0) then it is the current Greenwich Mean Time (GMT). Hours can be added by putting in a fraction
of 24. So SYSTIME(8/24) is equal to GMT + 08:00 (Beijing, Singapore, and Taipei.) If you want to go
backwards in time then enter SYSTIME(0-8/24) which is equal to GMT – 08:00 (PST.) Note that this is
Pacific Standard Time and not Pacific Daylight Savings Time. Make the appropriate adjustments to reflect
Daylight Savings Time if that is what is desired. Also partial hours can be added by entering
SYSTIME(2.5/24) for GMT + 02:30.
If only the date is required, then populate a Literal Date field. If only time is required then use a Literal Time
field. Table below is using SYSTIME(0) = May 1, 2007 4:31:44 GMT.
Field Type Populated
Formula
Result
Details
Literal Date
SYSTIME(0)
05/07/2007
Literal Time
SYSTIME(0)
4:31:44 AM
GM Time
SYSTIME(0)
05/07/2007 4:31:44 GMT
Integer
SYSTIME(0)
148414
Does not round number.
Real w/6 decimals
SYSTIME(0)
148414.188705
Displays the first 6 decimals but stores
148414.188704525.
Text
SYSTIME(0)
2007:05:07:04:31:44:000
Field width had to be set to 23 or more.
Calculating a Date
There are requirements to determine a date in the future, in the past, or calculated based on specific criteria.
For example, what date is 10 days after the hire date? Table below is using the Literal Date field called Hire
Date with the value of May 1, 2007.
Field Type Populated
Not a valuable display for most
people but useful for calculations.
BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com
9
Calculating Dates in SAP NetWeaver MDM
Calculating a Numerical Value
In this example, the days a person has worked for the company will be calculated. The repository has the
actual hire date of all employees. The table below is using Hire Date (Literal Date) = May 1, 2007 (unless
otherwise stated) and the System Time = May 7, 2007 4:31:44 GMT.
Request
Field Type
Populated
Formula
Result
Details
Number of days
since hire date
Integer1
SYSTIME(0) – Hire Date
6
Does not
number.
Number of days
since hire date
Real
w/6
decimals
Same as above
6.188705
Displays the first 6
decimals but stores
6.1887045254698.
Number of days
since hire date
Text1
Same as above
6.1887045254698
Number of days
since hire date using
string functions
Integer2
(Left(SYSTIME(0) – Hire Date,
FIND(SYSTIME(0)-Hire
Date,”.”) -1)
6
Does not round
number.
Number of days
since hire date with
“days”
Text2
(Left(SYSTIME(0) – Hire Date,
FIND(SYSTIME(0)-Hire
Date,”.”) -1) & “ days”
6 days
Combination of
integer and text.
Is it the hire date
anniversary?
Text3
IF((MOD(SYSTIME(0),6,2) –
MID(Hire Date,6,2))=0,
(IF((MID(SYSTIME(0),9,2) –
MID(Hire Date,9,2))=0,
“Anniversary Day!!”, “Day is
not equal”)),
(IF((MID(SYSTIME(0),9,2) –
MID(Hire Date,9,2))=0, “Month
is not equal”, “Month and Day
are not equal”)))