Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
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
hic
Former Employee
Former Employee

-Mikael-

This should work fine. Then a specific value in the mapping table is used several times.

HIC

809 Views
Not applicable

Hi HIC,

I knew this but to confirm, If the Lookup column in the Destination is given different column name than the Source Lookup Column name than will the Script throw error or it will execute with defined third parameter or it will do the lookup and get the output into my destination table ?

Thanks,

Santosh

0 Likes
809 Views
anderseriksson
Partner - Specialist
Partner - Specialist

Name of columns in the mapping table does not matter.

The lookup value will be matched to the first column and output from the second column.

Is there no match in first column the optional third parameter will be returned.

Is there no third parameter either the lookup value will be returned.

809 Views
hic
Former Employee
Former Employee

The Applymap() function will never check the field names - it will only compare the field values. So, it will do the lookup, and what you call the two columns in the Mapping table is really irrelevant.

HIC

809 Views
Not applicable

Thanks for your Time

ok then let me confirm from my side again

If i change the CustomerID to Cust_ID  and do the reload , this will not throw any error ?? and will it give me the output ??

     MapCustomerIDtoCountry:
     Mapping Load Cust_ID , Country From Customers ;

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

     Orders:
     Load *,

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

0 Likes
762 Views
Not applicable

ok , I have tested it now, what you were suggesting is correct !!

Thanks for your Time and Comment hic and AndersEriksson !!

0 Likes
762 Views
hic
Former Employee
Former Employee

The field reference in the Mapping load must of course match what you have in the input table. But what you call it is irrelevant. So, your suggested change will cause an error. But the following will not:

MapCustomerIDtoCountry:
     Mapping Load CustomerID as SomethingElse1, Country as SomethingElse2 From Customers ;

Orders:

     Load *, ApplyMap('MapCustomerIDtoCountry', CustomerID, 'Unknown') as Country From Orders ;

hic

0 Likes
762 Views
qlikviewwizard
Master II
Master II

Nice blog post.

Thank you.

0 Likes
762 Views
anderseriksson
Partner - Specialist
Partner - Specialist

And to confuse things further, if you change the order of fields in the mapping table you could find yourself in trouble.

The below mapping would match the CustomerID field from the Orders table with Country field of Customers table.

If by chance there is a Orders.CustomerID = Customers.Country you would get Customers.CustomerID.

If not you would get 'Unknown', probably not what you expect if new to mapping tables.

Just to emphasize field names in mapping tables have no significance at all.

Mapping tables have exactly two columns, first is the mapping key, second is the mapped value.

MapCustomerIDtoCountry:
     Mapping Load Country, CustomerID From Customers ;

Orders:

     Load *, ApplyMap('MapCustomerIDtoCountry', CustomerID, 'Unknown') as Country From Orders ;

0 Likes
762 Views
Anonymous
Not applicable

I have been working on the duplication problem (due to a left join of tables) for a couple of days but no luck until I saw this great solution.

It is working in Qlik Sense too, just by replacing "from" to "resident".

Thanks!

0 Likes
762 Views