person quantity
A 2
B 3
C 4
D 5
E 8
F 10
G 15
H 20
I 55
J 30
K 21
L 4
M 6
N 10
O 25
P 22
No of people having quantity <25
No of people having quantity >25
please help for the above questions
First what you need to do is make a parameter of the type string:
Then you make a field that is working from the values set in the parameter. Use a case statement which has the logic "If the parameter is x, do this; if y do this"
For the "do this" part, you can use an if statement as well:
CASE [Quantity Paramter]
WHEN ">= 25 Quantity" THEN IF [Quantity] >= 25 THEN [Person] END
WHEN "< 25 Quantity" THEN IF [Quantity] < 25 THEN Person END
END
This newly created field is your new Person field and so you should drag that onto the Columns or Rows pills
Make sure to exclude the null values by using a filter on your new field. The null values in this context are the values that are not satisfying what you've chosen in the parameter. So if you've selected >=25, then a person with 2 would be a null
The below is demonstrating it working with the sample data:
Person Quantity
A 3
B 57
C 4
D 5
E 10
F 50
G 7
H 3
I 2
Related
I have one table:
a b
x 5
y 20
z 10
i want to have a sum of column b but without aggregation, thus i would get this as result:
a b c
x 5 35
y 20 35
z 10 35
This should be possible right? How this the Select comming from the table above?
You can use window functions for that:
select a, b, sum(b) over () as c
from the_table
order by a
I have two tables A and B. I want to join them based on their validity time intervals.
A has product quality (irregular times) and B has hourly settings during the production period. I need to create a table like C that includes the parameters p1 and p2 for all A's RefDates that fall in the time range of B's ValidFrom ValidTo.
A
RefDate result
'11-Oct-2017 00:14:00' 17
'11-Oct-2017 00:14:00' 19
'11-Oct-2017 00:20:00' 5
'11-Oct-2017 01:30:00' 25
'11-Oct-2017 01:30:00' 18
'11-Oct-2017 03:03:00' 28
B
ValidFrom ValidTo p1 p2
'11-Oct-2017 00:13:00' '11-Oct-2017 01:12:59' 2 1
'11-Oct-2017 01:13:00' '11-Oct-2017 02:12:59' 3 1
'11-Oct-2017 02:13:00' '11-Oct-2017 03:12:59' 4 5
'11-Oct-2017 03:13:00' '11-Oct-2017 04:12:59' 6 1
'11-Oct-2017 04:13:00' '11-Oct-2017 05:12:59' 7 9
I need to get something like this.
C
RefDate res p1 p2
'11-Oct-2017 00:14:00' 17 2 1
'11-Oct-2017 00:14:00' 19 2 1
'11-Oct-2017 00:20:00' 5 2 1
'11-Oct-2017 01:30:00' 25 3 1
'11-Oct-2017 01:30:00' 18 3 1
'11-Oct-2017 03:03:00' 28 4 5
I know how to do this in SQL and I think I have figured out how to do this row by row in MatLab but this is horribly slow. The data set is rather large. I just assume there must be a more elegant way that I just couldn't find.
Something that caused many of my approaches to fail is that the RefDate column is not unique.
edit:
the real tables have thousands of rows and hundreds of variables.
C (in reality)
RefDate res res2 ... res200 p1 p2 ... p1000
11-Oct-2017 00:14:00 17 2 1
11-Oct-2017 00:14:00 19 2 1
11-Oct-2017 00:20:00 5 2 1
11-Oct-2017 01:30:00 25 3 1
11-Oct-2017 01:30:00 18 3 1
11-Oct-2017 03:03:00 28 4 5
This can actually be done in a single line of code. Assuming your ValidTo value always ends immediately before the ValidFrom in the next row (which it does in your example), you only need to use your ValidFrom values. First, convert those and your RefDate values to serial date numbers using datenum. Then use the discretize function to bin the RefDate values using the ValidFrom values as the edges, which will give you the row index in B that contains each time in A. Then use that index to extract the p1 and p2 values and append them to A:
>> C = [A B(discretize(datenum(A.RefDate), datenum(B.ValidFrom)), 3:end)]
C =
RefDate result p1 p2
______________________ ______ __ __
'11-Oct-2017 00:14:00' 17 2 1
'11-Oct-2017 00:14:00' 19 2 1
'11-Oct-2017 00:20:00' 5 2 1
'11-Oct-2017 01:30:00' 25 3 1
'11-Oct-2017 01:30:00' 18 3 1
'11-Oct-2017 03:03:00' 28 4 5
The above solution should work for any number of columns pN in B.
If there are any times in A that don't fall in any of the ranges in B, you will have to break the solution into multiple lines so you can check whether or not the index returned from discretize contains NaN values. Assuming you want to exclude those rows from C, this would be the new solution:
index = discretize(datenum(A.RefDate), datenum(B.ValidFrom));
C = [A(~isnan(index), :) B(index(~isnan(index)), 3:end)];
The following code does exactly what you are asking for:
% convert to datetime
A.RefDate = datetime(A.RefDate);
B.ValidFrom = datetime(B.ValidFrom);
B.ValidTo = datetime(B.ValidTo);
% for each row in A, find the matching row in B
i = cellfun(#find, arrayfun(#(x) (x >= B.ValidFrom) & (x <= B.ValidTo), A.RefDate, 'UniformOutput', false), 'UniformOutput', false);
% find rows in A that where not matched
j = cellfun(#isempty, i, 'UniformOutput', false);
% build the result
C = [B(cell2mat(i),:) A(~cell2mat(j),:)];
% display output
C
I have data which includes 2 columns, ages, and groups similar to
B 1 B 1 B 1 B 4 B 5 B 8 D 2 D 2 D 3 D 3 D 3 D 4 D 6 D 7 D 9 D 9
In Tableau, I wish to plot a line for each group B and D, % number of records(observations) (of group in group), against the age range, 1 to 9.
So B 1 - 3/6*100, B 5 1/6*100, D 3 - 3/10*100.
Any help or pointers would be really appreciated.
Enda
Drag 'Age' measure in columns.
Drag 'Group' dimension in 'Color'
Drag the tableau default measure of 'Number of Records' in rows. Make it's aggregation as 'sum', add quick table calculation of 'Percent of Total'. Change it's 'Compute Using' to 'Age'.
That's it! Hopefully this is what you were trying to do.
Consider an example given below. There are 3 customers A, B,C.
1st row of matrix is the demand of respective customer and second row is the day when they need.
for example: demand A=[10,40,50;15,45,75]; Customer A needs 10 items on 15th day.. 40 items on 45th day and 50 items on 75th day..Similarly for B,C.
demand A=[10,40,50;15,45,75];
demand B=[80,30,20;05,35,80];
demand C=[50,40,30;20,47,88];
Now i need to rank the customer on basis of days. So here answer should be like
rank 1: 5th day customer B 80 items
rank 2: 15th day customer A 10 items
rank 3: 20th day customer C 50 items.
and so on.
How can i do it in mat lab. so that when i rank it on basis of the day then I should then know how many items and which customer accordingly.
output should be like this:
Rank Customer items day
1 B 80 05
2 A 10 15
3 C 50 20
4 B 30 35
5 A 40 45
6 C 40 47
7 A 40 75
8 B 20 80
9 C 30 88
I suggest the following approach:
First stage
generates a new matrix, which is the composition of A,B and C, such that:
The first col represents the day.
The second col represents the requested amount.
The third col is the costumer index (A=1,B=2,C=3).
res = [A',ones(size(A',1),1);B',ones(size(A',1),1)*2;C',ones(size(C',1),1)*3];
res(:,[2,1]) = res(:,[1,2]);
Second stage
sort the matrix according to the first column, which represents the day
[~,sortedDaysIndices] = sort(res(:,1));
res = res(sortedDaysIndices,:);
Third stage: print the results
for ii=1:size(res)
if res(ii,3)==1
costumerStr = 'A';
elseif res(ii,3)==2
costumerStr = 'B';
else
costumerStr = 'C';
end
fprintf('%s\n',[num2str(ii) ' ' costumerStr ' ' num2str(res(ii,2)) ' ' num2str(res(ii,1))])
end
Result
1 B 80 5
2 A 10 15
3 C 50 20
4 B 30 35
5 A 40 45
6 C 40 47
7 A 50 75
8 B 20 80
9 C 30 88
I'm trying to compare two cell arrays that contain both characters and numbers. I would like to compare two specific columns and then return the values in another related column.
For example, I have two cell arrays of the forms:
One= Two=
[A 2 10 [A 1 2 76
B 2 11 B 1 2 78
A 5 22 C 1 2 80
B 5 23 D 1 4 98
A 6 28 E 1 4 99
B 6 28 F 1 4 100
C 6 28] G 1 6 110]
And I want to be able to find everywhere column 2 of 'One' equals column 3 of 'Two' and return the specific value in column 4 of 'Two.' So for this example, I would obtain a result that is:
Three=
[76
78
80
110]
Any help would be appreciated.
Option 1: convert to numeric array first
X = cell2mat(One(:,2:end));
Y = cell2mat(Two(:,2:end));
result = Y(X(:,1)==Y(:,2),3)
Option 2: convert to numeric array at various points
result = cell2mat(Two(cell2mat(One(:,2))==cell2mat(Two(:,3)),4))
Option 3: convert cell to table first
T1 = cell2table(One);
T2 = cell2table(Two);
result = T2.Two4(T1.One2==T2.Two3)
Option 4: abuse how Matlab cell arrays and numeric arrays work
result = [Two{([One{:,2}]==[Two{:,3}])',4}]'