6 Replies Latest reply on Jan 19, 2016 9:40 AM by Rajeev Pandey

# Calculated Field - Nested Calculation in If Then Statement

Hi all,

Having problem with a calculated field I'm trying to create and haven't been able to find any clarification via the oracle.

Ideally, it would look something like the below but the calculation shows as invalid:

IF [Ship Type] = "Bulk" THEN 5*[Size]^12

ELSEIF [Ship Type] = "Tanker" THEN 4*[Size]^16

END

It doesn't like that i'm trying to enter a calculation after the THEN. Does anyone know how to fix this or can think of any work around?

Cheers, Scott.

• ###### 1. Re: Calculated Field - Nested Calculation in If Then Statement

Dear Scott.

Could you please try the below approach and let me know your opinion on this IF ATTR([Ship Mode]) = "Bulk" THEN 5*(SIZE()^12)

ELSEIF ATTR([Ship Mode]) = "Tanker" THEN 4*(SIZE()^16)

END

For superstore dataset,

Shipmode will be either one of three

Delivery Truck

Express Air

Regular Air • ###### 2. Re: Calculated Field - Nested Calculation in If Then Statement

Hi Rajeev,

Almost there, but i think I've lead you astray a little.

When i wrote size, that is actually a field I'm using rather than a function. Apologies for the confusion. Perhaps we should use [Ship Size] for this exercise.

It worked when i put a basic formula in at the end (such as 5*12) but I still can't get the above formula to work with my tinkering.

Any ideas?

• ###### 3. Re: Calculated Field - Nested Calculation in If Then Statement

Hi Scott

Question - is SIZE an existing field or the function, SIZE()?           @Rajeev Pandey < I suspect its an existing field, not the function, more below.

I assume its an existing field, and therefore assume the issue with your calculation is that Size is a String rather than a Integer.

Does it look like this? If so Tableau is trying to do something like this - 5*randomtext^12 < hence the error.

Right click SIZE and Change Data Type swap to Integer (Number)

Your calculation should now work.

PS. There is a POWER function.

Why I think that's going to work... In Superstore Sales is a Number/ Measure. Cheers

Mark

• ###### 4. Re: Calculated Field - Nested Calculation in If Then Statement

When i wrote size, that is actually a field I'm using rather than a function.

Thought so!

Try my suggestion... Sorry crossed over with your reply.

• ###### 5. Re: Calculated Field - Nested Calculation in If Then Statement

Thanks Mark,

I was originally using the field in my initial attempt - see the [ ].

Whilst fiddling, I scrapped and re-wrote the original to try and match your valid calculation and somehow it worked. I've no idea what i was doing wrong the first time but got there in the end!

Thank you both for your help!

• ###### 6. Re: Calculated Field - Nested Calculation in If Then Statement

Sorry I misunderstood the situation!!