Execute multiple groupby transformations and aggregate functions within Tableau - if-statement

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 &
group2 - old
Within the group1 - new, I would like to then groupby id and take the average of each grouped id and then sum
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
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
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 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
zn(avg(if [Group]= 'Group1' end))
+
zn(sum(if [Group] = 'Group2' end))
any suggestion is appreciated

For the first part i.e. groupby transformations and aggregations please create three calculated fields with the following calculations
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. 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. If you want I can again present you a solution.

Related

How to calculate sum distinct in quicksight

Partner
UserID
Marks
Group
A
1
4
AM
A
2
7
AM
A
1
4
AM
B
3
5
CM
C
4
6
TM
B
3
5
CM
I want to calculate sum of 'Marks' for each partner excluding double rows.
I've tried (sum(maxOver(Marks, [UserID, Partner], PRE_AGG))). But it's giving me a table like :
Partner
Marks
A
15
B
10
C
6
Whereas, I want a table as below :
Partner
Marks
A
11
B
5
C
6
Thank you for your help, cheers!
You can create a calculated field with a countOver() function to detect the duplicate rows, and then use it as a filter in a sumIf() function.
Example:
sumIf({Marks},countOver({Marks,[{Partner},{UserID},{Marks},{Group}],PRE_AGG)=1)

QuickSight : How to display the totals of the category without splitting it by sub totals and not duplicating the rows on the visual

I have a table that looks like below
ID
DT
Main Category
1
19-08-2022
A
2
20-08-2022
B
3
20-08-2022
A
4
19-08-2022
B
5
20-08-2022
A
6
20-08-2022
B
7
19-08-2022
A
8
20-08-2022
A
9
20-08-2022
A
10
20-08-2022
A
I want the output to be like below
Main Category
DT
Count of ID
A
19-08-2022
6
A
20-08-2022
6
B
19-08-2022
3
B
20-08-2022
3
I don't want the count of IDs to be split by the column DT. I have tried using the below functions
countOver({ID},[{Main Category}])
The code requires me to add the column ID on table and also results in duplicates on visuals.
Can someone suggest how can I solve this?

PowerBI: How can I have two different side by side tables scroll at the same time in PBI?

I have two tables:
Table A
id
name
month_1
month_2
month_3
month_4
month_5
month_6
1
John
3
0
1
0
null
null
2
Mary
6
1
2
1
1
2
3
Angelo
1
5
null
null
null
null
4
Diane
3
2
0
1
null
null
Table B
id
name
LastYearTotal
CurrentYearTotal
1
John
2
4
2
Mary
6
13
3
Angelo
9
6
4
Diane
9
6
And then tables A and B will be side by side but not in the same table. Like there will be a separator between A and B. But when I use a filter, both tables will reflect the filter. In addition, there will only be one scroll for both tables so they move at the same time.
Thanks.

cumulative average powerbi by month

I have below dataset.
Math Literature Biology date student
4 2 5 2019-08-25 A
4 5 4 2019-08-08 A
5 4 5 2019-08-23 A
5 5 5 2019-08-15 A
5 5 5 2019-07-19 A
5 5 5 2019-07-15 A
5 5 5 2019-07-03 A
5 5 5 2019-06-26 A
1 1 2 2019-06-18 A
2 3 3 2019-06-14 A
5 5 5 2019-05-01 A
2 1 3 2019-04-26 A
I need to develop a solution in powerbi so in output I have cumulative average per subject per month
For example
April May June July August
Math | 2 3.5 3 3.75 4
Literature | 1 3 3 3.75 3.83
Biology | 3 4 3.6 4.125 4.33
Can you help?
You can use a matrix visualization for this.
Create a month-year variable and use it in the columns.
Use Average of Math,Literature and Biology in values
Under the format pane --> Values --> Show on rows --> Select this
This should give the view you are looking for. You can edit the value headers to your requirement.

Stata: how to duplicate observations under certain conditions

Please help me duplicate a variable under certain conditions? My original dataset looks like this:
week category averageprice
1 1 5
1 2 6
2 1 4
2 2 7
This table says that for each week, there is a unique average price for each category of goods.
I need to create the following variables:
averageprice1 (av. price for category 1)
averageprice2 (av. price for category 2)
such that:
week category averageprice1 averageprice2
1 1 5 6
1 2 5 6
2 1 4 7
2 2 4 7
meaning that for week 1, average price for category 1 stayed at $5, and av. price for cater 2 stayed at 6. Similar logic applies to week 2.
As you could see that the new variables are duplicated depending on a week.
I am still learning Stata. I tried:
bysort week: replace averageprice1=averageprice if categ==1
but it doesn't work as expected.
You are not duplicating observations (meaning here in the Stata sense, i.e. cases or records) here at all, as (1) the number of observations remains the same (2) you are copying certain values, not the contents of observations. Similar comment on "duplicating variables". However, that's just loose use of terminology.
Taking your example very literally
clear
input week category averageprice
1 1 5
1 2 6
2 1 4
2 2 7
end
bysort week (category) : gen averageprice1 = averageprice[1]
by week: gen averageprice2 = averageprice[2]
l
+--------------------------------------------------+
| week category averag~e averag~1 averag~2 |
|--------------------------------------------------|
1. | 1 1 5 5 6 |
2. | 1 2 6 5 6 |
3. | 2 1 4 4 7 |
4. | 2 2 7 4 7 |
+--------------------------------------------------+
This is a standard application of subscripting with by:. Your code didn't work because it did not oblige Stata to look in other observations when that is needed. In fact your use of bysort week did not affect how the code applied at all.
EDIT:
A generalization is
egen averageprice1 = mean(averageprice / (category == 1)), by(week)
egen averageprice2 = mean(averageprice / (category == 2)), by(week)