I need to do a aggregation under IF statement in a calculated field
If a city is the same as selected by the user (parameter PAR_SELECT_CITY);
In case the condition 1 is true, then SUM(Number of records) - [PAR_SELECT_QTY]
[PAR_SELECT_QTY] is a parameter that user choose to deduct from the total quantity
In case the condition 1 is false, then SUM(Number of records)
IF [City] = [PAR_SELECT_CITY] THEN
SUM([Number of Records])-[PAR_SELECT_QTY]
ELSE
SUM([Number of Records])
END
However, IF Statament does not accept to mix aggregation and not aggregation
How do I solve this issue?
As your error suggests, the issue is the mix of aggregate and "row level" data. In Tableau, you ideally want your row level data to be contained within an aggregate function.
i.e. sum(if true then 1 end) instead of if true then sum(1) end
For your example, you could try
SUM([Number of Records])
-
AVG(IF [City] = [PAR_SELECT_CITY] THEN [PAR_SELECT_QTY] ELSE 0 END)
Your PAR_SELECT_CITY also needs to return an aggregate number. If PARA_SELECT_CITY = 5 (for example) and your dataset contains 100 rows, the AVG(PARA_SELECT_CITY) will also be 5, whereas SUM(PARA_SELECT_CITY) would return 500. Therefore the AVG should work as an aggregate function that returns the desired value.
Related
I have a rather long calculated column which uses the drop-downs of two other columns to return a 'score' based on their values.
A section of it looks like this:
IF(AND([Current Impact]="High",[Current Probability]="Low"),17)
This would return the value 17 if the impact is high and the probability is low in those other columns.
This seems to return the 17 as a string rather than a number because sorting on my calculated column (score) from high to low produces a result sorted on the first digit e.g. 5, 4, 32, 2, 18
I have a workaround to this - I enclose the whole formula in a (an?) =value() function.
However, I'm curious as to why the IF function returns the string data types instead of a number?
As far as I know, in the column settings you could specify what data type returned from the calculated formula:
Thanks in advance for helping with this.
I have an issue where this I need the following formula to calculate this- i would advise seeing the picture in the link to fully understand the question (as I'm probably not going to explain myself!);
Calculating Risk Score
Condition 1: If Blood Result is less <=9 or Score <=6 or Tumour Size = <5mm show as text value Low
Condition 2 : If Blood Result 10-20 or Score = 7 or Tumour Size 5mm-9mm show as text value Medium
Condition 3: If Blood Result >=20 or Score = 8 or Tumour Size >=10mm show as text value High
The issue I am having is that any person can have a value from any of the condition, but I need to display the overriding value.
Example:
Blood Result = 5 (condition 1)
Score = 7 (condition 2)
Tumour Size = 10mm (condition 3)
SHOW VALUE: HIGH.
The problemn i'm having is that when I'm doing IF statements, as condition 1 of blood result is True, its always displaying Low without looking at the other values which could overwrite it.
I have only tried nested IF statements with AND OR in them, but no luck.
See examples of values
you can do one thing for that type condition
store your message in one variable, for example, var = msg
if condition 1 true assign msg = "Low"
if condition 2 true assign msg = "Medium"
if condition 3 true assign msg = "High"
after checking all conditions print your message.
I hope this will help you
I have one sheet with data on my facebook ads. I have another sheet with data on the products in my store. I'm having trouble with some countifs where I'm counting how many times my product ID exists in a row where multiple numbers are. They are formatted like this: /2032/2034/2040/1/
It's easy on the rows where only one product ID exists but some rows have multiple ID's separated by a /. And I need to see if the ID exists as a exact match alone or somewhere between the /'s.
Rows with facebook ads data:
A1: /2032/2034/2040/1/
A2: /1548/84/2154/2001/
A3: /2032/1689/1840/2548/
Row with product data:
B1: 2034
C1: I need a countifs here that checks how many times B1 exists in column A. Lets say I have thousands of rows with different variations of A1 where B1 could standalone. How do I count this? I always need exact matches.
You can compare the number you want (56) with the REGEX #MonkeyZeus commented whith a little change -> "(?:^|/)"&B1&"(?:/|$)" so the end result is:
=IF(REGEXMATCH(A1, "(?:^|/)"&B1&"(?:/|$)"), true, false)
Example:
UPDATE
If you need to count the total of 56 in X rows you can change the "True / False" of the condition for "1 / 0" and then do a =SUM(C1:C5) on the last row:
=IF(REGEXMATCH(A1, "(?:^|/)"&B1&"(?:/|$)"), 1, 0)
UPDATE 2
Thanks for contributing. Unfortunately I'm not able to do it this way
since I have loads of data to do this on. Is there a way to do it with
a countif in a single cell without adding a extra step with "sum"?
In that case you can do:
=COUNTA(FILTER(A:A, REGEXMATCH(A:A, "(?:^|/)"&B2&"(?:/|$)")))
Example:
UPDATE 3
With the following condition you check every single possibility just by adding another COUNTIF:
=COUNTIF(A:A,B1) + COUNTIF(A:A, "*/"&B1) + COUNTIF(A:A, B1&"/*") + COUNTIF(A:A, "*/"&B1&"/*")
Hope this helps!
try:
=COUNTIF(SPLIT(A1, "/"), B1)
UPDATE:
=ARRAYFORMULA(IF(A2<>"", {
SUM(IF((REGEXMATCH(""&DATA!C:C, ""&A2))*(DATA!B:B="carousel"), 1, )),
SUM(IF((REGEXMATCH(""&DATA!C:C, ""&A2))*(DATA!B:B="imagepost"), 1, ))}, ))
I want to create a set in tableau, which will show either one of these two values: Y or N
2 already existing columns are here important, "VAT-ID" and "CUSTOMER-ID". the new column should check if a customer-ID has multiple VAT-IDs. If yes, the value "Y" should be displayed, else "N".
The table looks like:
customer-id VAT-id in-both
123456 EE999999999 Y
654321 AA999999999 N
666666 GG999999999 N
123456 KK999999999 Y
654321 AA999999999 N
any Help would be appreciated, I have tried IF [CustomerID] = 1 AND Count([VAT-ID]) > 1 THEN 'Y' ELSE 'N' END which didn't work.
You are close. For this you need an LOD (Level of Detail) expression. LOD expressions allow you to do calculations at a different granularity then the view is rendered.
You can use:
if
{fixed [Customer-Id]: countd([VAT-id]) } > 1
then 'Y'
else 'N'
end
The LOD is the {fixed...}. The way you read this is you want to count the distinct number of VAT-id per each Customer-id. (eg 123456 will return 2; all others will return 1). Then you just wrap that in an If statement.
In a cloudsearch structured query.
I have a couple of fields I am searching on.
On field one, the user selects "2"
On field two the user selects "1"
I am wanting to run this as a range query, so that the results that are returned are -1 to +1
eg. on field one the range would be 1,3 and on field 2 it would be 0,2
What I am wanting to do is sort the results so that the results that match both field 1 and field 2 are at the top, and the rest under it.
eg. where field one=2 and field two =1 would be at the top and the rest are not in any specific order,
note: I do need to end up sorting the results by distance, so that all the exact matching results are in distance order, then all the rest are ordered by distance.
I am sure I can do this with 2 queries, just trying to make it work in one query if at all possible to lighten the load.
Say your fields are 'a' and 'b', and the specified values are a=2 and b=1 (as in your example, except I've named the fields 'a' and 'b' instead of 'one' and 'two'). Here are the various terms of your query.
Range Query
This is the query for the range a±1 and b±1 where a=2 and b=1:
q=(and (range field=a[1,3]) (range field=b[0,2]))
Rank Expression
For your rank expression, compute a distance-based score using absolute value so that scores 'a' and 'b' can't cancel each other out (like a=3,b=0 would, for example):
expr.rank1=abs(a-2)+abs(b-1)
Sort by Rank
That defined a ranking expression named rank1, which we now want to sort by, starting with the lowest values ('0' means a=2,b=1):
sort=rank1 asc
Return the Rank
For debugging purposes, you may want return the ranking score:
return=rank1
Put all those terms together and you've got your query.
Further Potentially-Useful Things
If you want to get fancy and penalize things in a non-linear way, you can use exp. For example, if you want to differentiate between 'a' and 'b' both being off by 1 vs 'a' being an exact match and 'b' being off by 2 (eg a=3,b=2 will rank ahead of a=2,b=3 even though the previous ranker would give them both a score of 2):
expr.rank1=exp(abs(a-2))+exp(abs(b-1))
And you can use boolean logic and the ternary operator to detect and prefer certain results that meet certain criteria, eg to give a big boost when 'a' and 'b' are on-target, a smaller boost when 'a' or 'b' is on target, etc (since we're sorting in low-to-high, a boost in rank is actually achieved by adding less to the result):
((a==1&&b==2)?0:100)+((a==1||b==2)?0:1000)+abs(a-1)+abs(b-2)
See http://docs.aws.amazon.com/cloudsearch/latest/developerguide/configuring-expressions.html