1

Say I have city populations for each state / city in the united states. I want to mark each city with over 2 million population as a big city. then if a state contains a "big city", i want to mark that state as a "big state". I made a calculated field:

if Pop>2000000 then "big city" else "small city" end
This works fine.

Now when I try to make a calculated field for state I tried this:

if contains([big city],"big") then "big state" else "small state" end

This almost works, but I get multiple values for each state when I only want 1 value, either big state or small state. How do I stop tableau from creating multiple values?

2 Answers 2

2

You can try that with an approach involving two calculated fields.

Assuming that your big_city, small_city calculation is a calculated field named City_Size

Now, the First Calculated field will assign a 1 or 0 to each row, depeding upon the value of City_Size. Name it as is_big_state

if City_Size = 'big' then
    1
else
    0
end

Now use this calculated field into another calculation, termed as State_type

IIF(Max(City_Size) = 1, "Big State", "Small State")

Edit : You may combine both of them into one :

IIF(Max(iif(City_Size = 'big', 1, 0)) = 1, "Big State", "Small State")
0

Have state set to "small state" by default.

When you check for city population, set the state to "big state" if you find a "big city".

if Pop > 2000000
    then "big city"
         "big state"
else
    "small city"
3
  • Hi Bruce thanks for the reply. I do not believe your equation works in tableau, there is a logical error after your "then" statement (you cannot set big city and big state together like that). Also, this is a simple version of a more complex problem, so I can't use work arounds :)
    – barker
    Jun 22, 2015 at 20:41
  • if contains([big city],"big") then "big state" else "small state" - How about removing else "small state" from your code? That way big states won't get set to big and small states.
    – Srini
    Jun 22, 2015 at 22:22
  • because I also want to label the states that dont have big cities... and my real code is a little more complex anyways, it's more like if value>7 then x elseif value >4 then y else z... so I'm just trying to get the logic in place to develop other things :)
    – barker
    Jun 23, 2015 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.