Is it possible to count the number of occurrences of a calculated field's result in Tableau? - tableau-api

I have a function that categorizes calls a user has made into 3 categories using this calculation:
IF 0 <= DATEDIFF('dayofyear', [SubmittedDateTime], [CALLDATE])
AND DATEDIFF('dayofyear', [SubmittedDateTime], [CALLDATE]) <= 7
THEN "Week After"
ELSEIF -7 <= DATEDIFF('dayofyear', [SubmittedDateTime], [CALLDATE])
AND DATEDIFF('dayofyear', [SubmittedDateTime], [CALLDATE]) < 0
THEN "Week Before"
ELSE "Not within a week"
END
I was wondering if it's possible to count the number of occurrences of a particular outcome of the function on a per user basis in order to then categorize each user based off of the number of occurrences. I'm attempting to use this calculation to do so:
IF { FIXED [SUBID]: COUNT([DateDiff Calc] = 'Week After')} = 1
THEN "1 Conference User"
ELSEIF { FIXED [SUBID]: COUNT([DateDiff Calc] = 'Week After') } > 1
THEN "Multiple Conference User"
ELSE "0 Conference User"
END
but the COUNT function I'm using is not working properly it seems. It seems that the COUNT function is also counting occurrences of both "Week Before" and "Not within a week" from the first function.

I think the problem is the measure part of your LOD expression :
COUNT([DateDiff Calc] = 'Week After')
This will just give you count of both times: when your conditions is met and when its not met. [DateDiff Calc] = 'Week After' will return true or false, both will be counted as +1 in the count function.
You could try something like:
IF { FIXED [SUBID]: SUM(IF[DateDiff Calc] = 'Week After' THEN 1 ELSE 0 END)} = 1
THEN "1 Conference User"
...

Related

Count rows that meet certain requirements and then subtract that number from another count

Let's say I have data like this:
Type
OrderNumber
Priority
DeliveryMethod
Boxes
1
High
UPS
Misc
1
High
UPS
Boxes
2
Standard
InstaBox
Boxes
3
Standard
UPS
Boxes
3
Standard
UPS
Boxes
3
Standard
UPS
Boxes
4
Standard
Instabox
Boxes
5
Standard
Instabox
Boxes
5
Standard
Instabox
Boxes
6
Standard
UPS
Boxes
7
Standard
UPS
And I want to count all the so called "Private Orders". They are boxes with an unique ordernumber and a Standard priority and UPS delivery.
(There's 2 in this example, 6 and 7)
Then I want to count all the boxes with Standard priority and subtract all the Private orders. (There's 9 boxes with Standard priority, minus the 2 Private orders = 7.)
Is that possible in Tableau? I want to display the number 7 in a textbox.
For Private orders use this field
IF
{FIXED [Type], [Ordernumber] : COUNT([Ordernumber])} = 1
AND [Type] = 'Boxes' And [Priority] = 'Standard' And [DeliveryMethod] = 'UPS'
THEN 1 ELSE 0 END
For all boxes use this
Sum(
If [Type] = 'Boxes' And [Priority] = 'Standard' then 1 else 0 end
)
SO FOR FINAL OUTPUT YOU MAY USE THIS DIRECTLY say calculation2
Sum(
If [Type] = 'BOXES' And [Priority] = 'Standard' then 1 else 0 END
) -
Sum(IF
{FIXED [Type], [OrderNumber] : COUNT([OrderNumber])} = 1
AND [Type] = 'BOXES' And [Priority] = 'Standard' And [DeliveryMethod] = 'UPS'
THEN 1 ELSE 0 END)
For matching pattern change the above field to say calculation3
Sum(
If [Type] = 'BOXES' And [Priority] = 'Standard' then 1 else 0 END
) -
Sum(IF
{FIXED [Type], [OrderNumber] : COUNT([OrderNumber])} = 1
AND [Type] = 'BOXES' And [Priority] = 'Standard' And REGEXP_MATCH([DeliveryMethod], "UPS")
THEN 1 ELSE 0 END)
the data used
Results

Tableau: last value calculation in calculated field

my dataset is like this
KPI VALUE TYPE DATE
coffee break duration 11 0 30/06/2015
coffee break duration 12 0 31/07/2015
coffee break duration 10 0 30/11/2014
coffee break duration 10 0 31/12/2014
coffee expense 20 1 31/07/2015
coffee expense 20 1 31/12/2014
coffee consumers 15 -1 31/07/2015
coffee consumers 17 -1 31/12/2014
for Type, 0 means minutes, 1 means dollars and -1 means people
I want to get a table like this
KPI Year(date) YTD
coffee break duration 2015 11,5
coffee break duration 2014 10
....
YTD calculation is:
if sum([TYPE]) = 0 then avg([VALUE])
elseif sum([TYPE]) > 0 then sum([VALUE])
elseif sum([TYPE]) < 0 then [last value for the considered year]
end
By [Last value for the considered year] I mean the last entry available, in a year if my table is set to Year, otherwise it has to change dynamically based on what Timespan I want to show.
What can I do to have [last value for the considered year] as a calc field ready to use in my YTD calc?
Many thanks,
Stefania
If I understand your question, than you can use LOD in the IF statement
if sum(type) = 0 then avg([value])
elseif sum([type]) > 0 then sum(value)
elseif sum([type]) < 0 then max(if [date] = { INCLUDE kpi: max(date)} then [value] end)
end
If there are several values on the last day of the considered year, it would take the biggest value
I slightly modified your data to show that results are working correctly

Check whether the sum in a list is greater than 100

I have to write a drool function on a list which has to do the following thing
create a summation
check whether the summation is greater than 100.
below is the drool rule I have created
rule "001"
when
$charge : MainClass(subList.size() > 0)
$item : SubListClass(number < 0) from $charge.subOrderROList
$total : Number() from accumulate(SubListClass( $p : number ),sum( $p )
then
int index = $charge.SubListClass.indexOf($item)+1;
violations.error(kcontext, "ad", "ad.message", new String[]{String.valueOf(index),$item.getNumber().toString()},index);
end`
I am not able to check whether the $total is greater than 100
thanks
This would be correct if it were possible to obtain a sum > 100 by adding negative numbers. I have kept the constraints as they were in the Q, so change this as appropriate. Maybe number > 0?
rule "001"
when
$charge: MainClass(subList.size() > 0)
$total: Number( intValue > 100 )
from accumulate( SubListClass($p: number < 0)
from $charge.subOrderROList,
sum( $p ) )
then

Calabash: Select a date from a UIDatePickerview

In Calabash Automation testing on iPhone. I need to select a date from a date picker. Please guide me with a ruby step definition.
I want something like
Then I scroll datepicker to date "2002-10-22"
I made a solution after some research. This code is not bulletproof but it works fine for me. I guess someone will get a help from this. if anyone knows how to improve this. please add it here.
In step definitions I add..
Then /^I scroll datepicker to date "1985-01-01"
# Date Format "1985-01-01"
# make sure date picker is up
should_see_date_picker()
is_picker_in_date_mode()
maxdate = picker_maximum_date_time()
target = Date.parse(target_date)
current = Date.parse(datequery())
if(maxdate<target)
screenshot_and_raise "Target date'#{target}' is larger than maximum date'#{maxdate}' in date picker"
end
limit = 100
# => set year
count = 0
dir = (target.year < current.year) ? "down" : "up"
until (target.year == Date.parse(datequery()).year) or (count==limit) do
date = Date.parse(datequery())
scroll_date_component(dir, 2, date.year)
# puts("Inside the loop1 '#{count}'=> '#{date.year}'" )
count += 1
end
# => set month
count = 0
dir = (target.month < current.month) ? "down" : "up"
until (target.month == Date.parse(datequery()).month) or (count==limit) do
date = Date.parse(datequery())
scroll_date_component(dir, 0, date.month)
# puts("Inside the loop2 '#{count}'=> '#{date.month}'" )
count += 1
end
# => set day
count = 0
dir = (target.day < current.day) ? "down" : "up"
until (target.day == Date.parse(datequery()).day) or (count==limit) do
date = Date.parse(datequery())
scroll_date_component(dir, 1, date.day)
# puts("Inside the loop3 '#{count}'=> '#{date.day}'" )
count += 1
end
end
#########################################################
# => ##### Date picker helper methods. #####
def scroll_date_component(direction, column, component)
if(column==0)
# => Month scroll needs the month name string
if (direction.eql? "up")
month_str = Date::MONTHNAMES[component+1]
touch("pickerView scrollView index:#{column} label text:'#{month_str}'")
elsif (direction.eql? "down")
month_str = Date::MONTHNAMES[component-1]
touch("pickerView scrollView index:#{column} label text:'#{month_str}'")
end
else
# => Day and year scrolls are numeric
if (direction.eql? "up")
touch("pickerView scrollView index:#{column} label text:'#{component + 1}'")
elsif (direction.eql? "down")
touch("pickerView scrollView index:#{column} label text:'#{component - 1}'")
end
end
sleep(0.3)
end
def datequery()
return query("datePicker","date").first
end
def should_see_date_picker ()
if query("datePicker", :date).empty?
screenshot_and_raise "Could not find date picker"
end
end
def is_picker_in_date_mode()
res = query("datePicker", :datePickerMode)
screenshot_and_raise "expected to see a date picker" if res.empty?
screenshot_and_raise "expected to see UIDatePickerModeDate" if res.first!=1
end
def picker_maximum_date_time()
res = query("datePicker", :maximumDate)
screenshot_and_raise "expected to see a date picker" if res.empty?
return DateTime.parse(res.first) if (res.first)
end
That gist mentioned by Chathura is very out of date.
Calabash iOS now supports interaction with most date pickers natively.
https://github.com/calabash/calabash-ios/blob/develop/calabash-cucumber/features/step_definitions/calabash_steps.rb#L406
Scenario: I should be able to use the predefined steps to change the picker time
Then I change the date picker time to "10:45"
Scenario: I should be able to use the predefined steps to change the picker date
Then I change the date picker date to "July 28 2009"
Scenario: I should be able to use the predefined steps to change the picker date and time
Then I change the date picker date to "July 28" at "15:23"

crystal reports conditional formatting for summary fields

I'm trying to create a decimal formatting formula on my summary fields. The values in the database could have 0, 1, or 2 decimal places. I've started with this:
If (CurrentFieldValue mod 1 = 0) Then
0
Else If (CurrentFieldValue mod .1 = 0) Then
1
Else
2
On a simple single data field, this works and displays the value with 0, 1, or 2 decimal places based on the data coming from my database. The same formula doesn't work for a summary field on my reports with group data. Any ideas?
Edit: Since I don't know how to format code in a comment, I'll address the suggestion of using a formula here:
Didn't work. Formula:
Sum ({myTable.dataValue}, {myTable.groupField})
then I used:
If ({#formula} mod 1 = 0) Then
0
Else If ({#formula} mod .1 = 0) Then
1
Else
2
And I still got whole numbers for everything. My rounding is set to .01 with no formula. Do I need a formula for rounding too? I still don't understand why this works on individual values but not for group summaries.
OK- it turns out this is due to our lack of understanding of the mod function :)
Everything mod 1 actually returns 0. This is the formula you need to use:
if {ER100_ACCT_ORDER.ER100_ORD_TOT} * 100 mod 100 = 0 then
0
else if {ER100_ACCT_ORDER.ER100_ORD_TOT} * 100 mod 10 = 0 then
1
else
2
:)
How about just creating a formula field instead of using the built-in summary field:
sum({mytable.myfield})
Then you can use your conditional formatting:
If ({#formula} mod 1 = 0) Then
0
Else If ({#formula} mod .1 = 0) Then
1
Else
2