# Functions

| Benefit                                                                             | Feature                                                              |   |
| ----------------------------------------------------------------------------------- | -------------------------------------------------------------------- | - |
| Saves time and improves engagement through greater personalisation                  | Many popular functions available for use in calculations and content |   |
| Functions allow you to create more dynamic content by manipulating text and numbers | Functions can be nested for even greater power                       |   |

### TEXT functions

<table data-header-hidden><thead><tr><th width="171">Text Functions</th><th>Purpose</th></tr></thead><tbody><tr><td>CONCAT()<br></td><td>Adds together strings to create a new string.<br><br>|CONCAT("Rob","Bert")| returns RobBert<br><br>Can be any number of strings |CONCAT("rob","ert"," is ", " ","working"," ","today")|  returns "robert is working today"<br><br>Use with other nested string functions too |CONCAT("Rob",RIGHT("Bert",3))| returns Robert<br></td></tr><tr><td>LEFT()<br></td><td>Returns a new string for the length of characters starting from the left LEFT(string,length)<br><br>|LEFT("Robert",3)| returns "Rob"<br></td></tr><tr><td>LEN()<br></td><td>Returns the length of a string Example LEN("Robert") returns 6<br></td></tr><tr><td>LOWERCASE()<br></td><td> Forces strings to lowercase.<br><br>|LOWERCASE("HElen")| = "helen"<br></td></tr><tr><td>MID()<br></td><td> Returns part of a string MID(string, start, length) Example MID("Robert",3,4) returns "bert" All string functions cane be nested.<br><br>|UPPERCASE(MID("Robert",3,4))|                     returns "BERT"<br></td></tr><tr><td>REPLACE()<br></td><td>Replaces occurrences of a string within a string with another string :)<br><br>Format is REPLACE(string,what,by)<br><br>|REPLACE("i like to eat apples","a","@")| returns "i like to e@t @pples"<br></td></tr><tr><td>RIGHT<br></td><td>Returns a string of length characters starting from the right<br><br>Format is RIGHT(string, length)<br><br>|RIGHT("Mary had a little lamb",4)| returns "lamb"<br></td></tr><tr><td>STRCOUNT()<br></td><td>Returns the number of times a substring is found within a string.<br><br>|STRCOUNT("bird","a little bird sat on my window")| = 1<br><br>|STRCOUNT("a","it was a cold night and a bird pecked at an apple")| = 2<br></td></tr><tr><td>UPPERCASE()<br></td><td> Forces string to all uppercase.<br><br>|UPPERCASE("hello")| = "HELLO"</td></tr></tbody></table>

### NUMBER functions

<table data-header-hidden><thead><tr><th width="130">Number Functions</th><th>Purpose</th></tr></thead><tbody><tr><td> ABS()<br></td><td>ABS returns the absolute value (ie. removes the negative sign!)<br><br>ABS(-1) = ABS(1)<br></td></tr><tr><td> AVG()<br></td><td>Returns the average in a list of numbers.<br><br>e.g AVG(56,27, audience.sender.my_score)<br><br>|AVG(partition.custom_data["a"], partition.variables[CONCAT("a.",audience.sender.hash,".my_score")])|<br><br>To find an average of an array:<br>AVG(partition.scores)<br><br></td></tr><tr><td> FORMAT()<br></td><td>Used to make numbers look attractive - usually to reduce the number of digits after the decimal point. <br><br>Example |FORMAT(3.1276565,"###.##")|  gives 3.13<br></td></tr><tr><td> INT()<br></td><td>Forces value to an integer.<br><br>for example INT(12.45) = 12<br><br>Useful for converting a boolean to a value such as INT(5>2) = 1<br></td></tr><tr><td> MAX()<br></td><td>MAX() returns largest number.<br><br>MAX(5, 4, 1, 12, 56,72) returns 72<br></td></tr><tr><td> MIN()<br></td><td>MIN() returns the smallest number.<br><br>MIN(5, 4, 1, 12, 56,72) returns 1<br></td></tr><tr><td> MOD()<br></td><td>MOD stands for modulo.<br><br>The function returns the remained after a division. The format is MOD(number to be divided, the dividing number).<br><br>This is most easily shown with an example such as breaking out hours, mins and secs from a duration:<br><br>Hours = |audience.sender.duration|/3600 Minutes = MOD(|audience.sender.duration|/60,60) Seconds = MOD(|audience.sender.duration|,60)<br></td></tr><tr><td> SUM()<br></td><td>Returns the sum of all values e.g SUM(56,27, audience.sender.my_score)<br><br>|SUM(partition.custom_data["a"], partition.variables[CONCAT("a.",audience.sender.hash,".my_score")])|<br><br>To find the sum of an array:<br>SUM(partition.scores)</td></tr></tbody></table>

### DATE functions

\| DATETIME()    | <p>Formats dates and times. Please see the DATETIME() article for the full specification<br><br>Example<br>|DATETIME(NOW(),'MMMM d')| returns the current date in the format April 20<br><br>Recipe<br>You can add and subtract time from NOW() to calculate dates in the past on in the future. For example<br>|DATETIME(NOW()-'02:00:00:00','dddd')| will subtract two days from today's date and print the day of the week.  For example<br><br>"Hey, I know it was 2 days ago you looked into that issue because it was a |DATETIME(NOW()-'02:00:00:00','dddd')| "</p>                                                                                                                                                                                                                                                                                                                                                                                    |
\| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
\| NOW()         | <p> Returns current date and time<br></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
\| NEXTWEEKDAY() | <p>Creates a future date based on which day of the week and how many weeks from now. Use with DATETIME formatting for most practical result.<br><br><em>NEXTWEEKDAY(date, day of week number, number of weeks from date)</em><br><br>Example  <br>|DATETIME(NEXTWEEKDAY(NOW(), 6, 1),'dddd D MMM')| returns the date of Saturday one week from the scenario run date (i.e. from NOW())  e.g. from function given here Saturday 13 May <br>﻿<br>﻿<br>Day of Week number<br>Sunday 0<br>Monday 1<br>Tuesday 2<br>Wednesday 3<br>Thursday 4<br>Friday 5 <br>Saturday 6<br><br>Recipe<br>Imagine you need a persona to send an email warning about a protest on a Wednesday four weeks from the time of the exercise. The content might look like this: <br>"Hi, I'm writing to warn you of a march affecting access to your offices on |DATETIME(NEXTWEEKDAY(NOW(),3,4), 'ddd D MMM')|. I hope 4 week's notice is enough for you to organise remote working"</p> |
