Google Sheets Importrange statement in SWITCH - if-statement

I'm using the importdata statement inside in Switch condition and it does not return anything nor give an error.
My formula:
=SWITCH(C5,"Desired Value",IMPORTRANGE("url to the google spreadsheet", "MyTab!A:E"),C5,"Another Value","Another Value Output")
So when it's "Desired Value" is prints nothing.
If I write a separate IMPORTRANGE without SWITCH it works fine.
EDIT:
Example
Source File (from what I'm importing) link and the file with SWITCH statement link

drop the SWITCH and use IF like:
=IF(B1="Tab1", IMPORTRANGE("19csOm3txsZ-RAUiDYbEJLmWUsm5b-Qk9vkMMdJZ6CqM", "TAB1!A1:A19"),
IF(B1="Tab2", IMPORTRANGE("19csOm3txsZ-RAUiDYbEJLmWUsm5b-Qk9vkMMdJZ6CqM", "TAB2!A1:A19"), ))

Related

I have a google sheets cell with 5 different texts (from a dropdown) - how can I make another cell return a value based on the text

The 5 values in J4 are Studio, 1, 2, 3, 4.
Mortgage! is another sheet where I want to take B2*(52/12) if studio, B3*(52/12) if 1 and so on
Here is what I have been working with. I think I am close I just can't get it together to work
If
$J$4='Studio',(Mortgage!B2*(52/12)
$J$4='1',(Mortgage!B3*(52/12)
$J$4='2',(Mortgage!B3*(52/12)
$J$4='3',(Mortgage!B4*(52/12)
$J$4='4',(Mortgage!B5*(52/12)
Your logic is acceptable. You need to use the correct syntax.
=IF(
$J$4='Studio',Mortgage!B2*(52/12),
IF($J$4='1',Mortgage!B3*(52/12),
IF($J$4='2',Mortgage!B3*(52/12),
IF($J$4='3',Mortgage!B4*(52/12),
$J$4='4',Mortgage!B5*(52/12)
))))
By the way, it is customary to post a link to a public Google Sheet showing sample data when asking questions about Google Sheets.
Firstly, I think you have an errory in your sample code as you say I want to take B2*(52/12) if studio, B3*(52/12) if 1 and so on, but your sample is doing (Mortgage!B3*(52/12) for both $J$4='1' and $J$4='2'.
Therefor, I will assumne you are instead expecting...
if
$J$4='Studio',(Mortgage!B2*(52/12)
$J$4='1',(Mortgage!B3*(52/12)
$J$4='2',(Mortgage!B4*(52/12)
$J$4='3',(Mortgage!B5*(52/12)
$J$4='4',(Mortgage!B6*(52/12)
As others suggested, you can achive this by applying a nested IF() function with correct syntax,
but other than standard IF() funciont, you can use IFS() function for these kinds of cases:
=IFS(
$J$4='Studio',Mortgage!B2*(52/12),
$J$4='1',Mortgage!B3*(52/12),
$J$4='2',Mortgage!B4*(52/12),
$J$4='3',Mortgage!B5*(52/12),
$J$4='4',Mortgage!B6*(52/12)
)
There is also a SWITCH() function in google sheet that do similar things:
=SWITCH($J$4,
'Studio',Mortgage!B2*(52/12),
'1',Mortgage!B3*(52/12),
'2',Mortgage!B4*(52/12),
'3',Mortgage!B5*(52/12),
'4',Mortgage!B6*(52/12)
)
You may even create your own array to lookup for result:
=LAMBDA(DATA,
VLOOKUP($J$4,DATA,2,FALSE)
)({
'Studio',Mortgag!B2*(52/12);
'1',Mortgage!B3*(52/12);
'2',Mortgage!B4*(52/12);
'3',Mortgage!B5*(52/12);
'4',Mortgage!B6*(52/12)
})
where the array part:
{
'Studio',Mortgag!B2*(52/12);
'1',Mortgage!B3*(52/12);
'2',Mortgage!B4*(52/12);
'3',Mortgage!B5*(52/12);
'4',Mortgage!B6*(52/12)
}
can be shortened into something like this:
ArrayFormula({{'Studio';'1';'2';'3';'4'},Mortgage!B2:6*(52/12)})
or
ArrayFormula({{'Studio';TO_TEXT(SEQUENCE(4))},Mortgage!B2:6*(52/12)})
Combine this with the VLOOKUP(), you get this:
=ArrayFormula(VLOOKUP($J$4,{{'Studio';TO_TEXT(SEQUENCE(4))},Mortgage!B2:6*(52/12)},2,FALSE))
XLOOKUP() function also works in a similar way as VLOOKUP():
=ArrayFormula(XLOOKUP($J$4,{'Studio';TO_TEXT(SEQUENCE(4))},Mortgage!B2:6*(52/12)))

Nested IF function inside a vlookup for google sheets

I'm trying to nest an IF function inside a vlookup to filter data based on a cell in another sheet.
The ideal functionality would be for the cell to return data if it meets a specific criteria otherwise leave it blank.
This is what I have so far:
=IFERROR(vlookup(E2,IF('internal_all[Paste Here]'!H3="Indexable",'internal_all[Paste Here]'!A:AB,""),28,0))
Currently, it doesn't work but instead I get an error "VLOOKUP equates to an out of bounds range".
Any help would be much appreciated.
Here's a copy of my gsheet: https://docs.google.com/spreadsheets/d/13mcOOHIP6-YQKOhE_LHnmu3-7bSNKzvCSTVZ5BnTxPA/edit#gid=0
Here's an example of the error I'm running into:
If I understand your question correctly...Then all you would need to change is where you put the if. Including it in the Vlookup will mess with the size of range ie. A:AB has a size of 28 but "" has a size of 0. I would also change your formula to an array so you don't have 6000+ formulas calculating. Finally, I would add an array_constrain to stop the array at the end of your dataset. Here is a working example:
=Array_Constrain(ArrayFormula(IF('internal_all [Paste Here]'!B2:B="Indexable",vlookup(E2:E,'internal_all [Paste Here]'!A2:AB,28,0),"")),CountIf(A2:A,"<>"),1)
I also included that formula in H2 of your "internal links point to canonicalised URLs" tab. Good luck!
Edit:
After looking at the setup again, this updated formula should work.
=Array_Constrain(ArrayFormula(if(VLOOKUP(E2:E,'internal_all [Paste Here]'!A:B,2)="Indexable",vlookup(E2:E,'internal_all [Paste Here]'!A2:AB,28,0),)),CountIf(A2:A,"<>"),1)
Also, here is an updated Indexability formula for column G
=Array_Constrain(ArrayFormula(IFERROR(vlookup(G2:G,'internal_all [Paste Here]'!A:B,2,0))),CountIf(A2:A,"<>"),1)

PDI - Check data types of field

I'm trying to create a transformation read csv files and check data types for each field in that csv.
Like this : the standard field A should string(1) character and field B is integer/number.
And what I want is to check/validate: If A not string(1) then set Status = Not Valid also if B not a integer/number to. Then all file with status Not Valid will be moved to error folder.
I know I can use Data Validator to do it, but how to move the file with that status? I can't find any step to do it.
You can read files in loop, and
add step as below,
after data validation, you can filter rows with the negative result(not matched) -> add constant values step and with error = 1 -> add set variable step for error field with default values 0.
after transformation finishes, you can do add simple evaluation step in parent job to check value of ERROR variable.
If it has value 1 then move files else ....
I hope this can help.
You can do same as in this question. Once read use the Group by to have one flag per file. However, this time you cannot do it in one transform, you should use a job.
Your use case is in the samples that was shipped with your PDI distribution. The sample is in the folder your-PDI/samples/jobs/run_all. Open the Run all sample transformations.kjb and replace the Filter 2 of the Get Files - Get all transformations.ktr by your logic which includes a Group by to have one status per file and not one status per row.
In case you wonder why you need such a complex logic for such a task, remember that the PDI starts all the steps of a transformation at the same time. That's its great power, but you do not know if you have to move the file before every row has been processed.
Alternatively, you have the quick and dirty solution of your similar question. Change the filter row by a type check, and the final Synchronize after merge by a Process File/Move
And a final advice: instead of checking the type with a Data validator, which is a good solution in itself, you may use a Javascript like
there. It is more flexible if you need maintenance on the long run.

foreach loop running but not giving results

I am having trouble running a foreach loop. The loop runs without error but gives no output. Can someone tell me what they think might be going on? Many thanks in advance!
Here is the code:
cd "O:\RESEARCH\ikhilko\Subway Big Data project"
local datafiles : dir . files "*.txt"
foreach file in `datafiles' {
insheet using `file',
clear
insheet using `file',
drop v9-v43
save date1, replace
}
UPDATE:
Interestingly, the code runs when I just type it into the command line, rather than doing it from the .do file, any idea what might be going on there?
It is important to note that local macros are precisely that, i.e. defined and visible only locally.
Locally means within
the same interactive session
or
the same program
or
the same do file (or do file editor contents)
or
the same part of the do file (or ...) executed by selection
Locality is, it seems, biting you here. A local macro defined in one place is not visible in another. A local macro reference will evaluate to missing, i.e. an empty string, if the macro is not visible.
Some code for the debugging. display the contents of your local datafiles to see what's going into the loop:
local datafiles : dir . files "*.txt"
display `"`datafiles'"'
local wordx : word 1 of `datafiles'
display `"`wordx'"'
foreach file in `datafiles' {
display "`file'"
}
(The code does not format well in the comments section.)

how can i get the result of the output clause in an update statement

im programming in c++ using ado, the documnet on tsql says the OUTPUT clause can return the result to the calling program, is there anyway i can retrieve it with ado in c++ ? if it is possible how to do that ?
Setup an extra parameter in your ADO object and have the parameter direction as output. Then have your original update statement define an extra variable (which is the output variable) and assign your result to that output variable.