10 Replies Latest reply on Jan 25, 2014 5:33 AM by Shawn Wallwork

# Case statement for stacked bar graph

Hi There,

I am new to tableau and have difficulty with the statement below. I need to be able to count how many items there are of each within each range and then use that to create a stacked bar graph showing the percentage fr each category.

This is the error message I receive: Syntax Error(Maybe your are missing and identifier)

CASE [Items]

WHEN <= 90 THEN 1

WHEN > 90 AND <= 95 THEN 1

WHEN > 95 AND <= 100 THEN 1

WHEN > 100 AND <= 105 THEN 1

WHEN > 105 AND <= 110 THEN 1

WHEN > 110 AND <= 120 THEN 1

ELSE 1

END

• ###### 1. Re: Case statement for stacked bar graph

Greg in Tableau you have to refer to fields explicitly. Also you are looking for an IF ELSEIF THEN

Something like this:

IF [Item]<= 90 THEN 1

ELSEIF [Item]> 90 AND [Item]<= 95 THEN 1

ELSEIF [Item]> 95 AND [Item]<= 100 THEN 1

...

ELSE 1

END

--Shwan

1 of 1 people found this helpful
• ###### 2. Re: Case statement for stacked bar graph

Thank for the reply Shawn.

I applied the suggestion and now receive another error message:

"Expected type float, found boolean. Comparrison in 'CASE' expression must be float type."

Apolgies I do not have programming type background and I gues this would be easy to correct for someone who does.

• ###### 3. Re: Case statement for stacked bar graph

"Expected type float, found boolean. Comparrison in 'CASE' expression must be float type."

This error message seems to indicate you didn't change from using a CASE statement to using an IF ELSEIF THEN statement. Change to an IF statement and the logic should work.

--Shawn

• ###### 4. Re: Case statement for stacked bar graph

Thanks Shawn

• ###### 5. Re: Case statement for stacked bar graph

How do I set this up, so that for each person I can see the % ITEMS for each person, ie, <=90, >90 and <=95, >95 and <=100, etc.

• ###### 6. Re: Case statement for stacked bar graph

Set your compute using to Person.

--Shawn

• ###### 7. Re: Re: Case statement for stacked bar graph

Sorry Shawn, I have case of Beginners Blues

In the attached Excel sheet is an example of the graph I am trying to reproduce. I have attached a workbook as well.

Any help is appreciated.

• ###### 8. Re: Re: Re: Case statement for stacked bar graph

Greg, you were close. See attached. Let me know if you have any questions.

--Shawn

PS: Posting a packaged workbook is almost always the easiest, quickest way to get your question answered. It let's us better understand what you are trying to accomplish.

• ###### 9. Re: Re: Case statement for stacked bar graph

Shawn, thank tou very much. Got it working. I appreciate you taking the time to help me.

• ###### 10. Re: Re: Case statement for stacked bar graph

Excellent, glad to hear it.

One of the Tableau concepts that took me a long time to get my head around was how measures and dimensions work and interact. Generally speaking:

• Measures aggregate stuff
• Dimensions divide (slice) up stuff

For instance drag a sales measure into a new viz and you'll get the sum of all sales. Then drag any dimensions into the viz, and the sum of sales will get sliced into pieces by however many items are included in that dimension.

In your case the first thing I did was drag your original IF ELSEIF statement from the measures window to the dimensions window, and then drag it onto the color shelf. This sliced up your bars just like the final solutions does (which is why I said you were close), but it made for a pretty ugly color legend. Changing it so it produced a text result instead of a number result fixed this.

Understanding the way Dimensions affect Measures will go a long way toward keeping the Beginner Blues at bay.

--Shawn