Tableau Formatting Numbers in calculated field - tableau-api

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.

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

What is the problem in this code and how is this possible to write -3 index to an array. Like : y[-3] = 1st digit of ID number?

For this function:
y(t) =(sum from -inf to +inf) y[n]*p(t-nTs)
I wrote :
%main function
function project(arr,n,t,Ts)
symsum(arr(n)*p(t,-n*Ts),n,-inf,inf)
end
function rValue = p(t,Ts)
%standard square pulse of duration Ts:
if t<=(Ts/2)&& t>=(-Ts/2)
rValue = 1;
return
%standard triangular pulse of duration 2Ts:
elseif t<=Ts && t>=-Ts
rValue = (1-(abs(t)/Ts));
return
%truncated ideal pulse:
elseif t <=(3*Ts) && t>=(3*Ts)
rValue = sin(pi*t/Ts)/(pi*t/Ts);
return
%a pulse signal consisting of three parabolic segments
elseif t==no idea
rValue = no idea;
return
else
rValue = 0;
return
end
end
after function I have to create an array but what the assignment wants is like y[-3] . How is this possible to write -3 index to an array. Like :
y[-3] = 1st digit of ID number (leftmost digit)
y[-2] = -2nd digit of ID number
y[-1] = 0
y[0] = 3rd digit of ID number
y[1] = -4th digit of ID number
y[2] = 5th digit of ID number
y[3] = 0
y[4] = -6th digit of ID number
y[5] = 7th digit of ID number
y[6] = 8th digit of ID number (rightmost digit)
y[n] = 0 otherwise (-inf < n < inf)
If you absolutely need something that can be indexed using negative numbers you can use key-map pairs. Unfortunately, this method requires you to index using a char indicated by the single quotes ''. I would advise against typically indexing with negatives but this may help in your case. Also, you can evaluate this summation from the range n = -3 to n = 6 since the multiplication everywhere else outside that range is effectively zero.
Key-Map Pairs:
Keys = {'-3','-2','-1','0','1','2','3','4','5','6'};
Digits = [1 -2 0 3 -4 5 0 -6 7 8];
y = containers.Map(Keys,Digit);
y('-3')
y('-2')
y('-1')
y('0')
y('1')
y('2')
y('3')
y('4')
y('5')
y('6')
Alternatively by Using Anonymous Functions:
Thanks to #Hoki for this amazing alternative solution. It remaps the negative indices by using an anonymous function.
y = [1 -2 0 3 -4 5 0 -6 7 8];
idx = #(n) n+4;
y(idx(-3))
y(idx(-2))
y(idx(-1))
y(idx(0))
y(idx(1))
y(idx(2))
y(idx(3))
y(idx(4))
y(idx(5))
y(idx(6))
Ran using MATLAB R2019b

Dynamic color format values - Tableau

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

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

Count occurrences of an event by date

I am facing an issue with counting number of occurrences by date, suppose I have an excel file where the data is as follows:
1/1/2001 23
1/1/2001 29
1/1/2001 24
3/1/2001 22
3/1/2001 23
My desired output is:
1/1/2001 3
2/1/2001 0
3/1/2001 2
Though 2/1/2001 does't appear in the input, I want that included in the output with 0 counts. This is my current code:
[Value, Time] = xlsread('F:\1km\fire\2001- 02\2001_02.xlsx','Sheet1','A2:D159','',#convertSpreadsheetExcelDates);
tm=datenum(Time);
val=Value(:,4);
data=[tm val];
% a=(datestr(tm));
T1=datetime('9/23/2001');
T2=datetime('6/23/2002');
T = T1:T2;
tm_all=datenum(T);
[~, idx] = ismember(tm_all,data(:,1));
% idx=idx';
out = tm_all(idx);
The ismember function does not seem to work, because the length of tm_all is 274 and the size of data is 158x2
I suggest you to use datetime instead of datenum for converting your date strings into a serial representation, this can make (not only) the whole computation much easier:
tm = datetime({
'1/1/2001';
'1/1/2001';
'1/1/2001';
'3/1/2001';
'3/1/2001'
},'InputFormat','dd/MM/yyyy');
Once you have obtained your datetime vector, the calculation can be achieved as follows:
% Create a sequence of datetimes from the first date to the last date...
T = (min(tm):max(tm)).';
% Build an indexing of every occurrence to the regards of the sequence...
[~,idx] = ismember(tm,T);
% Count the occurrences for every occurrence...
C = accumarray(idx,1);
% Put unique dates and occurrences together into a single variable...
res = table(T,C)
Here is the output:
res =
T C
___________ _
01-Jan-2001 3
02-Jan-2001 0
03-Jan-2001 2
For more information about the functions used within the computation:
accumarray function
ismember function
On a side note, I didn't understand whether your dates are in dd/MM/yyyy or in MM/dd/yyyy format... because with the latter, you cannot have that output using my approach, and you should also implement an algorithm for detecting the current month and then splitting your data over a monthly (and eventually yearly, if your dates span over 2001) criterion instead:
tm = datetime({
'1/1/2001';
'1/1/2001';
'1/1/2001';
'3/1/2001';
'3/1/2001'
},'InputFormat','MM/dd/yyyy');
M = month(tm);
M_seq = (min(M):max(M)).';
[~,idx] = ismember(M,M_seq);
C = accumarray(idx,1);
res = table(datetime(2001,M_seq,1),C)
res =
Var1 C
___________ _
01-Jan-2001 3
01-Feb-2001 0
01-Mar-2001 2
I'll first give the code and then explain step by step.
code:
[Value, Time] = xlsread('stack','A1:D159','',#convertSpreadsheetExcelDates);
tm=datenum(Time);
val=Value(:,4);
data=[tm val];
a=(datestr(tm));
T1=datetime('1/1/2001');
T2=datetime('6/23/2002');
T = T1:T2;
tm_all=datenum(T);
[~, idx] = ismember(tm_all,data(:,1)); % get indices
[occurence,dates]= hist(data(:,1),unique(data(:,1))); % count occurences of dates from file
t = [0;data(:,1)]; % add 0 to dates (for later because MATLAB starts at 1
[~,idx] = ismember(t(idx+1),dates); % get incides
q = [0 occurence]; % add 0 to occurence (for later because MATLAB starts at 1
occ = q(idx+1); % make vector with occurences
out = [tm_all' occ']; % output
idx of ismember is an 1xlength(tm_all) vector that at position i contains the lowest index of where tm_all(i) is found in data(:,1). So take for example A = [1 2 3 4] and B = [1 1 2 4] then for [~,idx] = ismember(A,B) the result will be
idx = [1 3 0 4]
because A(1) = 1 and the first 1 in B is found at posistion 1. If a number in A doesn't occur in B, then the result will be 0.
[occurence,dates]= hist(data(:,1),unique(data(:,1))); gives the number of occurences for the dates.
t = [0;data(:,1)]; adds a zero in the beginning so tlooks like:
0
'date 1'
'date 2'
'date 3'
'date 4'
...
Why this is done, will be explained next.
t(idx+1) is a vector that is 1xlength(tm_all), and is kind of a copy of tm_all except that when a date doesn't occur in the file, the date is zero. How does this work? t(i) gives you the value of t at position i. So t( 1 5 4 2 9) is a vector with the values of t at positions 1, 5, 4, 2 and 9. Remember idx is the vector that contains the incides of the of the dates in data(:,1). Because Matlab indexing starts at 1, idx+1 is needed. The dates in data':,1) then must also be increased. That's done by adding the zero in the beginning.
[~,idx] = ismember(t(idx+1),dates); is the same as before, but idx now contains the indices of dates.
q = [0 occurence]; again adds a zero occ = q(idx+1); is the row of occurences of the dates.