In this article, you will learn about how to apply Filter Expression using Helical Insight.
Filter expression is a query expression written in order to fulfill certain condition which cannot be directly fulfilled by applying a general filter.
There are two types of filter expression which can be used, one in which functions are present and the other without functions (refer below image):
- Filter Expression
Any column which is not having any aggregate functions or group by function applied to it, if it is added as a filter will come under “Filter Expression“. This means if we are directly dragging any column from left selection and using it as filter it will fall under this category. - Having Expression
We can drag any column from any table into selection panel and then add function (like average, groupby, sum, etc). Now this can also be used as filter with these functions applied. These then appear under “Having Expression”.
Also, the way of writing a Filter Expression or Having Expressions are same.
Whenever a user insert a filter then each filter is given a number starting with 0. First filter is assigned $(0), second $(1) so on and so forth in the Filter Expression. By default these are separated by AND operator, which can be changed (will be elaborated in the latter part of this article).
Important :
Whenever we add any filter we generally pass three things. First is column name on which filter is applied, second is condition and the third is value. For example, if we want to filter all the travel which happened in domestic route, RouteType is column, EQUALS is condition and DOMESTIC is value.
- When only “${0}“ is used in the filter expression / having expression, it signifies that all the parameters like column, condition and value are considered.
- ${0}.column -> It signifies that only column parameter for the first filter is considered.
- ${0}.condition -> It signifies that only condition parameter for the first filter is considered.
- ${0}.values -> It signifies that only values parameter for the first filter is considered.
For Example :
A DateTime datatype column is applied as a filter where a year needs to be extracted from that column irrespective of the value selected. The problem in this case is since the datatype is different and we need to extract only the year and filter accordingly, in such cases Filter expression would be useful.
Suppose selected value is “2016-06-01 17:51:56” in the format “YYYY-MM-DD hh:mm:ss”
Filter expression :
Year(${0}.column) = Year(${0}.value)
2016 = 2016 //If condition fulfills then output is generated, otherwise “No data” message is displayed.
Year(${0}.column) : Year is extracted from the selected columns which is used as a filter
Year(‘${0}.value’) : Year is extracted from the filter value inserted by the user
You can also use SQL query and compare the output to the filter values.
From version 4.0 GA onwards, we have implemented security upgrade which restricts use of certain SQL keywords and patterns in the filter expression and having clause. You can change the SQL pattern and add or remove the patterns that need to be restricted in the defaults.properties file present in ../hi/hi-repository/System/Admin folder. For more details on the security rules and usage, please refer to this blog.
Use Case :
An area spline chart showing Month-Year wise travel expense report for the year 2015.
If you want your data to be filter out based on the “Year” and “Month Range” then what to do ?
A filter expression is required by converting a filter column into appropriate format for comparison.
Follow the steps :
-
- Insert the column twice and set values to “Auto”. By clicking edit you can set value.
-
- Rename the filters for your understanding and apply proper condition
-
- Apply a filter expression
year(${0}.column)=year(${0}.value) // year is extracted for comparison
AND
month(${1}.column) >=month(${1}.value) // month is extracted for comparison
AND
month(${2}.column)<month(${2}.value) // month is extracted for comparison
- Click generate to view output
For more details, mail us at demo@helicalinsight.com