Advanced Report Builder - Writing Expressions
Advanced Report Builder - Writing Expressions
When writing an expression, various functions can be used:
1. IF
Basic if statement:
IF([condition], [true result], [false result])
Examples:
When IsBillableRecord is a bool in the DataTable:
IF([IsBillableRecord], "Is Billable for " + [EmployeeFullName], "Is Not Billable")
If it’s a billable record, say “"Is Billable for " + EmployeeFullName", otherwise say "Is Not Billable“
HungryState is a string in this example:
IF([HungryState] = "VERY_HUNGRY", "Go to Green Acres for food", "Continue working")
If the value of HungryState is “VERY_HUNGRY”, then the “Go to Green Acres for food” message will show, otherwise “Continue working”.
Can also handle AND and ORs
IF([HungryState] = "VERY_HUNGRY" AND HasFood, "Go to Green Acres for food", "Continue working")
Can also handle Contains()
IF(HungryState.Contains("VERY_HUNGRY") AND HasFood, "Go to Green Acres for food", "Continue working")
Can also handle .Substring()
IF([HungryState] = “VERY_HUNGRY” AND [HasFood], HungryState.Substring(6,6), "Continue working")
This statement would produce “HUNGRY” if HungryState = “VERY_HUNGRY”
2. SWITCH
Supports up to 8 cases.
SWITCH([case 1], [case 1 result], [case n+1], [case n+1 result])
e.g.
When the earnings code is regular, output the standard rate, if it’s overtime, do Rate * 2, if it’s weekend, do Rate * 1.5, and the ‘true’ at the end is the default case (any other codes have no pay):
SWITCH([PayrollEarningsCode] = "REGULAR", [Rate],
[PayrollEarningsCode] = "OVERTIME", Rate * 2,
[PayrollEarningsCode] = "WEEKEND", Rate * 1.5,
true, 0.0)
Syntax notes
For comparison expressions, use single equals (=) not ==.
Not equals: Use <>, e.g.
If HBWConsumer is not “”, then output HBWConsumer...
SWITCH([HBWConsumer] <> "", [HBWConsumer], [DepartmentTypeCode] = "RESIDENTIAL", [DepartmentCode], true, "000")
Additional functions:
Column 1 : Column 2 — Column 3 — Column 4
Function : Description — Example — Technical Info
ISNULL : Check if a value is null. Advanced feature. Should not be needed in most instances. — IF(ISNULL([GroupOverallComments]), "No data", [GroupOverallComments]) — ISNULL(object value, object defaultValue)
ISNULLOREMPTY : Check if a value is null or empty. Used this one typically instead of ISNULL. — IF(ISNULLOREMPTY([Comments]), "[No Comments]", [Comments]) — ISNULLOREMPTY(object value, object defaultValue)
ISNULLOREMPTY : Check if a value is null or empty. Used this one typically instead of ISNULL. — IF(ISNULLOREMPTY([Comments]), "[No Comments]", [Comments]) — ISNULLOREMPTY(object value) RETURNS Bool
GETDATE : Returns the current date and time. Common use is to add a new field/column "CurrentDate", and then apply this complex expression to fill it with the current date based on this field. — GETDATE() — GETDATE()
FormatDateTime : Allows you to format a date time inside of the expression. Note there is a separate date format transformation as well, but this one can be used if you also want to append other text to the string. — FormatDateTime([Date_Range_From], \"MMMM yyyy\") + \" SCL Intermittent H2015\" — FormatDateTime(DateTime date, string format)
REGEX_MATCHES : Allows you to match based on a regular expression. — IF(REGEX_MATCHES([Service], \"Tier I{1,2}\\\\b\") OR CONTAINS(Service, \"Tier 1\") OR CONTAINS(Service, \"Tier 2\"), 0, Units) — REGEX_MATCHES(string str, string value)
CONTAINS : Checks if some text is contained inside another text/string. — IF(CONTAINS([Comments], "Newton"), "Newton is in this comment", "Newton is not in this comment") — CONTAINS(string str, string valueToSearchFor)
INCLUDES : Checks if a value/column is any of X number of values. — IF(INCLUDES([AgencyTaskDescription], "Vacation", "PTO", "Sick"), [SpecialRate], [RegularRate]) — INCLUDES(object value, params object[] args)
SWITCH : Similar to an IF statement, see above. — See above. — SWITCH(bool[])
padLeft : Adds padding prior to a field with the character defined. — — padLeft(object value, int targetLength, char padString = ' ')
padRight : Adds padding after a field with the character defined. — — padRight(object value, int targetLength, char padString = ' ')
RegexGroupIndex : Returns the value captured by a regular expression group at a given index. Note: Regular expression group indices start at 1 rather than 0 — RegexGroupIndex([Service], "Tier (I{1,3})", 1) — RegexGroupIndex(string str, string regex, int index)
DateDayDiff : Compares to dates and returns the difference in days between them — DateDayDiff([EndDate], [StartDate]) — DateDayDiff(obj minuend, obj subtrahend)
HoursDiff: Compares the dates and returns the difference in hours between them — HoursDiff([EndDate], [StartDate]) — HoursDiff(obj minuend, obj subtrahend)
MinutesDiff: Compares the dates and returns the difference in minutes between them — MinutesDiff([EndDate], [StartDate]) — MinutesDiff(obj minuend, obj subtrahend)