Say I have city populations for each state / city in the united states. I want to mark each city with over 2 million population as a big city. then if a state contains a "big city", i want to mark that state as a "big state". I made a calculated field:
if Pop>2000000 then "big city" else "small city" end
This works fine.
Now when I try to make a calculated field for state I tried this:
if contains([big city],"big") then "big state" else "small state" end
This almost works, but I get multiple values for each state when I only want 1 value, either big state or small state. How do I stop tableau from creating multiple values?
You can try that with an approach involving two calculated fields.
Assuming that your big_city, small_city calculation is a calculated field named City_Size
Now, the First Calculated field will assign a 1 or 0 to each row, depeding upon the value of City_Size. Name it as is_big_state
if City_Size = 'big' then
1
else
0
end
Now use this calculated field into another calculation, termed as State_type
IIF(Max(City_Size) = 1, "Big State", "Small State")
Edit : You may combine both of them into one :
IIF(Max(iif(City_Size = 'big', 1, 0)) = 1, "Big State", "Small State")
Have state set to "small state" by default.
When you check for city population, set the state to "big state" if you find a "big city".
if Pop > 2000000
then "big city"
"big state"
else
"small city"
Related
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.
Source data:
Market Platform Web sales $ Mobile sales $ Insured
FR iPhone 1323 8709 Y
IT iPad 12434 7657 N
FR android 234 2352355 N
IT android 12323 23434 Y
Is there a way to evaluate the sales of devices that are insured?
if List.Contains({"iPhone","iPad","iPod"},[Platform]) and ([Insured]="Y") then [Mobile sales] else "error"
Something to that extent, just not sure how to approach it
A direct answer to your question is
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
SumUpSales = Table.AddColumn(Source, "Sales of insured devices", each if List.Contains({"iPhone","iPad","iPod"}, _[Platform]) and Text.Upper(_[Insured]) = "Y" then _[#"Mobile sales $"] else null, type number)
in
SumUpSales
However, I would like to stress you few things.
First, it's better to convert values in [Insured] column to boolean first. That way you can catch errors before they corrupt your data without you noticing. My example doesn't do that, all it does is negating letter case in [Insured], since PowerM is case-sensitive language.
Second, you'd better use null rather than text value error. Then, you can set column type, and do some math with its values, such as summing them up. In case of mixed text and number values you will get an error in this and many other cases.
And last.
It is probably better way to use a pivot table for visualizing data like this. You just need to add a column which groups all Apple (and/or other) devices together based on the same logic, but excluding [Insured]. Pivot tables are more flexible, and I personally like them very much.
I have made a basic formula which calculates the number of staff required to process sales in any given hour on a typical working day.
Now I am trying to create a formula in Google sheets which checks the number of staff required to process the sales VS the actual number of staff who have been rostered on.
I have one row with the predicted hourly sales, one row with the number of staff required to process the sales, and one row with the actual number of staff rostered for every given hour.
I need to create a formula which checks the number of staff required vs the number of staff actually rostered:
IF there is insufficient staff, it will return "Insufficient".
IF the number of staff rostered is sufficient (within 10% less of, or 25% more of the predicted sales), return "Sufficient".
IF there is more than 25% more staff than necessary, return "Overstaffed".
I've searched this, but I am unable to find a suitable example / wrap my head around them and customize them to suit my needs.
I've got reasonable experience programming with PHP, Python, Java, and JavaScript, however, in this instance, I am strictly limited (by my employer) to creating a roster in Google sheets.
I have tried the following:
=IF(F13 = "", "", IF(F15 = "", "", IF(F15 < F13, "Understaffed", IF(F15 >= F13, "Enough Staff", ELSEIF(F15 > (F13*1.3), "Too many staff")))))
And I have also tried:
=SWITCH(G15, G15 < G13, "Under", G15 >= G13, "Fine", G15 > G13*1.3, "Over")
But they do not work correctly.
Can someone assist me in this? TIA
=SWITCH(G15, G15 < G13, "Under", G15 >= G13, "Fine", G15 > G13*1.3, "Over")
In your second SWITCH formula, the second condition is true for both the second and third condition and so the third condition is never reached. Flip the second and third condition placements.
=if(count(F13, F15)=2, IFS(F15<F13, "under", F15>F13*1.3, "over", F15>=F13, "enough"), "")
I have the following set of data :
(Name=[Jane Doe]>[Jane Doe]),
(Job=[Temporary Employee]>[Full Time]),
(Address=[1 place]>[2 St.]),
(Title=[Account Manager]>[Account Manager])
I am trying to find out which name-value pairs have been modified. For example,
While Name value stayed the same "Jane Doe" to "Jane Doe", Job's value was changed from "temporary employee" to "full time", Address' value was changed from "1 place" to "2 St.". Finally, title value remained the same.
All the comparisons will be done for values in [..] > [..]. I will also need what was changed, name, job, title and address.
Any help will be appreciated. Thanks
EDIT : Not sure why this was down voted. It is still a regex question where one needs to extract name, from-value and to-value pairs. Comparison will be done afterwards.
You can only match the keys and values - regex will not compare them for you:
\(([^=]+)=\[([^]]+)\]>\[([^]]+)\]\)
demo
I have a field with that contains a mix of descriptions and dollar amounts. With TSQL, I would like to extract those dollar amounts, then insert them into a new field for the record.
-- UPDATE --
Some data samples could be:
Used knife set for sale $200.00 or best offer.
$4,500 Persian rug for sale.
Today only, $100 rebate.
Five items for sale: $20 Motorola phone car charger, $150 PS2, $50.00 3 foot high shelf.
In the set above I was thinking of just grabbing the first occurrence of the dollar figure... that is the simplest.
I'm not trying to remove the amounts from the original text, just get their value, and add them to a new field.
The amounts could/could not contain decimals, and commas.
I'm sure PATINDEX won't cut it and I don't need an extremely RegEx function to accomplish this.
However, looking at The OLE Regex Find (Execute) function here, appears to be the most robust, however when trying to use the function I get the following error message in SSMS:
SQL Server blocked access to procedure 'sys.sp_OACreate' of component
'Ole Automation Procedures' because this component is turned off as
part of the security configuration for this server. A system
administrator can enable the use of 'Ole Automation Procedures' by
using sp_configure. For more information about enabling 'Ole
Automation Procedures', see "Surface Area Configuration" in SQL Server
Books Online.
I don't want to go and changing my server settings just for this function. I have another regex function that works just fine without changes.
I can't imagine this being that complicated to just extract dollar amounts. Any simpler ways?
Thanks.
CREATE FUNCTION dbo.fnGetAmounts(#str nvarchar(max))
RETURNS TABLE
AS
RETURN
(
-- generate all possible starting positions ( 1 to len(#str))
WITH StartingPositions AS
(
SELECT 1 AS Position
UNION ALL
SELECT Position+1
FROM StartingPositions
WHERE Position <= LEN(#str)
)
-- generate possible lengths
, Lengths AS
(
SELECT 1 AS [Length]
UNION ALL
SELECT [Length]+1
FROM Lengths
WHERE [Length] <= 15
)
-- a Cartesian product between StartingPositions and Lengths
-- if the substring is numeric then get it
,PossibleCombinations AS
(
SELECT CASE
WHEN ISNUMERIC(substring(#str,sp.Position,l.Length)) = 1
THEN substring(#str,sp.Position,l.Length)
ELSE null END as Number
,sp.Position
,l.Length
FROM StartingPositions sp, Lengths l
WHERE sp.Position <= LEN(#str)
)
-- get only the numbers that start with Dollar Sign,
-- group by starting position and take the maximum value
-- (ie, from $, $2, $20, $200 etc)
SELECT MAX(convert(money, Number)) as Amount
FROM PossibleCombinations
WHERE Number like '$%'
GROUP BY Position
)
GO
declare #str nvarchar(max) = 'Used knife set for sale $200.00 or best offer.
$4,500 Persian rug for sale.
Today only, $100 rebate.
Five items for sale: $20 Motorola phone car charger, $150 PS2, $50.00 3 foot high shelf.'
SELECT *
FROM dbo.fnGetAmounts(#str)
OPTION(MAXRECURSION 32767) -- max recursion option is required in the select that uses this function
This link should help.
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/extracting-numbers-with-sql-server
Assuming you are OK with extracting the numeric's, regardless of wether or not there is a $ sign. If that is a strict requirement, some mods will be needed.