community
cancel
Showing results for 
Search instead for 
Did you mean: 

Alteryx Designer Knowledge Base

Definitive answers from Designer experts.

Tool Mastery | Cross Tab

Alteryx Alumni (Retired)
Created on

Cross Tab.pngThis article is part of the Tool Mastery Series, a compilation of Knowledge Base contributions to introduce diverse working examples for Designer Tools. Here we’ll delve into uses of the Cross Tab Tool on our way to mastering the Alteryx Designer:

 

Sometimes you look at the steaming pile of data before you and wonder how you’ll ever get it in the form you need. Every option seems to require a great deal of manual labor, and as a lazy– er that is, as a data blending professional, that is simply something you will not abide.

 

In situations like these, you may want to consider shaking things up. There's no better tool for this than the Cross Tab, a powerful tool that allows you to reshape your data any-which-way, allowing you to approach your problem from a new angle. In this article, I will demonstrate a few use cases to showcase how you can leverage this awesome tool.

 

Use Case 1: Extracting dynamic data made easier by assigning groups

 

The data?

 

You receive a list that looks like Field_1 below. The PMID refers to an ID number for a medical journal article in a database. Each FAU is another author on the paper. There may be any number of authors for a paper.

 

crosstab1a.jpg

 

 

 

The goal?

A table with ID numbers in the first field and columns corresponding to authors.

 

2019-03-01_10-13-18.png

 

 

How?

  1. Prepare the data by filtering and splitting away the identifier. The third column above, "Field_12", shows the usable data.
  2. Use a Multi-Row Formula to identify the ID column and author columns uniquely. In this case, each ID number is represented by ‘0’, and authors are counted up from zero until they hit another ID. See the “Headers” column.
  3. Use another multi-row to associate each group of headers to one another. This is basically a RecordID – it identifies a single paper in the database. See the “Groups” column above.
  4. Cross-Tab! By using these identifying columns, you may shift your data so that each of the Headers creates a column, and each of the Groups create a row. See the configuration window in the first image above.

 

Use Case 2: Performing calculations dynamically for any number of fields

 

The data?

A handful of numerical fields, shown below. They are grouped by a category field and you’ve added a unique RecordID field.

 

 

transpose1a.jpg

 

 

 

The goal?

Rolling averages for each column, within their respective category.

 

2019-03-01_10-18-17.png

 

 

How?

  1. Instead of writing a multi-row formula for each column, Transpose everything down to a single column, and tack on the Key Fields “RecordID” and “HP Category.” See the configuration window in the first picture. This results in the below output.

    2019-03-01_10-19-09.png

  2. While it may appear to be even more difficult to work with, this allows you to calculate a rolling average in one fell swoop. Use a Multi-Row tool to calculate an Average. You can easily avoid picking up the wrong values by using the Group By option – check off “HP Category” and “Name”. Make sure also to set Values for Rows that don’t Exist to the closest valid row.

    2019-03-01_10-19-52.png

  3. Restructure using Cross Tab! (Group by “RecordID”, Header “Name”, Data “r3”)

 

Use Case 3: Tricky logic made easier with Cross Tab methodologies

 

The data?

You have a list of all possible combinations of 5 items. For each combination, a number of rows corresponding to the number of items lists each item's weight and value - i.e. Combination 123 will be represented three times, with information for item1, item2, and item3.

 

crosstab3a.jpg

 

 

 

The goal?

You wish to optimize your selection of items to meet certain criteria, such as minimum weight and maximum value.

2019-03-01_10-24-22.png      

 

How?

  1. Use a Formula tool to add a column for "Weight" as shown in the first image.
  2. Use the Cross Tab with the "Sum" methodology to find the combined weight of all the items in each combination. The "Weight" header will group all the "kg" values together, and grouping by "Combinations" will create one row for every combination.
  3. Repeat this for "Value" ($).

 Pro Tip: Renaming Fields

 

A downside of the Cross Tab tool is that it doesn't play nice with special characters in field headers, including spaces. This means that if you have a field header "a a", it will actually come out as "a_a". I know this can be a bit inconvenient, but when we were developing the Alteryx engine we prioritized speed and efficiency over keeping the field headers looking nice. Don't worry though - there's a perfectly doable solution to this problem (more than one actually!) that makes use of an awesome tool called Dynamic Rename. This is the way I usually like to go about it:

 

Capture.PNG

 

For the workflows shown in these use cases, please see the supplementary Alteryx package. Note that you may recieve an error upon extracting the content, but this won't affect running the workflow.

 

By now, you should have expert-level proficiency with the Cross Tab Tool! If you can think of a use case we left out, feel free to use the comments section below! Consider yourself a Tool Master already? Let us know at community@alteryx.com if you’d like your creative tool uses to be featured in the Tool Mastery Series.

 

Stay tuned with our latest posts every Tool Tuesday by following Alteryx on Twitter! If you want to master all the Designer tools, consider subscribing for email notifications.

Attachments
Comments
Alteryx Alumni (Retired)

Updated Use Case 1 to show @PeterS's Run Total method from "Parsing Data with Unknown Number of Fields"

Reformatted Use Case 2 Results to eliminate precision warnings.

Included the latest version of my “Combination” macro, which can also be found on the Gallery here.

Alteryx Partner

Something to remember as well (I got caught on this today), after the cross-tab the columns will be in alphabetic order 😞