How to use AND in Google sheets? - google-sheets

I am currently having hard time creating a formula for my sheet to record values when the following conditions will be met:
I want to give 15% discount from the original price if a Person purchased their ticket online (Online = TRUE), and only applicable for customers who is atleast 50 years old, kindly list the new calculated discounted price in the Discounted Price Column
I have tried creating a formula but I got formula parse error.
Formula used : =IF((B2=TRUE AND D2>50),(E2*0.85),E2)
This is my sample data in the sheet and the expected output on Discounted Price column. Any help will be appreciated.
Person
Online
Physical Store
Age
Original Price
Discounted Price
A
TRUE
FALSE
67
1000
850
B
TRUE
FALSE
16
1000
1000
C
FALSE
TRUE
24
1000
1000
D
TRUE
FALSE
52
1000
850
E
FALSE
TRUE
60
1000
1000

Your formula is almost correct except for the AND operator, the syntax for using AND is as follows: AND(logical_expression1, [logical_expression2, ...]).
I have replicated your data and fix the formula. Please see formula and desired output below
Formula:
=IF(AND(B2=TRUE,D2>50),(E2*0.85),E2)
Data and Output:
References:
https://support.google.com/docs/answer/7014145
https://support.google.com/docs/answer/3093301?hl=en

use:
=INDEX(IF(B2:B="",,IF((B2:B=TRUE)*(D2:D>50), (E2:E*0.85), E2:E))

Related

RUNNING TOTAL with ARRAYFORMULA that can SUM and SUBTRACT if cell values changes

I'm trying to calculate the cumulative total in a column. It needs to sum or subtract if the value of (A) cell is set to buy or sell. here is an example:
A (task)
B (qtty)
C (total)
calculation explanation
buy
10
10
sum 10
buy
10
20
sum 10
sell
5
15
subtract 5
buy
20
35
sum 20
sell
10
25
subtract 10
I´m using the folowing formula:
={"Total", ARRAYFORMULA(IF(LEN(A2:A),IF(A2:A="buy",SUMIF(ROW(B2:B),"<="&ROW(B2:B),B2:B), "NEED CODE FOR SELL" ),))}
Is there another way to do the calc?
I don't want to use negative values to subtract, because the values are used in other formulas.
Thanks in advance.
try this:
=SCAN(0,A2:A,LAMBDA(ac,cv,if(cv="",,ac + ifs(cv="buy",OFFSET(cv,,1),cv="sell",-OFFSET(cv,,1)))))
You can use SCAN:
=ArrayFormula(IFNA(SCAN(,A2:A&B2:B,LAMBDA(tot,cur,tot+REGEXEXTRACT(cur,"\d+")*IF(REGEXMATCH(cur,"(?i)buy"),1,-1)))))
Or:
=SCAN(,B2:B,LAMBDA(tot,cur,IF(cur,tot+IF(INDEX(A2:A,ROW(cur)-1)="buy",cur,-cur),)))
It's also possible to do it with Sumif:
=ArrayFormula(if(A2:A="",,
sumif(row(A2:A)*if(A2:A="buy",1,9^9),"<="&row(A2:A),B2:B)-
sumif(row(A2:A)*if(A2:A="sell",1,9^9),"<="&row(A2:A),B2:B)))
Of course, there's no particular reason for doing it this way now that you have lambdas. I'm including it for old time's sake as I was one of the first people to use Sumif in this way.

How to find balance stock cost of SKU based on received date?

Above is an example of data in Google Sheets. The green table is to keep track my purchases and the red table is to show the inventory summary.
I am stuck at creating a formula to calculate the Balance Stock Cost.
The Balance Stock Cost should only sum up the cost of unsold quantity from latest received date.
For example, for SKU A, the stock balance is 31 units. The purchases of SKU A sorted by received date will become like this...
Since the stock balance is 31, the formula should sum up the purchases on 10-Jan (5 units x 6), 7-Jan (25 units x 4) and 5-Jan (1 unit x 5.5) which brings the total to 135.5.
Please help me to come up a formula to calculate the Balance Stock Cost based on above requirements. Thanks.
You can try:
=MAP(A18:A,D18:D,LAMBDA(ax,dx,IF(ax="",,SUM(QUERY(SORT(ARRAYFORMULA(split(flatten(A2:A15&"|"&B2:B15&"|"&D2:D15&"|"&MAP(C2:C15,LAMBDA(cx,TRANSPOSE(SEQUENCE(cx,1,1,0))))),"|",0,0)),1,1,2,0),"Select Col3 Where Col4 is NOT NULL AND Col1='"&ax&"' LIMIT "&dx)))))

Conditional Sum to a Specific Total in a Single Column

I am trying to weighted average of available stock ie 888 Items. We operate FIFO so that means I need to start sum from recent date backwards. How do i only select those cells that sum up to available stock balance (888) then sumproduct with the price?
Date Items Recieved Price
9/1/2022 254 $25.00
8/25/2022 242 $25.00
8/18/2022 230 $65.00
8/11/2022 218 $77.00
8/4/2022 206 $45.00
7/28/2022 194 $77.00
7/21/2022 182 $89.00
7/14/2022 737 $74.00
7/7/2022 1292 $86.00
6/30/2022 1847 $87.00
Query, Arrayformula & SUMproduct
You tagged both Excel and Google sheets. They're different. In Excel (Office 365) you can do this using:
=LET(stock,888,
data,B2:C11,
items,INDEX(data,,1),
price,INDEX(data,,2),
cumulative,SCAN(0,items,LAMBDA(a,b,a+b)),
r,XMATCH(stock,cumulative,1),
correction,INDEX(items,r)+stock-INDEX(cumulative,r),
SUMPRODUCT(
IFERROR(
VSTACK(
TAKE(items,r-1),
correction),
correction),
TAKE(price,r)))
stock is the number to sum up to.
data is the range containing both the items and prices.
SCAN is used to get the cumulative sum of all items row-by-row.
XMATCH is used to find the row (r) in the cumulative sum where the value is greater than or equal to the stock value.
r is used to correct the items in that row to the value required to get the cumulative sum up to row r equal to the stock value. (Item in row r + stock - cumulative sum in row r).
I than take the rows before r of the items and add (stack) the correction items value calculated and use that in a SUMPRODUCT with the prices up to r.
If r is the first row it'll throw an error at the TAKE(items,r-1)-part, if so IFERROR makes sure the corrected value is used without stacking it on previous items values.
Edit: since you mentioned FIFO you'd probably be interested to calculate from the bottom up. In this case you could use:
=LET(stock,888,
data,SORT(A2:C11,1,1),
items,INDEX(data,,2),
price,INDEX(data,,3),
cumulative,SCAN(0,items,LAMBDA(a,b,a+b)),
r,XMATCH(stock,cumulative,1),
correction,INDEX(items,r)+stock-INDEX(cumulative,r),
SUMPRODUCT(
IFERROR(
VSTACK(
TAKE(items,r-1),
correction),
correction),
TAKE(price,r)))
It works the same, it just uses an extra column for the data, so it could sort from old (first in) to new.
And it's unclear if you wanted this SUMPRODUCT or the average of it, but that's simply adding /stock to the last argument of LET

How to apply array formula taking data from another table?

We have two tables in Google Sheets.
First:
Date
Amount
Currency
Worth
01.01.2021
100
USD
373
02.01.2021
100
EUR
451
03.01.2021
100
PLN
100
04.01.2021
100
USD
373
05.01.2021
100
USD
372
Second:
Date
PLN
EUR
USD
01.01.2021
1
4,50
3,73
02.01.2021
1
4,51
3,75
03.01.2021
1
4,50
3,74
04.01.2021
1
4,48
3,73
05.01.2021
1
4,49
3,72
I tried find array formula for first table, column Worth. Formula should take proper value from second table (based on two columns from table one - Date and Currency) and multiply that values by worth in column Amount. I really want to use array formula. Is it possible?
Use VLOOKUP to find the correct date row and MATCH to find which column the value is in:
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A,I2:L,MATCH(C2:C,I1:L1,0))*B2:B))
Option 01: Getting the result with one cell one formula.
Paste this in B3 "Amount" column in the first table, take a look at this Sheet.
=ArrayFormula(IF(ArrayFormula(IF(A3:A="",,VLOOKUP(A3:A,G3:J,ArrayFormula(IF(D3:D="",,MATCH(D3:D,$H$2:$J$2,0)+1)),0)))="",,ArrayFormula(IF(A3:A="",,VLOOKUP(A3:A,G3:J,ArrayFormula(IF(D3:D="",,MATCH(D3:D,$H$2:$J$2,0)+1)),0)))*E3:E))
Explanation ...
1 - MATCH(D3:D,$H$2:$J$2,0) To get the index you want to VLOOKUP the "Currency" column from the second table with, we need that in the next step.
2 - VLOOKUP the "date" found in First table A3:A from Range in the second table G3:J, with Index set to MATCH(D3:D,$H$2:$J$2,0), and [is_sorted] set to 0
3 - till now we have the value of the exchange rate if we can call it that for each Currency chosen in the first Table, we need to multiply it by Worth to get Amount
ArrayFormula(IF(A3:A="",,VLOOKUP(A3:A,G3:J,ArrayFormula(IF(D3:D="",,MATCH(D3:D,$H$2:$J$2,0)+1)),0)))*E3:E is structured like this Exchange rate * Amount note that E3:E is the Amount, and this IF(A3:A="",, to calculate only when A3:A range is not blank.
4 - ArrayFormula and a IF is needed to be wrapped around like this ArrayFormula(IF(Range=Empty,Do nothing,formula)
Range:
ArrayFormula(IF(A3:A="",,VLOOKUP(A3:A,G3:J,ArrayFormula(IF(D3:D="",,MATCH(D3:D,$H$2:$J$2,0)+1)),0)))
Empty
""
Do nothing :
,,
Formula:
ArrayFormula(IF(A3:A="",,VLOOKUP(A3:A,G3:J,ArrayFormula(IF(D3:D="",,MATCH(D3:D,$H$2:$J$2,0)+1)),0)))*E3:E
Option 02: Getting the result with intermediate steps.
Same as option 01 but in seprate columns take a look at this Sheet.

Value (currency) not being imported correctly with importxml in Google Sheets (due to a dot when price is higher than 999 euros)

I have a question regarding the importxml function. At this moment I'm fetching the price of some of our travel pages to a Google Sheets list so we can track the price development of our packages (travel product, so prices are chaning all the time).
Now I'm facing the next problem. When I'm importing the price of a package that has a price higher than 999 euros (for example 1060 euros) the value I get in my Google Sheets is 1.06 (due to the use of a dot in between the 1 and the 0 (1.060 euros).
Is there a possiblility to import the full price in Google Sheets? Without the dot seperating the numbers?
Formula I use: =importxml(A2, "/html/body/div[7]/div[1]/section[1]/div[2]/div[1]/div/div[1]/div[1]/div/span[3]/span"))
Result I get:
€1.09
Result I should get:
€1090
Example sheet: https://docs.google.com/spreadsheets/d/1uzpLEhpjpVcZI8QTLx_Pz4EFL0YJMPHJU6WR1CVbZ-w/edit?usp=sharing
try:
=SUBSTITUTE(TO_TEXT(IMPORTXML(A2, "/html/body/div[7]/div[1]/section[1]/div[2]/div[1]/div/div[1]/div[1]/div/span[3]/span"))), ".", )

Resources