Adding a formula to a range based on final row - count

I am attempting to add a formula to a range starting from the last row of data.
Establishing the last row
Going to a cell address with column and last row
Drop in a formula
Move up a row (-1)
Drop in the same formula
Repeat step 4 until Row 2
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For I = FinalRow To 2 Step -1
Range(Cells((I), 18)).Formula = "=IF Cells((I),9)=""C"",CONCATENATE (Cells((I),10)),"" "", Cells((I),11),,"" "", Cells((I),12),"" "",Cells((I),13),"" PAID WITH INVOICE "",Cells((I),2),"" FOR "",Cells((I),5),Cells((I),6),"" ON "",TEXT(Cell((I),17)),""mm/dd/yyyy"")),"" "")"
Next I
I'm getting the following error
Run-time error '1004'
Method'range' of object'_Global' failed

Related

QUERY formula to return only the latest record per email based on timestamp

Having this Google Sheet QUERY formula:
=QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > date '"&TEXTO(HOY()-3, "yyyy-mm-dd")&"' and E is not null and O is not null and Q contains '#' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"'", 0)
How to return only one record per email (column Q) being the most recent one looking at date which is at column A?
TEST SHEET:
https://docs.google.com/spreadsheets/d/1jBMo42cbylrNHcf9b9QLI4oDw0H0oomCGrEAuPsBlA8/edit#gid=1465616509
Under tab "results" is the working query where i only need to filter out duplicates based on recency, leaving only the most recent one, and the unique identifier being email
EDIT
(following OP's shared demo sheet)
The formula to use would be:
=SORT(SORTN(QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > datetime '"&TEXTO(AHORA()-3, "yyyy-mm-dd HH:mm:ss")&"' and E is not null and O is not null and Q contains '#' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"'", 1),9^9,2,2,1),1,1)
What was changed:
We wrapped your original query with the SORTN function to find and exclude all duplicate emails from column Q except the most recent one based on the Timestamp from column A.
We -one more time- wrapped the results with the SORT function to have our final results sorted by the Timestamp.
Pro tip
An extra change was also made within the main query by changing
and A > date '"&TEXT(HOY()-3, "yyyy-mm-dd")&"'
to
and A > datetime '"&TEXT(AHORA()-3, "yyyy-mm-dd HH:mm:ss")&"'
So we changed TODAY to NOW.
By doing this we count dates making use of both the date and time present in a timestamp instead of just the date.
(There is still room for minor improvements/alterations but out of the scope of this question.)
Original answer
Taking for granted that your formula works as expected (cannot check it without a test sheet) but produces multiple rows you can use the limit clause in the end of your formula.
=QUERY(FORMULARIO!A4:Q, " select G,E,K,M,N,L,O,Q where not upper(J) = 'PAGÓ' and A > date '"&TEXTO(HOY()-3, "yyyy-mm-dd")&"' and E is not null and O is not null and Q contains '#' and not Q matches '"&TEXTJOIN("|", 1, QUERY(FORMULARIO!J4:Q, "select Q where upper(J) = 'PAGÓ' ", 0))&"' limit 1", 0)

JuliaDB select a column from a string, not a symbol

I want to select a column from a JuliDB database. The problem is that I can't do it with string (not a symbol that starts with :), for example:
db = loadtable("table.dat")
#This table has 3 columns named position_1, position2, position_3
pos_num = 3
column_name = "position_$pos_num"
select(db,column_name)
If I do that, then the following error appears:
column position_3 not found.
Any suggestion?
You can create a symbol directly Symbol("position_$pos_num") or from your string with Symbol(column_name).

Teradata : using case statement in Where clause

My question is about using case statement in where clause to check for date and assign values to columns. My sample code include.
select * from table
where
column 1 > 10 and
case when column 2 = 1
then
column 3<= 10 and column 4 between (1st day of prev month) and (prev month end) or column 5 = '8888-01-01'
else
column 4 between (1st day of this month) and (yesterday)
end ;
when I am running this code. I am getting 3706 syntax error:expected something in between field and '='.
How to fix this ?
A CASE statement will always return a value or NULL (if none of the conditions matches), so you can use it in your WHERE clause. There are a couple ways to format your CASE statement:
Format 1
CASE
WHEN <condition> THEN <some_expression>
WHEN <another_condition> THEN <another_expression>
ELSE <final_expression>
END
-- Example
CASE
WHEN col1 = 10 THEN 'Y'
WHEN col1 = 20 THEN 'N'
ELSE 'N/A'
END
Format 2
CASE <expression>
WHEN <value> THEN <expression>
WHEN <another_value> THEN <another_expression>
ELSE <final_expression>
END
-- Example
CASE col1
WHEN 10 THEN 'Y'
WHEN 20 THEN 'N'
ELSE 'NA'
END
I'm not sure what you're trying to do with your sample code, but it looks more like pseudo-code and will not work as-is. Your CASE statement is not formatted properly and your column references like column 1 will not work that way. If your column is actually named column 1, then you need to put double-quotes around it:
select * from table where "column 1" > 10
Can you please describe a little more clearly what exactly you are trying to do?
A CASE expression can't be used to create some kind of dynamic conditions. Write it as a bunch of AND/OR conditons:
select * from table
where
column 1 > 10 and
(
( column 2 = 1 and
(column 3<= 10 and column 4 between (1st day of prev month) and (prev month end) or column 5 = '8888-01-01')
)
or
column 4 between (1st day of this month) and (yesterday)
);
Double check the logic, the precedence of logical operators is
parenthesis
NOT
AND
OR

How to check the duplicate row with multiple column in infopath repeating table

I want to check the duplicate row with three columns(ProductID,ProductName,ProductCategory). So user should not enter duplicate row with the same ProductID,ProductName and ProductCategory(with these 3 combination). ex1:
ProductID ProductName ProductCategory
1 Aciloc150 Medicine
User should be restricted to enter the above same ProductID,ProductName and ProductCategory(1 Aciloc150 Medicine)
I know the below code using this I can check the duplicate value in one column, How can I check the duplicate row with the above combination(ProductID ProductName ProductCategory)?
count(/my:myFields/my:group1/my:group2[my:field2 != ""]) > 1 and (. = ../preceding-sibling::my:group2/my:field2 or . = ../following-sibling::my:group2/my:field2)
Here /my:myFields/my:group1/my:group2 -> repeating group xpath
My:field2 -> field in repeating table

count the unique values in one column in EXCEL 2010 or R with 1 million rows

After searching the forum, I did not find a good solution for this question. If I missed it, please tell me.
I need to count the unique values in one column in EXCEL 2010.
The worksheet has 1 million rows and 10 columns. All cell values are string or numbers.
I used the solution at Count unique values in a column in Excel
=SUMPRODUCT((A2:A1000000<>"")/COUNTIF(A2:A100000,A2:A1000000&""))
But, it runs so long time that the EXCEL is almost frozen. And, it generates 25 processes in Win 7.
Are there more efficient ways to do it?
Also, in the column, all values have for format of
AX_Y
here, A is a character, X is an integer, Y is an integer from 1 to 10.
For example, A5389579_10
I need to cut off the part after (including) undersocre. for the example,
A5389579
This is what I need to count as unique values in all cells in one column.
For example, A5389579_10
A1543848_6
A5389579_8
Here, the unique value has 2 after removing the part after underscore.
How to do it in EXCEL VBA and R (if no efficient solution for EXCEL)?
If you want to do this by VBA, you can take advantage of the Collection object. Since collections can only contain unique values, trying to add all of your input data to a collection will result in an array of unique values. The code below takes all the variables in a selected range and then outputs an array with distinct values to an other sheet (in this case a sheet named Output).
Sub ReturnDistinct()
Dim Cell As Range
Dim i As Integer
Dim DistCol As New Collection
Dim DistArr()
Dim OutSht As Worksheet
Dim LookupVal As String
Set OutSht = ActiveWorkbook.Sheets("Output") '<~~ Define sheet to putput array
If TypeName(Selection) <> "Range" Then Exit Sub
'Add all distinct values to collection
For Each Cell In Selection
If InStr(Cell.Value, "_") > 0 Then
LookupVal = Mid(Cell.Value, 1, InStr(Cell.Value, "_") - 1)
Else
LookupVal = Cell.Value
End If
On Error Resume Next
DistCol.Add LookupVal, CStr(LookupVal)
On Error GoTo 0
Next Cell
'Write collection to array
ReDim DistArr(1 To DistCol.Count, 1 To 1)
For i = 1 To DistCol.Count Step 1
DistArr(i, 1) = DistCol.Item(i)
Next i
'Outputs distinct values
OutSht.Range("A1:A" & UBound(DistArr)).Value = DistArr
End Sub
Note that since this code writes all the distinct values to a single column in the OutSht-sheet, this will return an error if there are more than 1,048,576 distinct values in your dataset. In that case you would have to split the data to be filled into multiple output columns.
For your specific request to count, use the below in a formula like =COUNTA(GetUniques(LEFT("A1:A100000",FIND("_","A1:A100000")-1)) entered as an array formula with Ctrl+Shift+Enter.
It also accepts multiple ranges / values (e.g. GetUniques("A1:A10","B2:E4"))
Function GetUniques(ParamArray args())
Dim arg, ele, arr, i As Long
Dim c As Collection
Set c = New Collection
For Each arg In args
If TypeOf arg Is Range Then
If arg.Count = 1 Then
arr = array(arg.value)
Else
arr = arg.Value
End If
ElseIf VarType(arg) > vbArray Then
arr = arg
Else
arr = Array(arg)
End If
For Each ele In arr
On Error Resume Next
c.Add ele, VarType(ele) & "|" & CStr(ele)
On Error GoTo 0
Next ele
Next arg
If c.Count > 0 Then
ReDim arr(0 To c.Count - 1)
For i = 0 To UBound(arr)
arr(i) = c(i + 1)
Next i
Set c = Nothing
GetUniques = arr
End If
End Function
edit: added a performance optimisation for ranges (loads them at once into an array - much faster than enumerating through a range)
In R:
# sample data
df <- data.frame(x=1:1000000,
y=sample(1e6:(1e7-1),1e6,replace=T))
df$y <- paste0("A",df$y,"_",sample(1:10,1e6,replace=T))
# this does the work...
length(unique(sub("_[0-9]+","",df$y)))
# [1] 946442
# and it's fast...
system.time(length(unique(sub("_[0-9]+","",df$y))))
# user system elapsed
# 2.01 0.00 2.02
In excel 2010... in the next column add (if original data was in A:A add in B1)
= 1/COUNTIF(A:A,A1) and copy down col B to the bottom of your data. Depending on your PC it may chug away calculating for a long time, but it will work. Then copy col B & paste values over itself.
Then SUM col B

Resources