wildmatch - script and chart function
The wildmatch function compares the first parameter with all the following ones and returns the number of the expression that matches. It permits the use of wildcard characters ( * and ?) in the comparison strings. * matches any sequence of characters. ? matches any single character. The comparison is case insensitive.
Syntax:
wildmatch( str, expr1 [ , expr2,...exprN ])
If you want to use comparison without wildcards, use the match or mixmatch functions.
Some of the examples in this topic use inline loads. For more information, see Inline loads.
Example: Load script using wildmatch
Load script
You can use wildmatch to load a subset of data. For example, you can return a numeric value for an expression in the function. You can then limit the data loaded based on the numeric value. Wildmatch returns 0 if there is no match. All expressions that are not matched in this example will therefore return 0 and will be excluded from the data load by the WHERE statement.
Create a new tab in the data load editor, and then load the following data as an inline load. Create the table below in Qlik Sense to see the results.
Transactions:
Load * Inline [
transaction_id, transaction_date, transaction_amount, transaction_quantity, customer_id, size, color_code
3750, 20180830, 23.56, 2, 2038593, L, Red
3751, 20180907, 556.31, 6, 203521, m, orange
3752, 20180916, 5.75, 1, 5646471, S, blue
3753, 20180922, 125.00, 7, 3036491, l, Black
3754, 20180922, 484.21, 13, 049681, xs, Red
3756, 20180922, 59.18, 2, 2038593, M, Blue
3757, 20180923, 177.42, 21, 203521, XL, Black
];
/*
Create new table called Transaction_Buckets
Create new fields called Customer, and Color code - Black, Blue, blue, red
Load Transactions table.
Wildmatch returns 1 for 'Black', 'Blue', and 'blue', and 2 for 'Red'.
Only values that returned numeric value greater than 0
are loaded by WHERE statement into Transactions_Buckets table.
*/
Transaction_Buckets:
Load
customer_id,
customer_id as [Customer],
color_code as [Color Code Black, Blue, blue, Red]
Resident Transactions
Where wildmatch(color_code,'Bl*','R??') > 0;
Results
Color Code Black, Blue, blue, Red | Customer |
---|---|
Black | 203521 |
Black | 3036491 |
Blue | 2038593 |
blue | 5646471 |
Red | 049681 |
Red | 2038593 |
Examples: Chart expressions using wildmatch
Chart expression 1
Create a new tab in the data load editor, and then load the following data as an inline load. After loading the data, create the chart expression examples below in a Qlik Sense table.
MyTable:
Load * inline [Cities, Count
Toronto, 123
Toronto, 234
Toronto, 231
Boston, 32
Boston, 23
Boston, 1341
Beijing, 234
Beijing, 45
Beijing, 235
Stockholm, 938
Stockholm, 39
Stockholm, 189
zurich, 2342
zurich, 9033
zurich, 0039];
The first expression in the table below returns 0 for Stockholm because 'Stockholm' is not included in the list of expressions in the wildmatch function. It also returns 0 for 'Boston' because ? only matches on a single character.
Cities | wildmatch( Cities,'Tor*','?ton','Beijing','*urich') | wildmatch( Cities,'Tor*','???ton','Beijing','Stockholm','*urich') |
---|---|---|
Beijing |
3 |
3 |
Boston | 0 | 2 |
Stockholm | 0 | 4 |
Toronto | 1 | 1 |
zurich | 4 | 5 |
Chart expression 2
You can use wildmatch to perform a custom sort for an expression.
By default, columns sort numerically or alphabetically, depending on the data.
Cities |
---|
Beijing |
Boston |
Stockholm |
Toronto |
zurich |
To change the order, do the following:
- Open the Sorting section for your chart in the Properties panel.
- Turn off auto sorting for the column on which you want to do a custom sort.
- Deselect Sort numerically and Sort alphabetically.
-
Select Sort by expression, and then enter an expression similar to the following:
=wildmatch( Cities, 'Tor*','???ton','Beijing','Stockholm','*urich')
The sort order on the Cities column changes.
Cities |
---|
Toronto |
Boston |
Beijing |
Stockholm |
zurich |
You can also view the numeric value that is returned.
Cities | Cities & ' - ' & wildmatch ( Cities, 'Tor*','???ton','Beijing','Stockholm','*urich') |
---|---|
Toronto | Toronto - 1 |
Boston | Boston - 2 |
Beijing | Beijing - 3 |
Stockholm | Stockholm - 4 |
zurich | zurich - 5 |