samedi 21 novembre 2020

Group calculation in Tableau

I have some data where I would like to create a query that will perform 2 separate calculations based on the group id in tableau

Here is my data

id  total   avail
A   10  5
A   40  20
B   20  10
B   10  5
B   10  5
A   20  10
A   20  10
B   10  5
B   10  5

I wish to group all the 'A' ids and sum them (Group1) and I wish to group all the 'B' ids and Average them. (Group2)

This is what I am doing:

first group the IDs:

IF [Id] = 'A' THEN 'Group1'
ELSEIF [Id] = 'B' THEN 'Group2'
ELSE 'none'
END

Now perform calculation:

IF ATTR([group]) = 'Group1' THEN  SUM('A') ELSEIF
ATTR([group]) = 'Group2' THEN  SUM(['B'])/SUM(['B']) ELSE 0
END

enter image description here

However, this is not correct. I am still researching this. Any suggestion is appreciated.

Aucun commentaire:

Enregistrer un commentaire