Tableau: Aggregate and Non-aggregate error with Date calculations - date

I'm having trouble with a calculation in tableau. I've created some calculations that are listed at the top that I use in the calculation I'm currently working on.
Last 30 days calc:
IF [Date] < (today() - 31) OR [Date] > today()
THEN NULL ELSE [Date] END
Price-AvgWeighted calc:
SUM ([SaleTotal]) / SUM([Qty])
Calculation with ERROR:
IF NOT (ISNULL([Last30days])) AND [Rev]>500 AND QTY > 10 AND [Price] < (.7*([Price-AvgWeighted]))
THEN 'True'
ELSE 'False'
END
ERROR: cannot mix aggregate and non-aggregate arguments with this function. (Highlights the "<")
My goal is to create a calculation that does the following:
"If in the last 30 days revenue is greater than 500 and qty is greater than 10 for all previous sold and the current (available) price is less than .7 of the last 30 day weighted average sales price THEN True/false"
I'm also not sure how to incorporate the Status Dimension (shows type of product: sold/avil)
Any help would be greatly appreciated. Thanks

It looks like you have to turn [Last30days], [Rev], [QTY] and [Price] into aggregations since [Price-AvgWeighted] is an aggregation.
Try the following:
IF NOT (ISNULL(ATTR([Last30days]))) AND SUM([Rev])>500 AND SUM([QTY]) > 10 AND SUM([Price]) < (.7*([Price-AvgWeighted])) THEN 'True' ELSE 'False' END

Related

Get value that is outside scope of filter

I'm working on a tableau project with long format table data displaying start months, end months, and monthly costs. I'm trying to display a visual of a (dynamic) range of dates' monthly costs, and then overlay a reference line of the multi-month row's total cost. I suspect the answer lies in using a level of detail (LOD) expression but can't seem to figure this out.
example csv:
start,end,month length,x,a,b,c,d,total
1/1/2019,1/1/2019,1,1.2,0.08,0.01,0.1,0.299,0.489
2/1/2019,2/1/2019,1,1.1,0.08,0.01,0.1,0.295,0.485
3/1/2019,3/1/2019,1,0.9,0.08,0.01,0.12,0.285,0.495
1/1/2019,2/1/2019,2,2.3,0.08,0.01,0.1,0.297086956521739,0.487086956521739
2/1/2019,3/1/2019,2,2,0.08,0.01,0.109,0.2905,0.4895
1/1/2019,3/1/2019,3,3.2,0.08,0.01,0.105625,0.2936875,0.4893125
ignore x,a,b,c,d - they are to show that the chart has multiple measures equaling the monthly totals.
For identical start and end months, we have a month length: 1. The values associated with these rows are the monthly values. For graphing a dynamic range of months, I can graph these monthly values by filtering the month length: 1 and adding a calculated field filter for setting the start date to end date range [Start Date] >= [Selected Start Date] AND [Start Date] <= [Selected End Date]
Given this table, how might I show a reference line for the total value that exists for the start to end range selected (start date: 1/1/2019, end date: 3/1/2019, month length: 3), even though there is a filter of month length: 1? The end result here would be a line of 0.4893125
Yes, you are correct, you need to use LoD.
If you need to bypass the filter then use LoD Fixed, below code snippet will give you the data for month = 3 though the filter is month=1. Change the code as per your requirements:
{ FIXED : Max(IF [Month Length] = 3 THEN [A] END)}
Edit----------------------------------------------------------------------
{ FIXED :
Max(
IF ([Start Date] >= [Selected Start Date] AND
[Start Date] <= [Selected End Date]) AND
[Month Length] = 3 THEN [A] END)}

Calculate product rate of previous day in tableau

I have a sample dataset:
DATE: 11-01-2015, 12-01-2015, 13-01-2015
SALE: $120, $0 , $100
In tableau I want if Today's sale is 0 as given above for 12-0-2015 then previous day sale should be considered as today's sale.
Can anyone help?
Create a calculated field with this formula:
IF SUM([Sale])=0
THEN LOOKUP(SUM([Sale]),-1)
ELSE SUM([Sale])
END
And compute using Date.
IF FIRST() <> 0 AND SUM([Sale])=0 THEN LOOKUP(SUM([Sale]), -1)
ELSE SUM([Sale]) END
Setting Compute Using to Date
Checking First() in the condition handles the case when the first day had zero sales

Calculate the percent difference between two percentages at two dates in Tableau

The goal is to create a column that shows the percent difference between to percentages.
Tried creating many calculated fields around
-first() - last() -- this just calculates the week number difference
-if date = #2019-02-03# then sum number of records else null end -- invalid
-case when date = #2019-02-03# then sum number of records else null end -- invalid
The 'filter 1' column is where I'd like to display this information.
Expected result:
row 1 in filter 1 should = 12% (absolute percentage change of -5 and 7)
Any help is super appreciated!
I figured this out in two ways:
one created two date fields - one for one week and one for the last week
: then made a diff % measure and used these as the columns.
There's another way, more gnarly:
if first() == 0 then
(
window_sum((if last() == 0 then sum(count_measure) end))
-(window_sum(if first()==0 then sum(count_measure) end))
)
/(window_sum(if first()==0 then sum(count_measure) end))
end

Is there a way to count days INSIDE a range of dates?

I'm quite a beginner on VB/SQL, I just began my learning few months ago, but I can understand the logic of algorithms as I used to do some Excel VBA .
I'm actually designing a database where I can (wish to) follow up every colleague's activity during the year.
The objective is to have a (Monthly) ratio of =>
Billable days / (Billable + Non Billable - Absent)
The context :
A single person can be : Working internally (Non billable), OR Working Externally (Billable) , OR on Holidays (Absent).
I have a [Planning] Table where it stores the following data : [Consultant_ID] (linked to another table [Consultant], [Activity] (A list with the three choices described above), [Beginning_Date], [End_Date].
Example :
Consultant 1 : Working externally from 01/01/2019 to 01/06/2019,
Working internally from 02/06/2019 to 31/12/2019,
Holidays from 02/03/2019 to 15/03/2019
Is there a way to have the Billable ratio of March for example ?
I created 4 queries (Maybe too much ?)
3 queries : [Consultant_ID] [Activity] [Beginning_Date] [End_Date] [Ratio : Datediff("d";[Beginning_Date];[End_Date]).
For each query : The [Activity criteria] : one Working Internally, one Working Externally, one Absent.
And for the [Beginning_Date] and [End_Date] criterias : <=[Enter beginning date], >=[Enter End date]
And the 4th query [Consultant ID] [Billable] [Non billable] [Absent] (and planning to add the [RATIO]).
Problem is : the Datediff counts the dates of the whole activity of what it finds, and not only the dates between 01/03/2019 and 31/03/2019 as I wish to.
I Expect the output of the ratio to be : Billable days / (Billable + Non Billable - Absent) of the desired period.
The actual output shows the billable, non billable, and absent days of the whole period between the dates which are inputted
So instead of 31 Billable, 0 Non billable, 15 Absent
It shows 180 Billable, 0 Non Billable, 32 Absent
Sorry for the long post, it is actually my first, and thank you very much !
I've been struggling with this for a whole week
We first need to figure out the maxBegin and the minEnd dates for each row
SELECT
*,
(IIF (Beginning_Date > #3/1/2019#, Beginning_Date, #3/1/2019#) ) as maxBegin,
(IIF (End_Date < #3/31/2019#, End_Date, #3/31/2019#) ) as minEnd,
Datediff("d", maxBegin, minEnd) + 1 as theDiff
FROM Planning
Where Beginning_Date <= #3/31/2019# AND End_Date >= #3/1/2019#
Then use that to compute the durations. Note: DateDiff does not count both ends, so we need to add +1.
SELECT
Consultant_ID,
SUM(IIF (Activity = "Working Internally", Datediff("d", maxBegin, minEnd) +1, 0) ) as NonBillable,
SUM(IIF (Activity = "Working Externally", Datediff("d", maxBegin, minEnd) +1, 0) ) as Billable,
SUM(IIF (Activity = "Holidays", Datediff("d", maxBegin, minEnd) +1, 0) ) as Absent
FROM
(
SELECT
*,
(IIF (Beginning_Date > #3/1/2019#, Beginning_Date, #3/1/2019#) ) as maxBegin,
(IIF (End_Date < #3/31/2019#, End_Date, #3/31/2019#) ) as minEnd
FROM Planning
Where Beginning_Date <= #3/31/2019# AND End_Date >= #3/1/2019#
) as z
GROUP BY Planning.Consultant_ID;
Finally, you need to substitute the actual Begin/End dates via params into the sql to run your query. Also note that the Holidays are only 14, not 15.
Also, you can add the Ratio calculation right into this sql, and have only one query.

How to dynamically calculate the YTD for current year without using a table calculation in Tableau?

How do I dynamically calculate the Year To Date (YTD) for the current year without using a table calculation in Tableau?
I have used the below formulas to calculate YoY for the current year:
if datediff('year',[Date],TODAY())=0 then [Sales] END
For the previous year:
if datediff('year',[Date],TODAY())=1 then [Sales] END
YoY:
sum(current year)/sum(previous year)-1
create a calculated field:
[date] >= MAKEDATE(Year(today()),1,1) and
[date]<= today()
Drag this to filter and select True
It depends on what you're trying to achieve. If you want to filter dates to show only values in the current year without a table calculation, then you could create a calculated field like below and filter on the result:
if Year([Date]) = YEAR(TODAY()) then "YTD" else "Not" END