4 Replies Latest reply on Jul 21, 2016 8:14 AM by Eliran Epshtein

# Tableau code

Hi Everyone,

I was wondering if anybody here could help me with the following problem:

I've created a calculated field in order to have a weighted average of b based on a: sum(a*b)/sum(a). When one of my "a" measurements is null, I'm still getting the right weighted average since the null is both in the nominator and the denominator:

[null*b1 + a2*b2]/[null+a2]= [a2*b2]/a2

Yet, the problem is when one of my "b" measurements is null:

[a1*null + a2*b2]/[a1+a2]= [a2*b2]/[a1+a2]

Is there a way to avoid this wrong calculation in Tableau code? something like

IF b = NULL THEN SUM(a*b)/SUM(a - sum("a where b is null"))

Eliran

• ###### 1. Re: Tableau code

sum(a*b)/Sum(a- (if isnull(B) then a else 0 end))

1 of 1 people found this helpful
• ###### 2. Re: Tableau code

That works perfectly, thanks a lot Khalid!

• ###### 3. Re: Tableau code

Eliran -- I took the liberty of marking Khalid's reply as "Correct Answer".  Usually the person who starts a thread should do that on the reply that solves the question.  Marking a response "correct" lets other participant who are searching for similar answers know where a potential solution can be found.

To mark a response as correct, click the "Correct answer" button in the lower left corner of the response that solved the issue.

• ###### 4. Re: Tableau code

Understood. Will do that next time.