![](/img/trans.png)
[英]Resolve aggregate and Non-aggregate mix error for IF results in Tableau
[英]Execute multiple groupby transformations and aggregate functions within Tableau
我有一個看起來與下面的數據非常相似的數據集。
我想使用組列中的值創建兩個組。
group1 - 新 &
group2 - 舊
在 group1 - new 中,我想然后 groupby id 並取每個分組 id 的平均值,然后求和
數據:
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
第一步 - 創建 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
第二步 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
第三步,我們按 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
期望的結果
全部加起來得到:
used free total Last refreshed
9 17 26 11/1/2020
圖形:
這就是我正在做的事情:
2.將類型放入組 2.創建計算字段
zn(avg(if [Group]= 'Group1' end))
+
zn(sum(if [Group] = 'Group2' end))
任何建議表示贊賞
對於第一部分,即 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)})}
對於desired used
和desired total
也是如此。 您將根據需要獲得一個匯總表(請參見屏幕截圖)
要創建條形圖(堆疊),您必須 pivot(轉換)通過合並使用和釋放到一維的數據。 如果您願意,我可以再次為您提供解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.