This was originally posted on The Information Lab: https://www.theinformationlab.co.uk/2019/02/21/consultants-diary-nested-lod-for-ranking/
I’m going to try and share some of the techniques and methods I use as a consultant. Using Tableau & Alteryx daily means that I’ve found I take small techniques, tips & tricks for granted. Hopefully this will be useful for you all. Table calculations can be mysterious.
When first learning about table calculations, it may be a tricky subject to wrap your head around. One of the most used table calculation is the RANK() function. Rank is a function which comes in many flavours... Yet another reason to pop open that calculation window when you first open a new version of Tableau.
A customer had a very specific question, which they mocked up in the trusty Sample Superstore dataset: How can I Rank this by Technology (because it has the highest selling subcategory (phones)) - then Furniture - then Office Supplies?
A normal rank function calculates that Phones are the highest, but moving the Basic Rank to the left of Category will rank based on Sub-Category.
So the method I thought of getting around this was using a nested level of detail calculation. The idea behind this was I wanted to create a field which took the highest value/maximum sales value for each Subcategory within each Category.
So I wanted to keep the total sales for each Subcategory, but cascade the maximum value within each Category.
Based on Andy Kriebel's excellent blog on understanding FIXED LoD calculations, we can translate my solution into plain English.
So with a { FIXED [Category] : MAX ( {INCLUDE [Sub-Category] : SUM([Sales])}) } we are saying... For each Category, compute the Maximum total sales for each sub-category.
This means that Tableau will first compute and return the values of the SUM(Sales) for each sub-category, and then take the maximum value from this partition for each category.
The FIXED means that the value is returned per row within the category field. This then allows us to use this field with our RANK() table calculation.
This blog was originally published on The Information Lab: https://www.theinformationlab.co.uk/2018/05/08/consultants-diary-calculated-field-pro-tips/ I’m going to try and share some of the techniques and methods I use as a consultant. Using Tableau & Alteryx daily means that I’ve found I take small techniques, tips & tricks for granted. Hopefully this will be useful for you all. Using Tableau almost everyday means you pick up a few shortcuts. Here's some of my favourites when it comes to calculated fields.
Between placements during my time in the Data School, I sat in on some training. Between creating a Pareto whale and supporting DS4, I wrote a quick blog about the 7 ways to create a calculated field in Tableau. Check out this post - and let me know if I've missed any - you'll find some others in the comments on that post.
What I want to share in this particular post is other shortcuts I typically use as I work on a piece of work in Tableau. Let's get started:
Setting up my calculation window
When you install a fresh new version of Tableau Desktop, you are presented with this fun pop up when you create a calculated field (using one of the 'traditional' methods) - but I personally hate this view. I much prefer clicking the little arrow on the right hand side which pops out a menu:
This is for two reasons; the first that you are able to see the explanation of each type of calculation - which I find super useful when both explaining the logic to others, and working out the logic for myself; and second you are able to see the syntax of the function you will use. The fact that each function is organised into sections is also useful, particularly given different functions have different affects on different field types.
Visual Cues
Tableau is full of visual cues - From the small icons for field types, and elements on a measure to indicate different manipulations - I'm thinking the Table Calculation triangle, the Set venn-diagram and the Forecast arrow.
Within the calculated field window, this is no different.
Let's take the above as an example;
[State] and [Sales] are orange given they are fields within the data source
'New York' is grey given it is a string
SUM( ) is blue given it is a function
Other examples include:
Purple for parameters
Black or grey for 'literal' functions - for example #2018-05-01# returns the ISO formatted date or TRUE/NULL, as well as numeric literals (ie. 1.45 or 12)
Documentation Pro-Tips
When documenting calculated fields there are two pro-tips I'd like to share about adding comments.
The first one you might perhaps know about - this is where you use two forward slashes (//) to write a comment within the calculated field. This is also useful for troubleshooting whether a long nested calculation is returning what you are looking for - by using // to 'grey out' parts of the calculation, and testing what value is being returned by each part.
The second is something I found when preparing for my Tableau Desktop Certification;
With this function, you're able to add a comment to your calculated field;
Which looks like this when you hover over the calculation.
A useful example of this is to either give a quick indication of what the calculation is doing, or what is inside it:
This blog was originally published on The Information Lab: https://www.theinformationlab.co.uk/2015/12/23/xml-mas-fixing-error-parsing-xml-line-2/
I recently came across an issue which I had never seen before, and I thought I'd share my experience and the fix that I found using the Tableau .xml which lies beneath the workbook. This issue itself is quite rare, but if it does occur then this is one approach to solving this particular problem.
Sometimes Tableau becomes corrupt; one such issue is the "Error parsing XML at line <insert any number here> of file..." when opening your workbook. This is when you think you've really screwed up. Tableau's Knowledge Base article recommends you send it over to them if the workbook is shown as bigger than 0kb which can take time to send over and get back. Though, as that article says, if your workbook says it's 0kb... You gotta start over.
But if it is a file that's bigger than 0KB then all is not lost! You can try this solution if it works for you, but in order to carry out this solution, you will need;
Your Tableau workbook in .twb format
Note: if you have saved it as a .twbx file, all you need to do is navigate to where you've stored it on your computer, right-click and select 'Unpackage' and it will partition the Tableau workbook (.twb) from the rest of the data.
The example I'm about to show uses Notepad++ as the XML editing tool
Got these? Good! Let's go and rescue that workbook.
I'm going to use my own case study here as an example.
When I was presented with this issue, I tried a host of different things before deciding to open the .twb in Notepad++.
But why would you open it in Notepad++ the first place? And what exactly is an XML of a Tableau Workbook?
As this response on the Tableau Community (which is an awesome place to troubleshoot, learn and hone your Tableau skills) by Jonathan Drummey states, a .twb file is an XML file which contains the metadata for a Tableau workbook.
When you open it, it looks a bit like this:
This XML of the .twb includes data source connections, dashboards, worksheets, colours used, parameters and virtually everything that allows the workbook to function. When I opened the workbook and navigated to the line which threw the error (My error message stated 'Error parsing XML at line 411386...'), this is the view which greeted me.
Huh - These are 'NULL' values at the end of the XML, highlighted above -
Perhaps it was trying to write some more XML before the computer crashed or something similar. The first thing I tried was to simply delete all the NULL files on this line and open it up again. No dice.
Then after racking my brain a little, I thought I'd compare the end of this .twb to another 'healthy' .twb file that I had. What the XML of *that* file looked like is below.
Bingo! This is the solution I'm after.
What seems to have happened is, as the computer crashed and the file has become corrupt, the final wrappings of the metadata hasn't been completed, meaning the XML file is left open ended and incomplete. I've highlighted and blown up what I mean by wrappings with the xml tags at the end of the workbook.
To test this theory, I copied the three parts here; the end parts (shown above) and pasted it into my damaged .twb to replace where the NULL symbols were.
And just like magic, the workbook sprung back to life!
I probably should add that I'm sure there's more detail to working with the XML of a Tableau Workbook than I have described here, and I should say that this XML is super important so shouldn't be overly messed around with if you're unsure what consequences what you're doing will have. The only other time I had encountered a use for this method to look 'under the hood' of a .twb file, was to change all instances of the '9.1' to '9.0' during customer projects while at the Data School. This trick meant that if we'd accidentally created something in the Tableau 9.1 beta and wanted to share it with the customer after the project was done, they would be able to open the file in Tableau 9.0.
But hopefully this will solve at least one other person's problem when presented with the 'Error parsing XML at line X of the file: no element found' in Tableau.
This blog was originally posted on The Information Lab: https://www.theinformationlab.co.uk/2015/11/04/fixed-level-detail-intricacies-case-study/ Preamble
Level of detail calculations are super duper tricky - Having used Tableau for the last four months at an intensive pace and level, I find myself steering away from level of detail where I can. So when an instance occurs when I need to use it, I either find myself thinking it through with minute detail, or referring back to a session with had the Queen of LOD's, Bethany Lyons - My notes actually come in handy in this instance!
As some of you may be aware, the Data School have already recruited for the next cohort of Data Padawans that The Information Lab are looking to mould into awesome Data Consultants. This intensive process included (as you can read here) a simple Tableau creation process, and followed by each interviewee getting given a similar data set. The beauty of this? Not only is it a chance to experience the world of Tableau and really get to grips with how it works, but it is also an opportunity to ask questions, learn and develop your skills as Andy and the rest of the team are always happy to answer questions, critique and help the interviewees during the process. This is an amazing opportunity for feedback, especially (in my experience anyway), a large amount of applications and notes made during the process disappear into the netherworld, never to be seen or heard from again. The transparency is amazing.
The Exercise
Back to the purpose of this post - As a warm-up a few weeks ago as the Data School training was winding down, Andy Kriebel set us a challenge to answer a question one of the candidates had asked; how to work out the minimum and maximum life expectancy for;
Each Local Authority
Each Gender
For every date range
Where there are no nulls, the metric used is average, and the age category is 'at birth'
The reason for doing analysis like this could be to see whether or not there is a change in the movement of the distance between the highest and lowest 'years left to live' value. Which is actually a good idea, as if this gap widens in some areas in the same year as it gets smaller in others, then perhaps one ward is more successful than others and their practices should be implemented.
So creating something like this (here we have the basic calculations of MIN and MAX in the viz) should work right?
No - This doesn't seem to work as the visualisation is returning the same value for minimum and maximum for each one of these marks, as it's the lowest level of granularity. The difference is obvious when you take things at the local authority level. But the question, if you remember is this; if you rank the ward by any measure then you can't compare the difference in gender, for instance or see the minimum or maximum for the life expectancy (years left to live) measure.
The Action
What we want for this one is to not allow Tableau to aggregate at the default level, but instead to determine at which level of detail Tableau should be running it's calculations.
So the 'level of detail' that needs to be aggregated up & fixed to is at the Local Authority and Gender level. So my first attempt used a Fixed level of detail.
Fixed aggregates at the specific level of detail of the expression, regardless of what is present in the view. With this in mind, my first attempt was this.
Max LtL as { FIXED [Local Authority], [Gender] : MAX([Years Left to Live]) } for the maximum value
Min LtL as { FIXED [Local Authority], [Gender] : MIN([Years Left to Live]) } for the minimum value
LtL Diff as [Max Ltl] - [Min Ltl]
And the is what the result I was given was.
This seems a remarkable high range, even at birth - It doesn't make sense for the minimum life expectancy for a male or female to be 70 years apart from the maximum level of detail given. For instance in the example above, we can see that for the years 1999-2003, a female in the Eastbrook ward of Barking & Dagenham, the minimum years left to live is 14.9 years; a difference of almost 70 years.
All the values for the Local Authority are now at the same level of detail, but the result definitely can't be right! (I hope!)
However, this doesn't give the correct answer because in order for the fixed level of detail to work, all dimensions which are being addressed need to be in play within the calculation - so instead, our LOD calculation would be;
Max LtL as { FIXED [Local Authority], [Gender], [Metric],[Age Category],[Date Range] : MAX([Years Left to Live]) } for the maximum value
Min LtL as { FIXED [Local Authority], [Gender], [Metric],[Age Category],[Date Range] : MIN([Years Left to Live]) } for the minimum value
LtL Diff stays as [Max Ltl] - [Min Ltl]
Giving us this
Much better. Now in the years 1999-2003, a female in the Eastbrook ward of Barking & Dagenham has a minimum life expectancy of 77.4 years, with a small difference of 5.1 years. This seems to be correct.
The Lesson
So, the question was how to work out the minimum and maximum life expectancy for;
Each Local Authority
Each Gender
For every date range
Where there are no nulls, the metric used is average, and the age category is 'at birth'
And in order to do this, all the dimensions (blue pills) in the view must be included in the Level of Detail calculation. As you can see, before the colon you have every pill which is in the view. In order for the calculation to work, every dimension which is in the view must be taken in the calculation. This makes sure that all of these are held as fixed, meaning that the calculation is left at this high level of detail.
After conversing with Chris Love about this post, he mentioned that the first attempt can also be remedied using context filters. This is because the FIXED level of detail calculation doesn't take filters into account unless they are in context. So adding Metric and Age Category to context also works in this instance.
The reason for sharing this was to show an example where understanding how the " { FIXED " function can be used, but also to highlight once more that within Tableau, there is more than one way to reach a solution.
Have a look at the workbook I've uploaded Tableau Public; click on the image below to have it pop up.
It's got the two sheets - My first attempt, and second, with the data attached for you to explore further and understand the exercise.
If I've made any errors in how I've communicated or explained this, please let me know so I can edit the post accordingly, and more importantly, carry on learning.