Friday, July 17, 2015

Creating Bins Made Easy

This is a sequel to Coding Case Statement Made Easy.  This topic came up when a colleague needs to create 25 uniform bins on percentiles. Quite a bit of manual work, isn't it?

It is pretty easy if you create bins of equal size on regular measures or dimensions. You can use the right-click context menu "Create Bins" in Tableau to create them.

It becomes tedious when working with aggregated measures (no context menu available for creating bins), especially when the number of bins is big.

The expected bins are like those in the picture:
There are in total 25 bins between 0 and 1. It takes quite sometime to do it by hand. The accuracy of the code is not guaranteed.

So, Excel can do this pretty handily.

1.Create column A for the bin ticks. Excel can do it by incrementing 0.4 per row. Just by dragging down.
2.Create column B of bin labels from 1 to 25. By dragging down again.
3.Create column C for the code based on A and B through the function Concatenate(), plus dragging down.
Then copy the code to the bin creation formula with some edit.

This applies equally to creating variable-size bins. Then you have to create columns A and B manually.

PS.I thought everyone knew this trick in Excel. But it seems not so. Hope this helps.

5 comments:

  1. This method is exactly what I use for variable sized bins. For equal-sized bins like in the example I use math rather than a long IF/ELSEIF: In Tableau v9 we can use the CEILING() function now (I'm not sure if it's supported in all sources) and that enables the 25 line IF/ELSEIF to be rewritten as CEILING([UniqueRank]/4 * 100, 1). When CEILING is not available, we can use the workarounds from http://community.tableau.com/docs/DOC-1354 and the formula could be written out as IIF([UniqueRank]*25 = INT([UniqueRank]*25), [UniqueRank]*25, INT([UniqueRank]*25)+1).

    ReplyDelete
    Replies
    1. This is awesome!
      There is a typo. CEILING([UniqueRank]/4 * 100) not CEILING([UniqueRank]/4 * 100, 1)
      In case of uniform bins, using the math formula as a calc field, we can create the bin labels. We don't need Excel.

      The above method is best used for variable-size bins.

      Delete
    2. Jonathan, you wrote CEILING([UniqueRank]/4 * 100). Why not CEILING([UniqueRank]*25)? Any caveat?

      Delete
    3. The Ceiling(Percentile*25) approach doesn't work when Percentile is a table calculation. That limits its use. But the formula with IIF() is always working. See workbook https://public.tableau.com/views/CreatingBinsStudy/Sheet12?:embed=y&:display_count=yes&:showTabs=y

      Delete
    4. Wrote a note on creating bins using formula http://vizdiff.blogspot.com/2015/07/create-bins-via-math-formula.html

      Delete