
For her final challenge of #WOW2022, Erica set this interesting challenge; to show how the sales of the top n Products within each Sub-Category compared against the total sales. An added twist was to flip the display between actual sales, and % of total.
It did take me a short while to get my starting point. When I’ve worked with the concept of proportional brushing before, it’s typically involved set actions and one viz driving the interactivity on another viz via a dashboard action. I didn’t have this. I’ve obviously come up with a solution, but I’m not sure if it’s overly complicated… it doesn’t seem it, but for some reason I feel it could have been simpler than I’ve made it. Only time will tell as I check out other solutions.
My solution involves table calcs, so as I do with many challenges, I’ll start by building out all the data I need in tabular form.
Defining the calculated fields
Start off by adding Sub-Category to Rows, Product Name to Rows and Sales into Text and sort descending. We want to identify the Product Names in the top n, so lets first rank the products per sub-category. Create a calculated field
Sales Rank
RANK_UNIQUE(SUM([Sales]))
Make it a discrete field, then add it to Rows between Sub-Category and Product Name. Set the table calculation to compute using Product Name.
The rows should be numbered from 1 upwards, and restart at 1 when the Sub-Category changes

We want to get the sum of the sales for all those products in the top n. We’ll use a parameter to identify the n
pTop
Integer parameter defaulted to 10

Then we’ll use another table calculation to get the sum of the sales in that top n
Sales in Top n
WINDOW_SUM(IF [Sales Rank]<=[pTop] THEN SUM([Sales]) END)
If the Sales Rank is less than or equal to the top parameter, then get the Sales for that row, then sum all the sales values up together.
Show the parameter on screen, and add Sales in Top n into the table, setting the table calc to once again compute using by Product Name.
You should see that the sum of the sales for the rows marked 1-10 should equate to the value in the Sales in Top n column for the Sub-Category

We also need to get the total sales for each Sub-Category, which we can do with
Sub Cat Sales
{FIXED [Sub-Category]: SUM([Sales])}
Format to $ with 0 dp and displayed in k.
With this , we can also determine the proportion of sales
Top n % of Total
[Sales in Top n]/SUM([Sub Cat Sales])
Format this to a % with 0 dp.
Add both these fields to the table, making sure the table calcs are set to compute using Product Name.

So now we’ve got the base fields that form the building blocks for the viz. But we have this added functionality where we want to show either actual values (ie Sales in Top n and Sub Cat Sales), or we want to show the Top n % of Total compared to the ‘whole’ ie 1.
To manage this, we first need the parameter to control the decision
pView
integer list parameter set to 0 or 1, with appropriate display values. and defaulted to Sales Value

We can then build
Measure 1 to Display
CASE [pView]
WHEN 0 THEN [Sub Cat Sales]
ELSE 1
END
and
Measure 2 to Display
CASE [pView]
WHEN 0 THEN [Sales in Top n]
ELSE [Top n % of Total]
END
Pop these into the table and show the pView parameter. Test changing the parameter and see the results. Don’t worry that the % shows 0.. you can format the Measure 2 to Display field to show 2 dp if you want to see the data really changing.

Building the bar chart
On a new sheet, add Sub-Category to Rows, Product Name to Detail and Measure 1 to Display to Columns.

Drag the Measure 2 to Display field onto the Measue 1 to Display axis, and release when the 2 green columns symbol appears

Set the Measure 2 to Display field to compute using Product Name, then move Measure Names from Rows to Colour, and adjust the colours. Ensure Measure 2 to Display is listed first in the colour legend, so the marks are on top. Set stack marks to off (Analysis menu -> Stack marks -> off) to make the bars a single block, rather than being split per Product.

Switch the view parameter to change the display and verify it behaves as expected

Add the Top n % of Total field to Rows and change to be discrete (blue). Ensure the field is set to compute by Product Name still.
Create a new field
Label to Display
CASE [pView]
WHEN 0 THEN [Sub Cat Sales]
ELSE NULL
END
Format this to $ with 0 dp and displayed in k. Add this to the Label shelf

Add Sub Cat Sales to the Tooltip shelf and then adjust the tooltip to match.
The final step (other than formatting) is to apply the sorting. Create a new field
Sort
CASE [pView]
WHEN 0 THEN SUM([Sub Cat Sales]) * -1
ELSE [Top n % of Total] *-1
END
Make this field discrete and then add to Rows in front of the Sub-Category pill. You should now find the bars are being sorted descended depending on the view being displayed

Finally, tidy everything up – Uncheck show header on both the Sort and Measure Values pills. Hide field labels for rows to remove the column headings. Change the font size/format of the first two columns, and adjust the colour of the bar label. Remove all gridlines, column banding etc.

Finally add to a dashboard. I used a horizontal container, text boxes and padding to build up the parameter selections at the top. My published viz is here.
Happy vizzin’!
Donna