Coldfusion loop query results and update using dynamic column names - coldfusion

In the database I have 5 columns plus the id:
id, m1, m2, m3, m4, m5
I'm selecting from one table that may have from 1 to 5 rows based on the id:
row,id,value
1, 1, 'A'
2, 1, 'B'
3, 1, 'C'
etc
What I need to do is update columns m1, m2 and m3 in the user table for user ID 1 with values A, B and C. I've started down the path of something like this but am hitting serious Monday brain on a Tuesday.
i=1;
cfloop (query=q) {
field = 'm' & i;
temp = invoke(myCFC,"updateUser",{ userid=q.id, field = q.value });
i++;
};
Where 'field' would be m1, m2, m3. One way would be to use a switch/case on the recordcount and have 5 different invokes, but not sure if there would be a better way programatically?
[edit] this works but may not be the best way - I have cases for 1-5:
switch(qryM.recordCount) {
case "1":
temp = invoke(userCFC,"updateUser", {
id = qryM.rsm_userid,
m1_c = qryM["rsm_c"][1],
m1_m = qryM["rsm_m"][1]
});
break;
case "2":
temp = invoke(userCFC,"updateUser", {
id = qryM.rsm_userid,
m1_c = qryM["rsm_c"][1],
m1_m = qryM["rsm_m"][1],
m2_c = qryM["rsm_c"][2],
m2_m = qryM["rsm_m"][2]
});
break;

If there really can only ever be a maximum of five rows and the target column always is "m" + the current row number, then this would work:
for (row in q) {
myCFC.updateUser(userid:row.id, field:'m#q.CurrentRow#');
}

Related

Google script insert value in a column if another column has a specific value

as basic as this may sound I am having difficulty writing this. I have two columns with checkboxes in a sheet(main) and I want to be able to checkbox(true) column 'O' if column 'm' has a checkmark after I am done with the sheet(macro button).
Thanks for any input.
If M is true set O to true
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const [hA,...vs] = sh.getDataRange().getValues();
vs.forEach((r,i) => {
if(r[12] == "TRUE") {
sh.getRange(i + 2, 15).setValue("TRUE");
}
})
}

Fetch for value and convert it to another according to condition

SHEET A - origin
Column A = ID
Column B = can contain "Accepted"(+ more text), "Rejected"(+more text), "Partially Accepted"(+ more text)
SHEET B - destination
Column A = Column A from Sheet A
Column B = If Column B from Sheet A = "Accepted*" return 1, "Rejected*" return 2, "Partially*" return 3, else return 4
WHAT I've tried so far:
a) Works but I can't make it into an array
=IF(COUNTIF(SHEETA!A2,"*Rejected*"),2,IF(COUNTIF(SHEETA!A2,"*Partially*"),3,IF(COUNTIF(SHEETA!A2,"Accepted*"),1,4)))
b) Been trying to make it work (simplified version) but it's not working
=if((VLOOKUP(A2,A2:B2,2,FALSE))="Rejected*","2","1")
Can anyone give me a hand?
Thank you in advance
try in row 2:
=INDEX(IFNA(VLOOKUP(A2:A, {SheetA!A2:A, IFNA(CHOOSE(MATCH(
REGEXEXTRACT(SheetA!B2:B, "(?i)accepted|rejected|partially accepted"),
{"accepted", "rejected", "partially accepted"}, ), 1, 2, 3), 4)}, 2, )))

Filter data using IF Statement in Tableau

I have a data source in tableau that looks something similar to this:
SKU Backup_Storage
A 5
A 1
B 2
B 3
C 1
D 0
I'd like to create a calculated field in tableau that performs a SUM calculation IF the SKU column contains the string 'A' or 'D' , and to perform an AVERAGE calculation if the SKU column contains the letters 'C' or 'B'
This is what I am doing:
IF CONTAINS(ATTR([SKU]),'A') or
CONTAINS(ATTR([SKU]),'D')
THEN SUM([Backup_Storage])
ELSEIF CONTAINS(ATTR([SKU]),'B') or
CONTAINS(ATTR([SKU]),'C')
THEN AVG([Backup_Storage])
END
UPDATE - desired output would be:
SKU BACKUP
A, D 6 (This is the SUM OF A and D)
B, C 2 (This is the AVG of B and C)
The calculation above shows as valid, however, I see NULLS in my data source table.
Any suggestion is appreciated.
I have named the calculated field:
SKU_FILTER_CALCULATION
Basically, IF THEN ELSE condition works when one test that is either TRUE/FALSE. Your specified condition is not a proper use case of IF THEN ELSE because SKUs can take all possible values. See it like this..
your data
SKU Backup_Storage
A 5
A 1
B 2
B 3
C 1
D 0
Let's name your calc field as CF, then CF will take value A in first row and will output SUM(5) = 5. For second row it will output sum(1) = 1, for third and onward rows it will output as avg(2) = 2, avg(3) = 3, avg(1) and sum(0) respectively. all these values just equals [Backup_storage] only and I'm sure that this you're not trying to get.
If instead you are trying to get sum(5,1,0) + avg(2,3,1) (obviously i have assumed + here) which equals 8 i.e. one single value for whole dataset, please proceed with this calculated field..
SUM(IF CONTAINS([SKU], 'A') OR CONTAINS([SKU], 'D')
THEN [Backup storage] END)
+
AVG(IF CONTAINS([SKU], 'B') OR CONTAINS([SKU], 'C')
THEN [Backup storage] END)
This will return an 8 when put to view
Needless to say, if you want any other operator instead of + you have to change that in CF accordingly
As per your edited post, I suggest a different methodology. Create diff groups where you want to perform different aggregations
Step-1 Create groups on SKU field. I have named this group as SKUG
Step-2 create a calculated field CF as
SUM(ZN(IF CONTAINS([SKU], 'A') OR CONTAINS([SKU], 'D')
THEN [Backup storage] END))
+
AVG(ZN(IF CONTAINS([SKU], 'B') OR CONTAINS([SKU], 'C')
THEN [Backup storage] END))
Step-3 get your desired view
Good Luck

Type conversion - two strings to int on power bi

On Power BI, how would I convert any row that is "Processed" OR "New" in my Status column, to be represented by a 1 in the new column (using DAX)?
Thanks!
You should be able to use an IF() function in a new calculated column.
NewCol = IF(Table1[Status] = "Processed" || Table1[Status] = "New", 1, 0)
You can also use the IN syntax instead of having multiple conditionals.
NewCol = IF(Table1[Status] IN {"Processed", "New"}, 1, 0)

Database table not being updated

I have been struggling with this piece of code. Everything is being updated except my items table in the database. Need the sales part to be plus 1 each time someone makes a purchase.
$setQuery = '';
if($extended) {
$setQuery = " `status` = 'extended_buy', ";
}
$mysql->query("
UPDATE `items`
SET `sales` = `sales` + 1,
$setQuery
`earning` = `earning` + '".sql_quote($price)."'
WHERE `id` = '".intval($item['id'])."'
");
return true;
}
You will need to use Subquery for the same
It would be somewhat like this
SET sales = (From items where WHERE id = '".intval($item['id']) + 1,
You will need to pull its value and then Add it.
In other case you can pick it in variable and update it.