Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Knowledge Base

Definitive answers from Designer Desktop experts.

All About Constants

AlexKo
Alteryx Alumni (Retired)
Created

You're working on your gazillionth Formula tool and "Jeff" from Quality Assurance sends you an email that the margin of error for your process has been restandardized. It's not 0.122 anymore but 0.121. Then the horrible reality sets in - you're going to have to go back through all of your formulas and update that one.. stupid.. little.. number.

 

You vow that you'll never again let smug "Jeff" ruin your night with his nitpicky tolerance standards, so you open up your browser, load up your home page (the Alteryx Community page duh), and find exactly what you've been looking for: a Knowledge Base article about document constants.

 

So what's a constant? According to the Alteryx Help pages: "Document Constants are global variables for a workflow. Constants make it possible to change a value in a single location and have that change propagate to the rest of the workflow."

 

You can find constants in your Workflow Properties tab (Click on the canvas and go to Workflow in the Configuration Window).

 

 

2018-11-14_15-43-31.png

The "Is Numeric" checkbox on the far right will make the value numeric rather than a string.

 

Types of Constants

 

Engine

Those four Engine constants will show up in every workflow, and an additional constant,IterationNumber, pops in there when you are authoring a Batch, Iterative, or Location Optimizer macro.

Temp File Path: The directory where the engine will write temporary files.

Version: The version number of Alteryx engine installed on the machine.

Workflow Directory: The directory of the current workflow. If no workflow is open, the value will be the Alteryx installation directory.

GuiInteraction: A Boolean value indicating whether or not a workflow is being run from the GUI. When a workflow is run from the Alteryx Designer or Analytic App interface, the returned value will be True or 1. When a workflow is run from command line, the returned value is false, or 0.

Iteration Number: An integer that corresponds to the number of times a macro has run, starting at 0.

 

User

Clicking on theadd.PNGsymbol will allow you to addUser Constants. In the above screenshot, I added a "Favorite Number" User Constant. These can be strings or numerics, and will stick with the workflow if you send it, schedule it, or upload it to a Gallery.

 

Question

If you're making an app or macro, manyof your Interface Tools will show up as Question Constants. The name of this constant will correspond to the name of the tool in the2018-11-14_15-51-08.pngAnnotation area of the Tool Properties.

 

2018-11-14_15-52-15.png

 

How to use constants

 

Formula Method

You can bring in a constant in any tool with a Formula Processor component, such as in the Formula, Filter, and Dynamic Rename tools.

 

2018-11-14_15-57-55.png

 

 

Engine and User constants will populate under the Constants menu in theVariables. They will come in as either numeric or string based on whether that Is Numericcheckbox is checked off.

Question constants will not show up in this menu, and cannot be typed in[...] notation like the other variables.

 

 

Reference Shortcut

Any constant (Question included) can be called upon inanyconfiguration option using a reference shortcut. This may not always work exactly like you think it will, so use this method with caution.

 

Generally, in any configuration you can type into, you can bring in a constant by using the notation%Type.ConstantName%. For example, in our Formula tool, we could refer to that Question constant in our Expression Editor.

 

2018-11-14_15-54-43.png

 

Note we still had to place it within quotes, because these do not come in with our constant value.

 

Using the reference shortcut, we can pass along data without even connecting up the Interface tool to the formula.

 

2018-11-14_16-04-58.png

 

Above we have a macro with the text box named "Constant", and below is the result when run in another workflow.

 

 

2018-11-14_16-00-03.png

 

Check out the attached example to see it all in action! (compiled in version 10.1)

 

Attachments
Comments
Su
7 - Meteor

Hello :)

 

How can I build it in order for this constant variable to be a date that the user types in? The idea is that the parameter will be used to query the database, for example, and then the same value will serve to add to the file we output.

AlexKo
Alteryx Alumni (Retired)

Hi @Su!

 

If you would like to have the user type in the value for a constant in an app, you just need a Text Box tool like in the picture below.

 

original.png

 

You could name it anything you like instead of "Constant", and you would just refer to it in the form of "%Question.Constant%".

You could also possibly use the output of the Date tool instead, but this will always return the format "yyyy-mm-dd" which may not necessarily be what you need.

JMoore
8 - Asteroid

Can a constant be used in a SQL query or do you have to feed the constant via a control input for a macro with a data input node inside it?

Matt_Curtis
9 - Comet

@JMoore that's how I ended up in this thread also.  Wondering if it can be used in my SQL queries.

 

mcha54
8 - Asteroid

Any updates to whether we can use constants in SQL queries? It would make our workflows so much more robust.

JMoore
8 - Asteroid

To use a constant in a SQL is complicated I found but feed a constant into something (data input or formula) then run SQL query inside of dynamic input and adjust the query with a replace command, this is the general idea although I have yet to do it as I moved on from the issue.

bluebrent
7 - Meteor
I seem to be able to call constants just fine in my SQL queries using the following syntax : '%Type.Name%'
somolov
5 - Atom

Is there any way to use one constant to define another? Example: I have the constant User.ClientName, which equals "ABC Co."

I want to define another constant named User.FolderPath, using "C://Documents/%User.ClientName%/Reports". But when I try to set this up in this way, Alteryx errors out and says it can't find the folder with that path. 

 

Thoughts, anyone?

Scott_Snowman
10 - Fireball

@somolov If you're still searching for this: You may not be able to create a constant file path like this, but you should be able to dynamically create one with the Formula tool and the following expression:

 

"C://Documents/" + %User.ClientName% + "/Reports"

 

WSData_dup_359
7 - Meteor

We have multiple people working on workflows with varying degrees of experience.  To streamline our process we want to reuse workflow sections as much as possible and make certain edits without requiring users to search for and update controls throughout the workflow(s). 

 

For the User Interface tools, we were wondering if we can use constants in the configuration of the UI tools.  For example, in the text tool, can constants be used to place text in the "Default Text" box.  That would allow us to change a single constant that may update multiple UI elements accordingly.  It would also make teamwork easier as we can train people to make certain edits to constants instead of more complicated workflow edits.

 

We attempted to put %User.VariableName% into the text box without success.  Is there another way?

gsantos
7 - Meteor

@WSData, I had the same doubt as you. In case you're still searching, take a look at this article:

 

https://community.alteryx.com/t5/Alteryx-Designer-Discussions/Dynamic-Textbox-default-values/td-p/36...

NZP
7 - Meteor

Can these constants be used on the Interface Designer?  I would like to indicate where the file is being saved but so far no syntax that I have used is working.

NZP_0-1585603120173.png

 

Ram_Kunchala
5 - Atom

Great article! Thank you.

 

I tried to access/use a Constant defined in a workflow, inside a custom macro contained in that workflow.

Doesn't look like macros can see the constants defined in the workflows they are used in.

 

To illustrate:

 

  • Defined a Constant called "Database" and set to a certain value in my main workflow
  • Somewhere in that workflow, I dropped in a custom built Standard macro.
  • Inside that macro, I tried to read the Constant %User.Database% in a formula.
  • But the value in that Constant is not seen by that Formula in that macro. Formula just gets "%User.Database%" as a literal string. The value from that Constant is not substituted.

 

Is that a known limitation with the Constants or I just don't know the right technique to make it work?

 

What I'm looking to accomplish is a true 'global' constant that my entire workflow and all contained macros can see and use.

 

If the scope of Constants is restricted to main workflow only, the only other possibilities I can think of are 1) Environment Variables or 2) Registry Keys.

 

Any thoughts or suggestions or ideas?

Scott_Snowman
10 - Fireball

@Ram_Kunchala I don't think the scope inside a macro can see outside: I've had similar experiences when trying to access parent-level parameters from inside a macro, such as the running workflow's name that called the macro. A couple workarounds that aren't your suggestions 1) and 2) above, but they're not elegant:

 

  • Pass the constant into the macro literally, as a field on your incoming data (or else as a control parameter for a batch macro)
  • Write the constant to a local file that is then read by the macro (assumes you can accurately scope the file name itself as a string understood both inside and outside the macro)
Ram_Kunchala
5 - Atom

@Scott_Snowman,

 

Thanks for validating that understanding.

 

I wanted to introduce this new ability in the existing macros, without changing the existing signature of IN/OUT.

We have over 100 macros used across even more number of applications and analytical workflows.

It is for the same reason I was hoping to implement this in somewhat non-conspicuous way.

 

It did occur to me to serialize the value in the Constant, into a place both parent workflow and the macros contained in there could access. Assuming I did this (against some kind of Session ID to prevent overlapping between users), the chaining and enforcing a specific sequence of tools execution could get tricky for me. Particularly because my goal is to have the value in this Constant drive how the existing input tools in those macros behave. (I'm basically trying to switch between different databases and was hoping to accomplish it with the Constant)