Conditional Writing in Alteryx

Conditional Writing in Alteryx

Do you know the situation? You create a table or a file with Alteryx, which is then used by other users. Master data maybe. Anyway, data that is needed desperately.?

Es wurde kein Alt-Text für dieses Bild angegeben.

So far, nothing special. But there is a small problem. In your workflow you process a number of files or tables you can' t always trust.?Sometimes one of the files is completely empty, sometimes the content of a field is incomplete or wrong. And when that happens, your result is also empty or wrong.

Es wurde kein Alt-Text für dieses Bild angegeben.

This does not happen to you? Lucky you.?

But if something like this happens to you sometimes, or if you can imagine that it could possibly happen - wouldn't it be better in this case if at least the last correct state of your table is preserved, i.e. you don't overwrite everything??

This is the point at which it would be helpful if you could put some kind of validation before the Output Data Tool, and only if this validation is passed (i.e. the data records are okay), it is actually written.?For simplicity, let's check here if there is any result data at all, but of course this can be extended as you like.

Es wurde kein Alt-Text für dieses Bild angegeben.

Maybe the Condition tool can help here. Not seen yet? We'll change that now.

Es wurde kein Alt-Text für dieses Bild angegeben.

However, you'll find this tool within the "Interface" palette - it seems we'll have to build a macro or an analytic app. For our use case, a macro seems to be the appropriate way to "wrap" the output data tool and to add the condition we want.

So - here we go. We open a new workflow and drag the Condition Tool onto the canvas.?The tool has a questions anchor, here we can "feed" in our case the number of result records that passed the validation.?

Es wurde kein Alt-Text für dieses Bild angegeben.

We can easily supply this number (or any other value to be checked) via a control parameter tool. We only have to give the tool a label (i.e. a name for the value that is to be delivered). Of course, it makes sense to name the parameter "Number".

Es wurde kein Alt-Text für dieses Bild angegeben.

The "input side" is done with it. On the "output side" there is a "T" and an "F" anchor, which are controlled depending on the condition we can set in the Condition tool.?In our use case, we check whether the delivered value (i.e. the number of records) is greater than 0. The parameter is passed as a variable "Connections from Questions". These variables are numbered - we have only one variable, i.e. "#1".?Passed is a string, so we need a conversion to a numeric value to perform the comparison "> 0".

Es wurde kein Alt-Text für dieses Bild angegeben.

Parameter transfer and parameter check are done with this, before we now take care of the actions that are linked to "T" and "F", let's have a look at another data transfer to our macro, because in addition to the value to be checked, the data that is to be written to the output file (if the workflow delivers data records) must of course also be transferred.

Es wurde kein Alt-Text für dieses Bild angegeben.

This handover can easily be done via a Macro Input tool. This tool is then connected to the Output Data tool, which is then used to write the data to the result file. The Macro Input tool gets the input name "Data" and the abbreviation (Anchor Abbreviation) "D". I'm perhaps a little bit ungenerous with the naming of Macro Input and Macro Output tools and the abbreviations for Input and Output anchors - let's say I learned from bad experiences.?

The Output Data tool must not always be active, but only when the condition is met, so it must be possible to switch it on and off. For this we use a little trick - we put the Output Data tool into a Tool Container.

Es wurde kein Alt-Text für dieses Bild angegeben.

In fact, the Tool Container has a switch by which we can enable or disable it (and all included tools). If there are no result data records, the tool container is simply deactivated, in the other case it should remain active. And this is exactly what we solve now via two Action tools, which we switch between the "T"/"F" outputs of the Condition tool and the Tool Container.

For both Action tools we select the Action Type "Enable/Disable Container from Condition". For the Action tool connected to the "T" output, the property is "Enabled", but for the "F" output it is "Disabled" - so if the condition is met, the Tool Container is enabled, but if the condition is not met, it is disabled and the Output Data tool does not write to the result file.?

Es wurde kein Alt-Text für dieses Bild angegeben.

In the overview our macro looks like this:

Es wurde kein Alt-Text für dieses Bild angegeben.

By the way, the Control Parameter Tool automatically sets the Workflow Type to "Batch Macro" (even though this isn't related to batch at all). We don't need any further configuration, but we could of course choose a custom icon for our macro using the Interface Designer.?Anyway we save our macro now, we can name it for example "ConditionalOutput", the file type is "yxmc" for a macro.?

Es wurde kein Alt-Text für dieses Bild angegeben.

All we have to do now is to add the macro to our workflow.?First, we add a Count Records tool to count the records. We can remove the existing Output Data Tool and add our macro instead.?The "D" Input Anchor we connect to the Summarize Tool (that's where the records come from that will be written if there are any).

The Question Anchor we connect to the Count Records Tool, which provides the condition to be checked. In the configuration we have to specify that Count (the output of the Count Records tool) returns the parameter Number, there is no "Group By" field here.

Es wurde kein Alt-Text für dieses Bild angegeben.

Time for a quick test. What happens if there is no result data?

Es wurde kein Alt-Text für dieses Bild angegeben.

There is no more message that a file is being written to - the existing file/table remains intact, the macro works as we expected.

So far, the macro is still very specific to our specific use case, but of course it can be easily extended - for example, we could pass the name and type of the file as additional parameters. Also a more flexible control of the condition can be easily implemented.??


要查看或添加评论,请登录

Roland Schubert的更多文章

  • Time For Summary

    Time For Summary

    When I receive a new table or file, I always try to first get an overview. Usually by bringing an INPUT DATA tool onto…

  • A Little Bit More: Oversampling

    A Little Bit More: Oversampling

    When selecting data records, it is often simply a matter of selecting only the first (or last) data from a table…

    1 条评论
  • Building Groups Based on Relations

    Building Groups Based on Relations

    Grouping is not necessarily a very unusual task - customer groups always come to my mind spontaneously. Common…

  • Different Types of Correlation

    Different Types of Correlation

    I have to admit it - I intuitively tend to look for relationships between different data. And indeed, I often recognize…

  • Grouping Data

    Grouping Data

    Grouping data in some way is an essential part of day-to-day business for data analysts. Many people immediately think…

  • Comparing To Lists

    Comparing To Lists

    Long years ago, a DIY chain in Germany launched a discount campaign entitled "20% discount on all items - except pet…

  • Famous (or Not-So-Famous) Last Words

    Famous (or Not-So-Famous) Last Words

    Sometimes you just have to have the last word - the last word from a text field, of course. When it comes to "breaking…

    1 条评论
  • Break on Error

    Break on Error

    An error has occurred in a workflow and it continues to run anyway? Sometimes that's all right, but only sometimes…

  • Year-To-Date Calculations

    Year-To-Date Calculations

    If you are working in Finance/FP&A/Controlling, calculating "Year to Date" (YTD) values is an essential part of your…

  • Compare Date and Time

    Compare Date and Time

    We often need data only for a specific period of time - a year, a month or a week, sometimes just a few hours, but the…

社区洞察

其他会员也浏览了