get second highest value from a dimension - tableau-api

click to view the image of the dataset
here as we can see
there is a state , there is a election constituency , and then there are multiple candidates
who contested a election for the said constituency ....
now i want is to get the second highest vote acquirer in tableau ...
may be using calculated field or any other way ...
can you help please ?
i tried rank_dense() function but i am struggling to plot the second highest value ,
i am missing something ....

Step 1: Create a Parameter: "Nth Rank" with type as Integer and values as 1,2,3,4 etc
Step 2: Create a Calculated field: "Rank number" and put this formula
RANK(SUM([Votes Secured],'desc') = [Nth Rank]
Step 3: Show the Parameter as a dropdown
Step 4 : Drag the field "Rank number" in the filters pane and set to TRUE
Now, whatever Rank number you select from the parameter will display in the dashboard

Related

Tableau KPI prev value depending on variable

am trying to get Previous Sum(of someField) based on a variable value which is an Id.
This is not a table, Im doing a KPI
On Qlik you would do something like:
SUM({<Id={"$(=Max(vVariable),-1))"}>} someField)
But I can not achieve it on Tableau, off course is due to my lack of knowledge, unfortunatelly time is tinking at work and wanted to see if anyone has any input!
Thanks
Assuming you may use a sample input like the Superstore (using sales as metric), this could be what you're looking for:
In red you can see your "variable" which allows you to select a value and in blue you'll find the unique row for the previous value (Order ID sorted).
The first thing you need to to do is creating a parameter based on all the Order ID values:
Then things start to get a bit complicated if you're not familiar with LOD (Level of details) and the order of execution in Tableau, especially for filters.
Assuming that you can get some information on your own (otherwise, feel free to ask), the first thing you nee to to do is to "pre-calculate" the equivalent of a table having a rowe for each Order ID, in which you also have the previous Order ID value.
You can achive this combining Fixed (LOD) and Lookup function, creating this Calculated Field "Lookup Order ID":
LOOKUP( max({ FIXED [Order ID] : MAX([Order ID])}),1)
This is actually just a calculated field that you want to "fix" because you need the filter to act after you have made that previous calculus, and then you shift your data by 1 row backward.
Once you've done that, you just nee to create another calculated field in order to test your parametric value, and it could be something like this "check param":
[Lookup Order ID] = [Order ID param]
Moving this calculated field in the filter section and selecting just "true" values, you'll get that unique rows like in the initial image, showing the previous value (blue) related to the one you select in the parameter drop-down menu (red).

Count of group dimensions item in Tableau

I'm fairly new to tableau and I'm having the following issue. Below is a sample of the data I'm using.
Customer No | Item
___________________
1 A
1 B
2 A
3 A
4 A
4 B
5 B
6 A
I'm trying to get a count of how many customers bought Item A and B. So far I tried doing a separate group by combining A and B but I get the total result of 8. I also tried doing a calculation and I'm getting the same result of 8. Can someone please point me to the right direction on how to get this result. Thanks!
This is the result I'm trying to get:
Item| Count
A 5
B 3
A and B 2
I recreated your exact dataset and pasted it into Tableau so you could see a couple of examples.
Here's how you can see the number of customers who purchased an individual item, plus the number of customers who purchased both items.
Your calculation will be:
IF { FIXED [Customer No]: COUNTD([Item]) } = 1 THEN
[Item]
ELSE
'Both A and B'
END
And you'll need to set your view up to look like this:
Below are ways you can see when both items were purchased.
Boolean OR
The calculation you'll want to use is:
ATTR([ITEM]) = 'A' OR ATTR([ITEM]) = 'B'
And you'll want to set up your view to look like this:
A, B or Both
If you would like a bit more specificity in your result, you might try:
IF ATTR([Item]) = 'A' THEN
'A'
ELSEIF ATTR([Item]) = 'B' THEN
'B'
ELSE
'BOTH'
END
Replacing the previous calculation with the new looks like this:
More than 1 item
If the specific items purchased don't matter, you could use this logic.
COUNTD([Item]) > 1
Replacing the previous calculation with this one would look like:
More than 1 Item using a window function (probably overkill)
The calculation you'll need to use is:
WINDOW_COUNT(COUNTD([Item]))
Because this is a Window function, we'll need to specify how it's calculated across our dimensions. To do this click the down arrow on the right-hand side of the pill and select Edit Table Calculation...
You'll then need to set these settings:
I'll add the calculation we created in the first example ([A and B]) to the filter shelf and select True. That should give you something that looks like:
More than 1 item using a Level of Detail expression
The calculation for this example is:
{ EXCLUDE [Item]: COUNTD([Item]) }
You'll view should look like:
As you can see Tableau is quite flexible. Hope these examples were helpful!
You might want to use Tableau’s set feature to approach problems like this.
For example, right click on the field [Customer No] in the data pane (i.e. left sidebar) and choose the “Create Set” command. Click “Use All” at the top of the set panel and then click the Condition tab. Define the set using the condition MAX([Item] = “A”). Name the set “Customers who bought A”.
Similarly, create a set of customers who bought item B. You can then select both sets in the data pane, and create a combined set to be the intersection, that is, customers who bought both an item A and an item B.
You can think of a set as either a mathematical set of the members of a field that belong to the set (i.e. a set of customer ids) or as Boolean function defined for each data record in the data source indicating whether that data record is associated with the set (i.e. a Boolean function that operates on transactions to say whether the associated customer ID is in the set. A key to keep in mind for the condition formulas used here is that the condition is an aggregate formula, operating on a block of data records for a customer ID to determine whether the customer ID is in the set.
Once you have defined your sets of interest, you can use them in many ways - in calculated fields, as filters, as dimensions on shelves in a visualization, in set actions, to combine with other sets ...
To define a measure that counts the customers in a set, create a calculated field such as “[Num A Customers]” as COUNTD(if [Customers who bought A] then [Customer ID] end) Do the same for whatever other sets you are interested in. Then you can use those measures (probably with Measure Names and Measure Values) to make your viz.

Dynamically display the measures based on filter value in tableau

I had a requirement where i have to display measures based on a filter created on Dimension value.
For eg
Dimension Filter Value Dropdown --> Value1, Value2, Value3
Measures --> Measure1, Measure11, Measure2 Measure21, Measure22, Measure3
When I select
Case 1 :
Filter -> Value1
Measure To display -> Measure1, Measure11
Case2 :
Filter -> Value2
Measure To display -> Measure2, Measure21, Measure22
Case3:
Filter -> Value3
Measure To display -> Measure3
I am new to tableau development and last few days i started using this . I googled few and found one resource in the below link
Choose Measures dynamically
But in the above link they shown based on filter value they display only one measures not multiple measures.
I need to display multiple measures based on filter value.
As Explained i followed the process but don't know how to include multiple measures in the below code in Calculate field
CASE [Filter Parameter]
WHEN “Value1” THEN Measure1, Measure11
WHEN “Value2” THEN Measure2, Measure21, Measure22
WHEN “Value3” THEN Measure3
END
Hope it is explained well, please let me know .
Thanks
You'll need to both transpose your dataset and create a flag to be used as the filter value.
To transpose your data:
Edit your data source
Click the first column you want to transpose and then shift + click the last
Click Pivot
Now navigate to your sheet to work with the data
I'll create a field to just pull out the number for the 'MeasureN' string so it's easier to create the filter, but this isn't necessary.
Now I'll write the calculation to be used as a filter that mirrors the logic you shared.
Arranging the pills on the screen like this...
Hopefully provides you with the functionality you were looking for. Happy vizzing!

Calculated field to select column based in countd in tableau

I am trying to create a calculated field like the below.
but it gives me an error on aggregate and non aggregate values.
Can someone help?
if (COUNTD([Category])=1 then [Sub-Category]
else [Category] END
Note: I am using the super store data. So when a user selects a filter
in the quick filter for category, I was hoping that the countd becomes
one and shows sub category as rows in the table instead of category.
You could use a sheet selector approach to get this effect. The idea is to create a calculated field that outputs a column to be used a the filter to select the sheet.
First create a calculated field as follows:
{ FIXED : if COUNTD([Category]) = 1 then 'Sub Category' elseif
countd([Category]) > 1 then 'Category' END}
Now create two worksheets one broken down by category and the other
my sub category.
Add your Category filter to both worksheets and set to a context
filter.
Drag your calculated field column onto the filter pane as well. For the Category worksheet select the filter to Category and vice versa for the sub category worksheet. Hint you can manually add the value or edit your category filter to show the relevant value for each worksheet
Drag both worksheets into a dashboard and format so the worksheets behave appropriately https://community.tableau.com/thread/132957
Tada! A dashboard where the axis will change depending on what you have selected.

how to do variable + 1 in iReport

i want to do line no in different band area. example, in group band i want it to appear
1.
2.
and the next band area, such as detail band, i want it continue
3.
4.
i dont it reset. how???
Create a group_COUNT variable. Make it an Integer, give it Calculation type Count, Reset type Report, Increment type Group and select the Increment Group. The Variable Expression should be $V{group_COUNT}++. Drop it to your Group zone and it should increment each time the group changes.
If you have created a group and you are using the detail band (means in detail band you have to print the valueis as you said ) then use the report generated variable like (your groupname_count) which gives the valuu like 123 and next band 3,4 as query found the records in the detail band.
or
if you have created the group and you have to print the nor as 1, 2 like this and for next group records 3,4
then create one variable class name is maths big decimal, calculation count, reset type your group nameincrement type none,variable expersion(in which the field which have alws value display) place there initial value expression is 0.