Dynamic color format values - Tableau - tableau-api

I want to color format the values (.csv file format) based on the below conditions in tableau desktop.
If consecutive increase of the values or same values with increase values from start Date then values should be Red color .
If Consecutive decrease of the values from startDate then values color should be Green Color
If the values are Increase and then decrease from start date then Yellow.
If the values are decrease then increase then yellow.
For the above data the expected color for the student values should be below..
Student:Mon,Pat,Henry,Kim Yellow,
Jack,stanley Red
Kevin,Lendl -Green
I have tried below code but not working as expected..
Please correct if i missing any thing thing.
//Green - consecutive downward trend
if
window_sum(if FIRST() = 0 then 0
elseif sum([Value]) < lookup(sum([Value]),-1) then 1
end) = MAX([Number of Days]) then "Green"
//Yellow - downward and upward trend but not consecutive
elseif window_sum(if FIRST() = 0 then 0
elseif sum([Value]) < lookup(sum([Value]),-1) then 1
end) > 0 and
window_sum(if FIRST() = 0 then 0
elseif sum([Value]) < lookup(sum([Value]),-1) then 1
end) < MAX([Number of Days]) then "Yellow"
//Red - consecutive upward trend
elseif
window_sum(if FIRST() = 0 then 0
elseif sum([Value]) > lookup(sum([Value]),-1) then 1
end) = MAX([Number of Days]) then "Red"
end
Thanks for your help in advance !!

Without knowing your data structure you can look into table calculations. This may not be straightforward, especially to get the "Compute Using" settings right, but some logic as follows may work.
Calculate the day on day differences (a table calculation will do that).
If the day on day diff is positive then give it a value 1, is negative set it to -1. This would be an IF statement around your table calculation.
Next SUM the result.
Calculate how many dates there are, which will provide the maximum possible number if all are increasing or decreasing every day.
Finally compare the result of the summing if up or down with the maximum possible. This field can sit on the colour shelf.
I haven't tested any of this but, from a logic perspective, you may be able to make this work.
Edit based on comments:
Build the following calculated fields, it should work. You'll be able to troubleshoot calc by calc if having "compute using" challenges. Put the [Colour] field on the colour shelf:
[DailyDiff]: ZN(SUM([Value])) - LOOKUP(ZN(SUM([Value])), -1)
[isUpOrDown]: IF [DailyDiff] >0 THEN 1 ELSEIF [DailyDiff] <0 THEN -1 ELSE 0 END
[TotalUpOrDown]: WINDOW_SUM([isUpOrDown])
[NumDays]: {COUNTD([Date])}-1 //1 less to account for the first day
[Colour]: IF [TotalUpOrDown] = MIN([NumDays]) THEN "Increasing" ELSEIF [TotalUpOrDown]=-MIN([NumDays]) THEN "Decreasing" ELSE "Other" END

Related

Semi Circle in Tableau Start at 270 and not 0

I have many calculated fields that come up with a percentage based on the number of days payment was made. I am using those calculated fields in place of the parameter [Percentage]. There is no number identifier to associate as instructed on the following website. I have the calculations done, the case statement and the color field. There is no Number Identifier, so I have no [SLICE#]
https://www.flerlagetwins.com/2018/01/percentage-gauges-in-tableau_61.html
I have a 96.02% and it always starts at 0 or 12 o'clock. I need it to start at 270. SAnd "Hidden" or "None" does not work in the Color Field.
I ended up duplicating my calculations and for each, I did for [Payment % 1 of 2]
IF([Payments] / [Total Default]) > .5 Then
( Payments] / [Total Default]) - .5
ELSE [Payments] / [Total Default] END
and for [Payment % 2 of 2]
IF( Payments] / [Total Default]) > .5 Then
([Payments] / [Total Default]-[Payment % 1 0f 2])
ELSE [Payment % 1 0f 2] END
And then just reorder them and make them the same color.
While having a calculation of 1 at the bottom

Conditional formatting in Tableau connected to Cube data

Could someone help me to color these measures on 3 variations - less than 0, equal to 0 and greater than 0. I tried with a calculation
IF [Margin 1] > 0 THEN 1 ELSEIF [Margin 1] < 0 THEN 2 ELSEIF [Margin 1] = 0 THEN 3 END
The reason I have assigned numbers instead of string in the above calculation is, the data is from a cube which will not allow string to be mixed with IF conditon
This is only for Margin 1 while a similar one is also required for Margin 2 and Margin 3 as well. Once I put this calculation to color shelf it colored first value of Margin 1 correctly but at the same time even colors Margin 2 and Margin 3 as well with same shade.
Could someone help to nest color logic calculation for all three Margins so I can have control to choose less than, equal to and greater than 0 in different shades.
Attached is the image of data and tableau sheet of what I have arrived as of now, do let me know for any other details
Not sure how the fact the data is from cube makes much difference, inside of tableau you're looking at a integer and returning a string shouldn't matter.
If you want give this a try
create a calculated field like this:
ZN([Margin 1]) + ZN([Margin 2]) + ZN([Margin 3])
Then create your if statement based on the new calculated field returning string
If [NewCalculated field] <0 then
"Red"
If [NewCalculated field] 0 then
"Amber"
Else
"Green"
End
Have a go

Tableau Formatting Numbers in calculated field

I have a calculate field named Table Measure, and it contains Percent and Numbers. How can I format the % with 1 decimal place and the number of records with comma and zero decimal place? I tried to use FORMAT([% of Composite VOC]),"#.0%") it gave me an error.
IF [Toggle] = 1 THEN ([% of Sale 1]) ELSEIF [Toggle] = 2 THEN [Total of
Profit] ELSEIF [Toggle] = 3 THEN [% of Discount] ELSEIF [Toggle] = 4 THEN % of
Commission ] ELSEIF [Toggle] = 5 THEN [Number of Records] END) END
Right click on the calculation "Table Measure" -> Select "Default Properties" -> "Number Format" -> "Percentage"...update to 1 decimal.

Random number generation - code not working as it should

So I have to generate a random number (called 'p' here) between 0 and 90 whose frequency distribution is a cosine function (i.e I should have more numbers between 0 and 45 than numbers between 45 and 90).
I am working on matlab
The code is as follows -
flag = 1;
while flag == 1
candidate = randi([0,90]);
if rand < cosd( candidate )
p = candidate;
flag = 2;
end
end
I am generating 20 such numbers but always I get most of the numbers towards the higher end (45-90).
From those 20 numbers, there is hardly 1-2 numbers < 45.
Is my code wrong?
EDIT: Okay, so I got the answer. I tried running the code separately as follows-
for i = 1:20
flag = 1;
while flag == 1
candidate = randi([0,90]);
if rand < cosd( candidate )
p = candidate;
flag = 2;
disp(p);
end
end
end
And I'm getting most of the values of p between 0 and 45. My original code had an external 'if' condition which was the reason for only accepting higher values of 'p'. I used a while loop and the number of iterations were much more than 20 to get 20 values of 'p'.
Here is my original code snippet -
while zz <=20
d = randi([0,359]);
flag = 1;
while flag == 1
c = randi([0,90]);
x = rand(1);
if x < cosd(c)
p = c;
flag = 2;
end
end
if 'external condition'
strike(zz) = d;
dip(zz) = p;
slip(zz) = round(i);
zz= zz+1;
end
end
If you just want to get answer, read the last line. But if you want to know that why that answer is right, read the explanation.
Assume that you have a distinct distribution function like this:
f(0)=1;
f(1.5)=10;
f(4)=9;
So the cumulative function is:
F(0)=1;
F(1.5)=11;
F(4)=20;
No we want to have a relative cumulative function, as F(4)=20 (4 is the last item), we divide cumulative function by 20. So it would be:
F'(0)=0.05
F'(1.5)=0.55
F'(4)=1.00
Now, we generate a random number between 0 and 1. Every time we generate a random number, we generate a value for F'(x) and if F'(x) does not have that value anywhere, we use nearest bigger number (like y) which for some x, F(x)=y. For my example, based on relative cumulative function:
If the random number was less than 0.05, our distribution-based random number is 1.5
If the random number was between 0.05 and 0.55, our distribution-based random number is 2,
If was more than 0.55, our distribution-based random number is 4
We should do a similar work with continuous distribution functions. The difference is that in continuous world, we use integral instead of cumulative function. So for your question, we have:
f(x)=cos(x) , 0<=x<=90
F(x)=sin(x)-sin(0)=sin(x) , 0<=x<=90
F'(x)=cos(x) , 0<=x<=90 (Because F(90)=1)
Now we generate a random number between 0 and 1 (like r). So we have:
F'(x)=r => sin(x)=r => x=arcsin(r)
Actually, you just need to generate a random number between 0 and 1 and calculate the arcsin of that.

How to create a percent on a column graph chart?

I have a column graph chart in SSRS 2008 R2 and now I want to display the values as a percent. Currently it just displays the sum totals. My dataset looks like:
people_count facility met_reqs
12 Chattanooga, TN 0
9 Clarksville, TN 0
6 Columbia, TN 0
51 Chattanooga, TN 1
22 Clarksville, TN 1
28 Columbia, TN 1
As you can see, each city has two rows: the first row is the count of persons who did not meet requirements and the second row is for each city which did meet reqs.
Where my last query is:
select
count(distinct people_id) as people_count,
facility,
case when total_los/total_visits *3/7 >= 1 then 1 else 0 end met_reqs
from #final
group by facility, case when total_los/total_visits *3/7 >= 1 then 1 else 0 end
Currently I have this chart to display the sum of people_count for Chart Values, Category Group = facility, and Series Group = met_reqs.
This looks like:
But now for the Y-axis I want this to instead display percentage of persons/facility who met the requirements. How can I do this? So the Y-axis should instead have a range of 0-100%.
Have you considered a 100 % Stacked Column chart?
If you change your existing setup this seems to achieve your requirements:
If this isn't what you're after, can you please update your questions with some more details?
Edit after comment
OK, to meet the specific requirement of displaying % Meeting Requirements only. With the same Dataset, use a Chart type of Column (i.e. the first option) and remove the Series Group:
Change the Values expression to:
=Sum(IIf(Fields!met_reqs.Value = 1, Fields!people_count.Value, Nothing))
/ Sum(Fields!people_count.Value)
i.e. getting the % of total people_count where met_reqs = 1 compared to the total people_count in each group.
For the sake of completeness I changed the number formatting on the Y axis to be percentage.
This looks OK to me:
Hopefully this help get a bit closer to the goal.