[英]Execute multiple groupby transformations and aggregate functions within Tableau
I have a dataset that looks very similar to the data below.我有一个看起来与下面的数据非常相似的数据集。
I would like to create two groups using values in the group column.我想使用组列中的值创建两个组。
group1 - new & group1 - 新 &
group2 - old group2 - 旧
Within the group1 - new, I would like to then groupby id and take the average of each grouped id and then sum在 group1 - new 中,我想然后 groupby id 并取每个分组 id 的平均值,然后求和
data:数据:
id type backup free total Last refreshed
a old 1 4 5 11/1/2020
b old 1 4 5 11/1/2020
c old 1 4 5 11/1/2020
d new 2 1 3 11/1/2020
e new 2 1 3 11/1/2020
f old 1 1 2 11/1/2020
g old 1 1 2 11/1/2020
e new 2 2 4 11/1/2020
d new 2 2 4 11/1/2020
1st step - create group1 and group2第一步 - 创建 group1 和 group2
id group type used free total Last refreshed
a group2 old 1 4 5 11/1/2020
b group2 old 1 4 5 11/1/2020
c group2 old 1 4 5 11/1/2020
f group2 old 1 1 2 11/1/2020
g group2 old 1 1 2 11/1/2020
d group1 new 2 1 3 11/1/2020
e group1 new 2 1 3 11/1/2020
e group1 new 2 2 4 11/1/2020
d group1 new 2 2 4 11/1/2020
2nd step groupby id第二步 groupby id
id group type used free total Last refreshed
d new group1 2 1 3 11/1/2020
d new group1 2 2 4 11/1/2020
e new group1 2 1 3 11/1/2020
e new group1 2 2 4 11/1/2020
a old group2 1 4 5 11/1/2020
b old group2 1 4 5 11/1/2020
c old group2 1 4 5 11/1/2020
f old group2 1 1 2 11/1/2020
g old group2 1 1 2 11/1/2020
3rd step we groupby id and take the average of d (for each column) we groupby id and take the average of e (for each column) then we sum the values in the old group (for each column) then we sum these altogether第三步,我们按 id 分组并取 d 的平均值(对于每一列)我们按 id 分组并取 e 的平均值(对于每一列),然后我们将旧组中的值(对于每一列)相加,然后我们将它们相加
id used free total Last refreshed
d 2 1.5 3.5 11/1/2020
e 2 1.5 3.5 11/1/2020
old 5 14 19 11/1/2020
Desired Result期望的结果
All summed together to get:全部加起来得到:
used free total Last refreshed
9 17 26 11/1/2020
graph:图形:
This is what I am doing:这就是我正在做的事情:
2.place the type in groups 2.create calculated field 2.将类型放入组 2.创建计算字段
zn(avg(if [Group]= 'Group1' end))
+
zn(sum(if [Group] = 'Group2' end))
any suggestion is appreciated任何建议表示赞赏
For the first part ie groupby transformations and aggregations please create three calculated fields with the following calculations对于第一部分,即 groupby 转换和聚合,请使用以下计算创建三个计算字段
desired free
{SUM({Fixed [Last Refreshed]: SUM(IF [Type] = "new" THEN
{FIXED [Last Refreshed], [Type], [Id] : AVG([Free])}
ELSEIF [Type] = "old"
THEN {FIXED [Last Refreshed], [Type], [Id] : SUM([Free])}
END)})}
similarly for desired used
and desired total
.对于
desired used
和desired total
也是如此。 You'll get a aggregated table as desired (see screenshot)您将根据需要获得一个汇总表(请参见屏幕截图)
For creating a bar chart (stacked) you'll have to pivot (transform) the data by clubbing used and free into one dimension.要创建条形图(堆叠),您必须 pivot(转换)通过合并使用和释放到一维的数据。 If you want I can again present you a solution.
如果您愿意,我可以再次为您提供解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.