Multiple values after THEN function in Tableau - tableau-api

IF [X] = "Pings" THEN [Pings]
ELSEIF [Y] = "Money" THEN [Rev]
ELSE NULL
END
I want to be able to assign [X] with [Pings] and [REV] in the same line but don't know how. First time using Tableau formula.
example:
IF [X] = "Pings" THEN [Pings] [REV]
ELSE NULL
END

Related

What is the best practice to recursively extract data from a nested structure in matlab?

I'm trying to exctract some data from a nested structure in a recursive manner. First, I know that this has a field (values) which repeats itself inside the nested structure. Secondly I know that the structure which has those values has only structures as fields. In the below code I've tried to acces the structure.values by searching if my current structure has a field named values. If it has, I put .values at the end of my structure name. If it doesn't have this field, I verify if all the fields are structures. If they are, it means that I will have to consider them further and to extract the values from each one. If the fields are not structures, it means that they are values and I save them into a new simplified structure. Example of fields that I want: S.values.model1.values.mission.values.(alt/list). Currently, with the below code I'm only able to get the values from one field and then I get an error and don't know how to approach further.
Code example:
clear all
clc
S=struct()
S.case='1';
S.type='A';
S.values.model1.case='2'
S.values.model1.type='C'
S.values.model1.values.mission.case='3'
S.values.model1.values.mission.type='D'
S.values.model1.values.mission.values.alt='none'
S.values.model1.values.mission.values.list=2
S.values.model1.values.mission.values.parameter=4
S.values.model1.values.phase.case='4'
S.values.model1.values.phase.type='A'
S.values.model1.values.phase.values.num='all'
S.values.model1.values.phase.values.eq=2
S.values.model1.values.phase.values.unit=4
S.values.model1.values.analysis.case='1'
S.values.model1.values.phase.type='A'
S.values.model1.values.phase.values.nump1.list='all'
S.values.model1.values.phase.values.nump1.table='four'
S.values.model1.values.phase.values.nump1.mean=0
S.values.model1.values.phase.values.nump2.list='none'
S.values.model1.values.phase.values.nump2.table='three';
S.values.model1.values.phase.values.nump2.mean=1
s=S.values.model1;
names=fieldnames(s);
nnames=numel(names);
newStruct={};
[valsi,newstructi]=extractValues(names,s,nnames,newStruct)
function [vals,newStruct]=extractValues(names,vals,nnames,newStruct)
if any(strcmp(names,'values'))
vals=vals.('values');
names=fieldnames(vals)
nnames=numel(names)
[vals,newStruct]=extractValues(names,vals,nnames,newStruct);
end
for j=1:nnames
value(j)=isstruct((vals.(names{j})));
end
if all(value)
for k=1:nnames
vals=(vals.(names{k}));
names=fieldnames(vals);
nnames=numel(names);
[vals,newStruct]=extractValues(names,vals,nnames,newStruct);
end
else
for j=1:nnames
value=(vals.(names{j}));
newStruct.(names{j})=value;
end
end
end
As it is known beforehand what fields are requested you can arrange the subsequent filed names in a cell array and use a loop to extract the value:
names = {'values', 'model1', 'values', 'mission', 'values', 'alt'};
out = S;
for name : names
out = out.(name{1});
end
So that is a loop version of using:
out = S.values.model1.values.mission.values.alt;
EDIT:
If you want to list all field names and all field values you can used these functions:
function out = names(s, p)
if isstruct(s)
out = {};
f = fieldnames(s);
for i = 1:numel(f)
s1 = s.(f{i});
p1 = [p '.' f{i}];
out = [out; names(s1, p1)];
end
else
out = {p};
end
end
function out = values(s)
if isstruct(s)
out = {};
f = fieldnames(s);
for i = 1:numel(f)
out = [out; values(s.(f{i}))];
end
else
out = {s};
end
end
Use them as:
n = names(S, 'S');
v = values(S);

Macro to find a value in one column and a value in another column to return the values in different columns when both the previous values are TRUE

I have a worksheet that I need to pull in data from another file. I need a Macro to take the values in Column A and Column D respectively in each row of the active worksheet and compare the those values with values in the other file, Columns A and F respectively. If a match is found somewhere within the rows of this worksheet, then I want it to take the values within Column B and C and place them into Column B and C of the first worksheet. I am new to building macros and this one just has me stumped on how to get what I need. Can anyone help?
Example of Worksheets:
First Worksheet
Second Worksheet
I figured out a solution. I combined the top assembly and the comp item and did a VLOOKUP for both of the columns. It's sloppy for it works...
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Sheets("Potential Issues").Select
Range("B1").Select
ActiveCell.FormulaR1C1 = "Unique Top Assembly Comp Item Number"
Range("C1").Select
ActiveCell.FormulaR1C1 = "NHA Item"
Range("D1").Select
ActiveCell.FormulaR1C1 = "NHA User Item Type"
With ActiveSheet.Range("A2").CurrentRegion
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
With .Columns(2)
.Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-1]&RC[3]"
End With
End If
End With
Cells.Select
Selection.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
With ActiveSheet.Range("A2").CurrentRegion
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
With .Columns(3)
.Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = _
"=VLOOKUP(C[-1],'B2.0 Buys Exploded BOM'!C2:C4,2,0)"
End With
End If
End With
With ActiveSheet.Range("A2").CurrentRegion
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
With .Columns(4)
.Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = _
"=VLOOKUP(C[-2],'B2.0 Buys Exploded BOM'!C2:C4,3,0)"
End With
End If
End With
Cells.Select
Selection.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:B").Select
Selection.ColumnWidth = 21
Columns("C:C").Select
Selection.ColumnWidth = 24
Columns("D:D").Select
Selection.ColumnWidth = 15
Range("B:B,G:H").Select
Selection.EntireColumn.Hidden = True
Range("A1").Select

How to use inputs in an if statement in matlab

I'm trying to write a code in MATLAB that has the user input two values. I already have everything written for the input part and I saved the two inputs into two variables: value1 and value2.
What I'm trying to do is use the input values in the matter of:
if value1 = 2
output_result=10
if value1 = 3
output_result=20
and so on.
I've been trying to write an if-elseif statement but I can't seem to figure it out.
Do a switch statement
switch value1
case 2
result = 10;
case 3
result = 20;
...
otherwise
statements
end
If you really want to use an if statement, do this:
if value == 1
result = 10;
elseif value == 2
result = 20;
elseif
%// Put more statements
...
elseif
%// Put even MOAR statements
...
...
else
%// Default case - optional
end
However, the switch statement as per #kkuilla is more elegant. Also note that the else statement is optional. You'd only put this in if everything else fails and want to use a default case.

Struggling to vertically append a string ( csv file) to an existing matrix in Scilab/matlab

M=read_csv("Gradesheets\MECN2073_2014.csv")
disp(size(M,1))
for x=1:1:size(M,1)
StudNum=M(x,1)
Grd=M(x,2) // Grd is grade column in gradesheets hence x,2 (column 2 of Grd )
if find(StudNum==StudentNumbers)>0 then disp("Exists")
H = [StudNum]
StudentNumbers = [StudentNumbers;StudNum]
end
end
This is where i get error:
Undefined operation for the given operands.
check or define function %s_f_c for overloading.
at line 38 of exec file called by :
I assume that StudentNumbers is the vector containing all your existing students right? If so, try the code below and let me know what you get.
M=read_csv("Gradesheets\MECN2073_2014.csv")
storing = [];
for i=1:size(M,1)
StudNum=M(i,1)
if find(StudNum==StudentNumbers(i)) == 1
disp("Exists");
storing = [storing;StudNum];
end
end

How to return NaN or empty matrix if a condition is false

I have a question based on the below function. How to return a NaN or empty matrix if a condition is false.
Below function actually check if the Anchor ID and Source ID are present and if such a combination exists it goes further inside the loop to check if Anchor Channel and Source Channel exists if it exists then it will calculate "y" and goes on. But what if the Anchor Channel and source Channel does not exist for this condition ? and also consider what if Anchor ID and source ID does not exist !!! If it does not exist then I want it to return to NaN or simply nil.
How to modify this function to my requirements ??
function [rssi_dBm1]= sampletrue(BlinkSetList,AnchorID,SourceID)
for i=1:length(BlinkSetList)
S=cell2mat(BlinkSetList(i));
for j=1:length(S)
if S(j).AnchorID==AnchorID && S(j).SourceID==SourceID
if S(j).AnchorChan==0 && S(j).SourceChan==0
y=S(j).agc;
rssi_dB1(i)= -(33+y*(89-33)/(29-1));
else
rssi_dB1(i)=NaN;
end
end
end
end
rssi_dB1(rssi_dB1==0)=[];
rssi_dBm1=sum(rssi_dB1(:))/length(rssi_dB1);
disp([sprintf('The rssi value with A-Chan 0 and S-Chan 0 is %0.0f',rssi_dBm1)]);
Note: This is just a one part of the conditions, there are still three more combinations for anchor channel and source channel to evaluate.
If the question is still not clear post your doubts and I will try to explain it more precisely.
Your help is highly appreciated . Thanks in advance.
An example of function returning Nan
function ret = retNan( value )
if value == true
ret = 1;
else
ret = NaN; % set returned value to Nan
end
A function that returns an empty matrix
function ret = retEmpty( value )
if value == true
ret = 1;
else
ret = []; % set returned value to an empty matrix
end
EDIT:
Bottom line, whatever the value of variable ret (or in your case, rssi_dBm1) is at the end of the function - this value is returned. So, if ret is empty or NaN the function simply returns an empty/NaN value.
You may set rssi_dBm1 to be an empty matrix at the beginning of the function and only change it if conditions are satisfied. In that case if all conditions fails the function will return the default value - an empty matrix.
PS,
1. It is best not to use i and j as variable names in Matlab.
2. You can use mean instead of sum()/length().
3. instead of disp( sprintf(...) ) you may use fprintf(1, ... )