IBM Query Management Facility (QMF) is a powerful tool for querying, reporting, and visualizing data. However, its true potential is unlocked when you can extend its built-in functionality with custom JavaScript functions. These custom functions enable you to perform advanced calculations, manipulate data, and make your reports more interactive and dynamic. In this blog, we will explore how you can add custom functions to QMF and provide examples of how to leverage them effectively.
Why Use Custom Functions in QMF?
Custom functions allow you to extend QMF’s capabilities beyond the standard options. Whether you need to:
- Perform complex calculations
- Format data in a specific way
- Apply custom filters or business logic
You can achieve it all by adding your own JavaScript functions. These functions can be used in calculated columns, dynamarts, or anywhere you need custom processing in your reports.
How to Add Custom Functions in QMF
Before we dive into examples, here’s a quick overview of the steps to add your custom JavaScript functions:
- Create a JavaScript File: Write your functions in a file called functions.js.
- Define Your Functions: Each function should start with the function keyword.
- Categorize Functions (Optional): Use comments to categorize and describe your function.
- Place the File in the User Directory: Save the functions.js file in the user home directory within QMF.
- Restart QMF: Restart QMF to load the new functions.
- Define Categories for Your Functions: Functions will be organized by the category you assign to them. For example, if you assign the category Conversion to a function, it will appear under the “Conversion” category in QMF. This makes it easy to find and use your functions in reports and queries.
Example: Calculate Age from Date of Birth
Let’s say you have a DOB (Date of Birth) field, and you need to calculate the age of individuals. Instead of manually calculating the age each time, you can write a custom function to do it for you.
JavaScript Code:
/**
* @category Conversion
* Calculates age from Date of Birth
*/
function calculateAge(dob) {
var birthDate = new Date(dob);
var ageDifMs = Date.now() - birthDate.getTime();
var ageDate = new Date(ageDifMs); // milliseconds from epoch
var age = Math.abs(ageDate.getUTCFullYear() - 1970);
return age;
}
|
How to Use:
Once this function is added to your functions.js file, you can use it like this in a calculated column:
This will return the age of everyone based on their DOB, saving you time and ensuring accuracy in your reports.
Example Data:
Name
|
DOB
|
Alice
|
1990-04-21
|
Bob
|
1985-12-10
|
When using:
Age = calculateAge(@[DOB])
QMF will:
- Call calculateAge('1990-04-21') for Alice
- Call calculateAge('1985-12-10') for Bob
Resulting in:
Name
|
DOB
|
Age
|
Alice
|
1990-04-21
|
35
|
Bob
|
1985-12-10
|
39
|
(Ages may vary depending on the current date.)
Please find below attached images for reference in QMF
Conclusion
Adding custom functions to IBM QMF can significantly enhance the power of your reports, allowing you to implement complex logic, automate calculations, and present your data in a more meaningful way. With the ability to define custom JavaScript functions, QMF becomes an even more versatile tool for business intelligence and data analysis.
In this blog, we demonstrated how to create and use custom functions for age calculation, By following this example and adapting it to your own needs, you can start unlocking the full potential of QMF.
Looking Ahead
Latest available release of IBM QMF 13.1.2 reaffirms our commitment to delivering the tools one need to efficiently manage and analyze data in an increasingly complex and hybrid data landscape. We will continue to enhance QMF to support customer needs, whether they're working with relational databases, cloud-based NoSQL solutions, or a mix of both.
To explore the powerful new features of QMF 13.1.2, we encourage you to refer to the following resources for additional information and support:
IBM documentation
IBM Program Directory
QMF Product Support page
What’s new in QMF 13.1.2
To try out these new features, upgrade now to QMF 13.1.2.
Thank you for your continued support. Happy reading!
#Db2QMF #QMF #Db2Tools #IBMDb2forz/OS