How modify and use totals in tableau - tableau-api

I have several measures names and 5 dates and I need to create a calculated field over the first 4 dates and use the result in news calculated fields with the 5 date
date1 date2 date3 date4 date5 AVGdate1-4
measure1
measure2
.
.
measureN

Related

How to Average by day of week in Google Sheets?

Given a table of dates and values.
How to average by day of week?
I found AVERAGEIF to average and TEXT(E4, "dddd") to convert to day of week.
But how to combine those two functions?
Date
Value
1/1/2001
1
1/2/2001
2
1/3/2001
3
1/4/2001
3
1/5/2001
6
1/6/2001
3
Day of Week
Average
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Go with QUERY() function. Try-
=QUERY({INDEX(TEXT(A1:A15,"dddd")),B1:B15},
"select Col1, avg(Col2) group by Col1 label Col1 'Day', avg(Col2) 'Average'")
To make it dynamic, use-
=QUERY({INDEX(TEXT(TOCOL(A1:A,1),"dddd")),TOCOL(B1:B,1)},
"select Col1, avg(Col2) group by Col1 label Col1 'Day', avg(Col2) 'Average'")

Google Sheets: averaging duration data per weekday

I've got date/ duration data, e.g.:
16-Apr (Mon) 30mins
16-Apr (Mon) 90mins
17 Apr (Tue) 60 mins
19-Apr (thu) 15 mins
19-Apr (thu) 20 mins
19-Apr (thu) 20 mins
21-April (Sat ) 120 mins
23-April (Mon) 60 mins
24-Apr (Tue)15 mins
I want to produce an average duration per weekday
A pivot table with date can give me a sum of the durations for each date.
A pivot table on (derived) Day of the week gives me an average for the number of entries for that day of the week, e.g. in the above, Monday = 3 entries and I want an average from 2 (dates).
What I want is Mondays = x mins, Tues= y mins, Wed =z mins, etc.
Can this be done in one step with a pivot table or array formula?
Suppose you have your dates in column E, and durations in F
This formula will give you a summary by day of week:
=query({arrayformula(weekday(E2:E7)), arrayformula(n(F2:F7))},
"select Col1, sum(Col2) group by Col1")
To get an average, use avg(Col2) instead of sum(Col2). You still have to format the second result column as Duration, and find a way to convert the days of week from numeric to text, but that can be left as an exercise to the reader :-)
You could do this to add labelling
=ArrayFormula(query({hlookup(weekday(A:A),{1,2,3,4,5,6,7;"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},2,false),B:B},"select Col1,avg(Col2) group by Col1 label Col1 'Day'"))
The day numbers in column C are just for checking.
If any of the dates are missing, you need to ignore them otherwise they will all be treated as Saturdays...
=ArrayFormula(query({A:A,hlookup(weekday(A:A),{1,2,3,4,5,6,7;"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},2,false),B:B},"select Col2,avg(Col3) where Col1 is not null group by Col2 label Col2 'Day'"))
EDIT
OK this is an answer for the actual requirement which is for sum of time durations for each weekday but divided by number of unique dates which fall on that weekday.
=ArrayFormula(query({query({C2:C,weekday(C2:C),choose(weekday(C2:C),"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"),E2:E},"select Col3,sum(Col4) where Col1 is not null group by Col2,Col3"),
query({unique(C2:C),weekday(unique(C2:C))},"select count(Col2) where Col1 is not null group by Col2")},"Select Col1,Col2/Col3 label Col1 'Day',Col2/Col3 'Average'"))
Notes
(1) The number of groups based on unique dates is exactly the same as the number of groups based on dates with duplicates.
(2) To get the day names in the correct order (Sunday, Monday, Tuesday...) I grouped on weekday number (1-7) then weekday name (Col2,Col3 in the first inner query). This doesn't create any more groups than just using Col3, but has the effect of putting the days in weekday number order instead of weekday name (alphabetical) order while still allowing you to put Col3 (weekday name) in the select list.
(3) Have included #ttarchala's recommendation of using Choose rather than Hlookup.

DB2 get max date of four dates query

I want to get greater date from four date columns and fields can be NULL.
Please help me to write a query for this.
Example:
select max(date1, date2, date3, date4) from table A where tag_id='xxxxx'
You can work with unions which combine your query results and then get the max date of the combined columns
Here is the same question asked in another post
Hope that helps you
select max(max_date) from
(select date1 max_date from table A where tag_id='xxxxx'
union all
select date2 from table A where tag_id='xxxxx'
union all
select date3 from table A where tag_id='xxxxx'
union all
select date4 from table A where tag_id='xxxxx')
Here you have:
SELECT
CASE
WHEN date1 >= date2 AND date1 >= date3 THEN date1
WHEN date2 >= date1 AND date2 >= date3 THEN date2
WHEN date3 >= date1 AND date3 >= date2 THEN date3
ELSE date1
END AS RecentDate
FROM table A
WHERE A.tag_id='xxxxx'
Hope it helps,
Did you try your example query?
MAX() (and MIN() for that matter) are a bit unusual....they exists as both an aggregate function operating on multiple rows and a scalar function operating on multiple columns.
The works fine for me on DB2 for IBM i:
select max(dte1,dte2,dte3,dte4) from qtemp.test

Jasper reports table

I created a table with jasper, the table is created fine but I only want to jasper shows the table from:
Group1
id Date
1 date1
2 date2
3 date3
Group1
id Date
1 date1
2 date2
3 date3
to:
Group1 Group2
id Date id Date
1 date1 1 date1
2 date2 2 date2
3 date3 3 date3
I checked the value group's porperty "start new column" but this not work fine and the result is like the first example
Thanks

Calculating Date ranges in T-SQL

I have a date field that I would like to results returned where the date is minus a given amount of days. For example, WHERE #today - 30 would give me the last 30 days of data. How can I do this?
For calculating date ranges you can use:-
WHERE date_column BETWEEN date1 AND date2
Now for subtracting you can use:-
DATEADD(DAY, -30, '9/1/2011')
something like:=
WHERE datecolumn BETWEEN dateAdd(day,-30,date1) AND date2
You can use dateAdd. The syntax of day may vary based on your DBMS
WHERE datecolumn BETWEEN dateAdd(day,-30,getDate()) AND getDate()
or
WHERE datecolumn > dateAdd(day,-30,getDate())