15 Creating and using macros

By the end of this chapter you will be able to:

  • Create macros
  • Use macros

Creating macros

Macros are used to create reusable expressions. They can be thought of as, the ability to write bespoke functions. Macros can optionally take in a number of parameters to use within its expression:  

  1. Find  Macros in the repository
  2. Right-click and select  Add Macro
  3. Configure the macro:
    1. Name: NumOfWeeks
    2. Minimum Parameters: 2
    3. Maximum Parameters: 2
    4. Expression: create an expression to work out the number of weeks - as a whole number – between two dates
      1. Use the functions dateDiff() and round() to help with this

         See Completed Expression

        do(

             // Create variables from incoming parameters

             $Date1 = $args[1],

             $Date2 = $args[2],

             // Get the difference in time between the two dates

             $dateDiffMilliSecs = dateDiff( $Date2, $Date1 ),

             // Round the answer into whole weeks

             round($dateDiffMilliSecs/(1000*60*60*24*7), 0 )

             )

  4. Press  Apply and Close to save your new macro

Using Macros

  1. Create a new model: Try Macros
  2. Drag the table MC Macro Terminated Customers on to your model
    1. This contains some key dates for terminated customers: the signup date; the date that the customer terminated their service; the date that the final debt on the customer's account was cleared
    2. Add  Attributes for:
      1. the number of weeks between signup and termination
      2. the number of weeks between termination and the final debt being cleared
    3. In each case, use your macro to calculate the number of weeks between the two dates

       How?

      Expression: NumOfWeeks(_out.SIGNUP_DATE,_out.TERMINATION_DATE)

      Expression: NumOfWeeks(_out.TERMINATION_DATE,_out.DEBT_CLEARED)

  3. Run the table MC Macro Terminated Customers to test your new attributes