Skip to main content
hic
Former Employee
Former Employee

My latest blog post was on joins in QlikView (see To Join or not to Join). In it I claimed that you should avoid making joins in the QlikView script, if possible. This blog post is about a function that can help you avoid joins. It is about the function Applymap().

It is, in fact, one of my favorite functions and I do not seem to be alone in this choice. Several of the QlikView developers who regularly write on QlikCommunity seem to share this preference and have written about this function, e.g., QlikView Notes, Quick Intelligence and QVApps.

 

So, what does the function do? Well, basically it is just a lookup function – it takes one value as input, checks if this value exists in a mapping table and returns the corresponding value from the second column in the mapping table. Think of it as a translation function where the translation is fetched from a pre-defined, two-column table. A simple translation function, but you can still do a lot with it...

 

What has this got to do with joins? Everything. A very common situation in data modeling is that you need to get a single field value (per record) from a different table than the current one. Then the standard way to do this is to use a join. However, in QlikView you can – and should – use Applymap() instead. The only time that you cannot use Applymap() instead of a join is if you want to fetch more than one single corresponding value (record) from the second table.

 

Let's look at an example of how to use Applymap(): In an ERP system, the customer table is the table where all information about the customer is stored, e.g. the name and country of the customer. Further, it could be that you need the customer’s country when making a calculation in the order table. This is a field that you don't have in the order table. In SQL you would join the two tables to get country into the order table.

 

In the QlikView script you would instead first define the mapping table that maps a customer ID to a customer country as follows:

 

     MapCustomerIDtoCountry:
     Mapping Load CustomerID, Country From Customers ;

 

The second step is to use this information when reading the order table, such as:

 

     Orders:
     Load *,

          ApplyMap('MapCustomerIDtoCountry', CustomerID, null()) as Country
          From Orders ;

Tables2.png

 

The "null()" as third parameter of the function defines what it should return when the customer ID isn't found in the mapping table.

 

And with this, you have joined the field Country with the order table – without using a join. And you have done it faster and with less chance of errors. Bottom line: Whenever you know that you just want to fetch one single value per record – don’t join. Use Applymap() instead.

 

HIC

 

See more in the Technical Brief on Joins and Lookups.

88 Comments
kalyandg
Partner - Creator III
Partner - Creator III

hi Henric,

Now it is working, thanks

0 Likes
14,879 Views
Anonymous
Not applicable

I absolutely love applymap()! It's great to have this confirmed by you Henric!

I just wanted to emphasize the importance of the third parameter, where you can "fill-in" the nulls with a default value (i.e. "MISSING") so users can click on it for further investigation. Filling nulls usually takes two LOAD statements if you are using joins. Applymap rocks!

Juan

0 Likes
14,879 Views
Not applicable

Hi Henric,

Thanks a lot for this great tip.

I do have a question still, regarding the possibility of using ApplyMap instead of Join. You state:

"The only time that you cannot use Applymap() instead of a join is if you want to fetch more than one single corresponding value (record) from the second table."

My question is, if the Key for mapping (or join) is over two different fields, mapping is not possible. Am I right?

Ex.: Imagine that to determine the Country you would need to use not only the CustomerID but also the Region (that also existed in the Orders table)

Thanks in advance for your help.

Either way this is very usefull.

Rui

0 Likes
14,879 Views
stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi Rui,

You simply need to create a composite key by concatenating multiple fields.  See my blog post for details on how to acheive this: http://bit.ly/kQcAZ5

Steve

http://www.quickintelligence.co.uk/

0 Likes
14,879 Views
Not applicable

Simple, easy and functional.

Just perfect!

Thank you all.

Rui

0 Likes
14,741 Views
hic
Former Employee
Former Employee

Steve is right. Using a composite key, you can do almost anything. My comment was about if you want two or more values from the same field. Then you need to join.

HIC

0 Likes
14,741 Views
montubhardwaj
Specialist
Specialist

Thanks for the wonderful post.

0 Likes
14,741 Views
wonoh0817
Contributor III
Contributor III

I was told to use 'ApplyMap' if data set is lower than 10 million rows by a QlikView instructor. If it's more than 10 million rows, he said that 'Join' would be performing better. Any thoughts?

0 Likes
14,741 Views
stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

This seems odd to me.  I would have thought that the more rows you have all the more reason to avoid doing JOINs.

Steve

http://www.quickintelligence.co.uk/

0 Likes
14,741 Views
hic
Former Employee
Former Employee

I agree with Steve. I would not use a Join it the table is large.

HIC

0 Likes
14,741 Views