Data in Google sheet - google-sheets

I tried following formula for getting pcr data...
=importxml("https://niftyinvest.com/put-call-ratio/MARUTI?expiry=30JUN2022","//span[#class='white-text red darken-1']"
For different stocks the values are in 3 different colours red/blue/green depending on their values predefined.
How to get the common formula so that it can retrieve data in whatever colour plz ????
Other Data Links...
1- https://niftyinvest.com/put-call-ratio/indigo?expiry=30jun2022
2- https://niftyinvest.com/put-call-ratio/britannia?expiry=30jun2022
3- https://niftyinvest.com/put-call-ratio/MARUTI?expiry=30JUN2022

for Pcr:
=INDEX(IMPORTXML(A2,
"//span[#class='white-text red darken-1'] |
//span[#class='white-text light-blue lighten-1'] |
//span[#class='white-text green darken-1']"), 1)
for Intraday Pcr:
=INDEX(IMPORTXML(A2,
"//span[#class='white-text red darken-1'] |
//span[#class='white-text light-blue lighten-1'] |
//span[#class='white-text green darken-1']"), 2)

Try to use this generic xpath //h5//span[#class[contains(.,'white-text ')]]
=transpose(importxml(A2,"//h5//span[#class[contains(.,'white-text ')]]"))
edit: you can use queries to reorder columns and select by specific criteria.
=QUERY(IMPORTHTML("https://niftyinvest.com/put-call-ratio/indigo?expiry=30jun2022","table",1),"select Col1,Col2,Col3,Col4,Col5,Col6,Col7 where Col4>0.6 and Col7>0.7 and Col1 <> 'IOC' and Col1 <> 'POWERGRID ' ")

Related

Countifs with OR criterias and ranges

I have 5 Columns with color options. I need to COUNT how many of them are different of Black in all options. I mean, the momment they have something different from black in one of the 5 columns, they are consider color. If not, they are considered as black. I.e, if "Red" appears in one of the 5 colums, is COLOR. I have tried to add OR inside Countifs, and using querys but it does not work. The only way I found is using a new columns with if conditions but i need to embed that in the count or query formula (I cannot add any new column in the main datasheet).[ I cannot embed pics yet so there is an screenshot.]
=if(OR(if(AND(L2<>"Black";L2<>"");"COLOR";"")="COLOR";
if(AND(M2<>"Black";M2<>"");"COLOR";"")="COLOR";
if(AND(N2<>"Black";N2<>"");"COLOR";"")="COLOR";
if(AND(O2<>"Black";O2<>"");"COLOR";"")="COLOR";
if(AND(P2<>"Black";P2<>"");"COLOR";"")="COLOR");"COLOR";"BLACK")
I have used that formula as an interface and I can count how many of them are COLOR or BLACK. It works, but I need the result without adding any additional column.
I know that any satement after "comma" in countifs is an "AND" so any range and condition is limiting needing to acomplish all the conditions. I need they work as "ORs" but I did not found a way. I would need something like:
=countifs(AND(L:L,<>"Black"L:L<>"") *OR* AND(M:M,<>"Black"M:M<>"") *OR* AND(N:N,<>"Black"N:N<>"") ...
try:
=INDEX(IF(TRIM(FLATTEN(QUERY(TRANSPOSE(L2:P),,9^9)))="",,
IF(REGEXMATCH(FLATTEN(QUERY(TRANSPOSE(L2:P),,9^9)), "(?i)black"), "BLACK", "COLOR")))
or:
=INDEX(IF(LEN(L2:L&M2:M&N2:N&O2:O&P2:P), UPPER(MAP(L2:L, M2:M, N2:N, O2:O, P2:P,
LAMBDA(L,M,N,O,P, IFS(L="BLACK",L,M="BLACK",M,N="BLACK",N,O="BLACK",O,P="BLACK",P,TRUE,"COLOR")))), ))
update:
=INDEX(QUERY(IF(TRIM(FLATTEN(QUERY(TRANSPOSE(L2:P),,9^9)))="",,
IF(REGEXMATCH(FLATTEN(QUERY(TRANSPOSE(L2:P),,9^9)), "(?i)\bblack\b"), "BLACK", "COLOR")),
"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1)''"))
You could do it like this in I2 say
=countif(byrow(L2:P,lambda(r,countifs(r,"<>Black",r,"<>"))),">"&0)
and in J2
=countif(byrow(L2:P,lambda(r,counta(r))),">"&0)-I2
Add a sheet reference if you need the formula to be in a different sheet e.g.
=countif(byrow(Sheet6!L2:P,lambda(r,countifs(r,"<>Black",r,"<>"))),">0")
=countif(byrow(Sheet6!L2:P,lambda(r,counta(r))),">0")-A2
if in A2 and B2 of a separate sheet.
Note
If your database gets large and you get problems with Byrow/Lambda you can revert back to the more conventional Mmult:
=ArrayFormula(countif(mmult(n((L2:P<>"Black")*(L2:P<>"")),sequence(5,1,1,0)),">0"))
and
=ArrayFormula(countif(mmult(n(L2:P<>""),sequence(5,1,1,0)),">0"))-C2
assuming previous formula is in C2.
Use this formula
=ARRAYFORMULA(LAMBDA(u, {UNIQUE(u),COUNTIF(u, "="&UNIQUE(u))})
(LAMBDA(t, IFS(t=FALSE,"Color",t=TRUE,"Black"))
(REGEXMATCH(trim(LAMBDA(f, FILTER(f,f<>""))(FLATTEN(L2:P))), "(?i)black"))))
Demo

Splitting data separated by a | in a cell to new rows

I have a Spreadsheet (see link at the bottom of the page) that has 1 row and 3 columns.
I want to take the data contained and split it out, resulting in a row by row breakdown.
Is anyone aware of how this could be done using a formula? It would save me a bunch of time doing it manually!
DemoSheet - This shows what the input and the desired outputs are
EDIT:
The Input sheet shows the data as I have it, using metasyntactic variables as examples (real data will vary, but will always follow the same formatting).
For every email address in the email column, I need to do the following
Get the list of managers and members and have it output as per the Desired Output 1 sheet. So for each entry in ColA, a row entry for each of the data in B and C, as if they were concatenated, split by " | " and transposed vertically.
Repeat the above process but only for managers (as per the the Desired Output 2 sheet).
Is this what you need?
Output1:
=arrayformula({"Email","Members";
query(
array_constrain(
{
flatten(split(rept("|"&Input!A2:A,len(regexreplace(Input!B2:B&" | "&Input!C2:C,"[^\|]",))+1),"|")),
trim(flatten(split(Input!B2:B&"|"&Input!C2:C,"|")))
},
max(if(Input!B2:B<>"",len(regexreplace(Input!B2:B&" | "&Input!C2:C,"[^\|]",))+1,))*counta(Input!B2:B),2),
"where Col1 is not null",0)
})
Output2:
=arrayformula({"Email","Manager";
query(
array_constrain(
{
flatten(split(rept("|"&Input!A2:A,len(regexreplace(Input!B2:B,"[^\|]",))+1),"|")),
trim(flatten(split(Input!B2:B,"|")))
},
max(if(Input!B2:B<>"",len(regexreplace(Input!B2:B,"[^\|]",))+1,))*counta(Input!B2:B),2),
"where Col1 is not null",0)
})
You could optionally wrap unique() within the arrayformula if it is likely that you'll get duplicates in the dataset.

Can I filter out pivot table results that only have one row for a value in column A?

I created a pivot table in googlesheets, and it returns results that look like:
first | second | CountOf3
--------------------------
thing | value | 23
| newVal | 3
| cool | 34
that | value | 234
otherThing | cool | 4
| newVal | 345
And I want to filter out results with just one resulting row for the item in the first column.
So in this example, that would be the row: that | value | 234.
I would like the filter to remove that row, and leave the remaining rows. This is a pivot table in a 2nd sheet that updates when Sheet1 changes.
I have been trying all day, and have not been able to come up with a solution. I was hoping there would be some sort of filter, or spreadsheet formula to do this. I've tried multiple combinations of filters, but nothing seems to work - I'm starting to wonder if this is even possible.
It isn't pretty, but a brute force way is to have a check column beside your pivot table, with this formula on the first data row, ie beside "thing | value | 23".
It flags each row where the subsequent cell in column D is not blank. Then use a query (or filter) to list only the output rows you want. Note that you would hide the columns or rows with the actual (unfiltered) pivot output.
This is the simplest version, to see the logic:
=AND(LEN(D3),LEN(D4))
which results in a TRUE value for pivot chart rows that only have one value.
A more elegant version is an arrayformula, adds the header lable, and uses "Skip" as the flag for which rows to filter out.
={"Better Check";ARRAYFORMULA(IF(LEN(D3:D998)*LEN(D4:D999)*LEN(E3:E998),"Skip",))}
Note that this formula allows for a pivot table result effectively to the bottom of the sheet, but it does have a finite range, due to the constraint of checking two rows at once. It could be enhanced by using a COUNTA on the third data column to measure the exact length of the pivot table results and control the range dynamically, Like this:
={"Better Check";
ARRAYFORMULA( IF( LEN(INDIRECT("D3:D" & (COUNTA(F$3:F)+ROW(F$2)))) *
LEN(INDIRECT("D4:D" & (COUNTA(F$3:F)+1+ROW(F$2)))),
"Skip",))}
Let us know if this helps at all.

GAS - Concatenate Unique List + Sumifs

I am really struggling with this to come up with an easy way to do this in Google Sheets.
I need a unique list with filter.
Fairly straight forward:
Unique(Filter(UniqueRange,FilterRange,Criteria)
I need to sumifs that list. If i do it one row at a time, its fairly straight forward pointing one of my criterias to the result of #1 and copy it downward:
sumifs(SumRange, Criteria1Range, Criteria1, Criteria2Range, Criteria2))
What i am struggling with is, i do not know how far that unique list will go. So i do not know how far down to copy #2's formula. Its no big deal if i had unlimited rows but i need to output the results of the above to all show up in a single cell with a character between the results because i am trying to make all this fit in a "calendar" for a dashboard. Can this even be done?
Sample Data:
Apple | 2
Orange | 3
Red | 1
Green | 4
Orange | 5
Red | 2
Simple result i have now by letting the uniques list grow as needed on the left column and copying formula #2 downward on the right hand column:
Apple | 2
Orange | 8
Red | 3
Green | 4
My question again is, is there a way to have the below result all show up in a single cell and toss in a hyphen between the results?
Apple - 2
Orange - 8
Red - 3
Green - 4
EDIT:
Thank you all for the help.
#theMayer
You pointed me in the right direction and ended up solving my issue. Thank you!
#I'-'I
Helper columns will just not work for my needs.
What i ended up doing is modifying the solution a little. I modified it to have a " _ " between the data because the data had hyphens in it and it was confusing. As for the number formatting, ill just leave it out for now. Here is my final code along with an additional date filter in the select query:
ARRAYFORMULA(TRIM(CONCATENATE(QUERY({CHAR(10)&$G$3:$G,$F$3:$F,$A$3:$A},"select Col1,' _ ',sum(Col2) where Col3 = date '" & text(C3,"yyyy-MM-dd") & "' group by Col1 label sum(Col2) ''"))))
=ARRAYFORMULA(TRIM(CONCATENATE(QUERY({CHAR(10)&A1:A6,--("-"&B1:B6)},"select Col1,sum(Col2) group by Col1 label sum(Col2) ''"))))

Google Sheets - Query - Running Total below dynamic results

Testing Sheet:
Wondering if there is a witty way to add a Total to the last row +1 of
a Query result.
See Sheet 'Lookup' for a static example of what I am asking for.
I don't know if there is a way to have a hidden column that calculates
transposed only under the last row of a query, or if there is a smart
way to work Query for this answer.
All great answers. Each on very useful in its use case.
Макс Махров gets the answer with using a query statement.
Now I was not keen on having an extra sheet to hold the totals so I added a row at the top which I can simply hide and used this formula:
query({Orders!A:E;A1:E1},"select Col1, Col3, Col4 where Col2 = '"&C3&"' order by Col4",1)
Only problem I have is trying to figure out how to add TEXT to the bottom row, it seems to only want numerical input.
How do I fix this? What am I glitching?
Thanks !
Mars
The trick is to make second query and count totals for selected product.
Plan of actions:
add new sheet with query on it, something like this: =QUERY(Orders!A:E,"select B, 0, sum(D) where B like '"&Lookup!C2&"' Group by B",0)
Prepare arrayformula which combines data in Lookup sheet: = ArrayFormula({Importrange(1),Importrange(2)}) Note that number of columns must retain the same.
Edit query so it takes Col1, Col2, Col3... instead of A, B, C...
Make word 'total' visible instead of zero. Set number format: 0;0;total Set it for range B9:B on Lookup sheet
Make Conditional Formatting with formula =and($B4 =0,isnumber($B4)) for range A4:C on Lookup sheet.
That's seems have to complete the task.
Hope it Helps!
Your Example
Working example.
Here is one way:
Put TOTAL way down in row 1000
Select the range A3:C999. Select data > filter to create filters
Select C3, set the filter to hide all blanks
A second way is to limit the query result to show only the top 8 results:
Change your query to =query(Orders!A:E, "select A, C, D where B = '"&C2&"' order by D desc limit 8",1) It will reverse-order column D (largest first), and set row limit to 8.
Change the formula of your TOTAL to =sumif(Orders!B:B,C2,Orders!D:D)
Try this formula in the column adjacent to your query:
=ArrayFormula({$C$4:offset($C$4,count($C$4:$C),0,1,1);sum($C$4:offset($C$4,count($C$4:$C),0,1,1))})
It duplicates your column of values (I haven't figured out a way around that yet) and then adds a total to the bottom of that column, and changes dynamically with the range from your query.
Here's a working version.
Interesting challenge! It got the old grey matter turning... ;)
Thanks,
Ben

Resources