WeeklyAlteryxTips#59 Field sort by your order list

Alteryx

In the last post I explained about sorting fields. At this post, In this article, I would show you a more complicated method which is about sorting fields by customized list.

For doing this, you learn the advanced techniques how to edit the raw xml.

Disadvantages of using the Union Tool to sort

The sort by the Union tool was explained at 43th Tips. In #43, I sorted by connecting the sorted header data to the Union tool. But this way is not compatible with Blob type and SpatialObj type field. And I can not control the data type. This was just the easy way to do it.

For all problems, the Select tool is able to solve them! But this way is a little complex. When I put the Select tool and the Control Parameter tool, I didn’t know how to solve this problem and was confused.

Preparation: Enabling the XML view

For this way, you should enable the XML View option in order to use the XML View. Please select the menu [Option]-[User Settings]-[Edit User Settings].

And then, check the option “Display XML in Properties Window” and Save the dialogue.

This makes the XML View options show at each setting window of the each tool.

“Update Raw XML with Formula” option

In this case, you have to use “Update Raw XML with Formula” option on the Action tool. This option is difficult to use , but it makes you be able to solve many complex problems.

First, you have to make this workflow.

Let’s look at the Select tool. You can see the XML View on the setting window, so please select it.

Aftar that, you can see the XML as follows. The inside of the red box is the XML version of the setting.

The point is when OrderChanged is only True, the field order will be applied. When you change the field order, this “OrderChanged” option is automatically True.

As the additional information, when you click on the “Edit” on the top right, you can edit the XML freely.

Next, let’s look at the Action tool. The Action tool should be set as follows.

Here, you have to set the action type to “Update Raw XML with Formula”. And then, select the “Update Inner Xml” and set the “formula to Generate New XML” to “#1” and “XML Element to Update” to “SelectFields”. These settings replace the XML SelectFields with the data from the Control Parameter tool. In other words, those settings rewrite the red box as follows.

If you want to rewrite the XML tag “<OrderChanged value=”True” />”, you can use the “Update Outer Xml” option.

So, the preparation is finished! The next things that you have to do is to make XML of the inside the red box part. This work should be done outside the macro.

Make the Update XML

You can create the XML for update by the Formula tool and the Summarize tool. As the input data, you need the field name and order. For exmaple, it is as follows.

And the Select tool has “Unknown” field, so I add it.

The workflow is as follows.

At first, you have to sort by “Order” field and add the XML tag before and after the field names. The formula is as follows.

'<SelectField field="'+[FieldName]+'" selected="True" />'

And then, concatinate them by the Summarize tool. If the Unknow fields is not contained in the order list, it should be added at the Formula tool. As the result, you can create the XML by this procedure and the thing that you have to do is to input this data to the Control Parameter input of the macro.

The point is that what XML you have to create and input it to the Control Parameter input. Basically, you have to confirm the XML that you have to create, which is difficult point. So, this way is for advanced users.

The final workflow is as follows. This is published at the Community Gallery.

This workflow is needed the nested macro, but you can make this complex macro by this article!

Conclusion

  • You can sort fields dynamically by updating the Select tool’s XML with your favorite order.
  • You have learned the XML View.
  • You have learned the “Update Raw XML with Formula” on the Action tool.
  • The sample workflow has contained the macro that you can use it as-is.

Sample Workflow Download

The next blog is …

The Next post will be about business day count.

コメント

Copied title and URL