query against concatenated array in Google Sheets - if-statement

I am trying to run а query against several ranges combined with {} like query({A2:C5, if(C1:C5='something',1,0)}, "select ..."). But I am getting an #REF! error with a message Function ARRAY_ROW parameter 2 has mismatched row size. Expected: 4. Actual: 1. What is the reason for that?
Here is a detailed example. Suppose I have a table like that:
id kind color
1 a green
2 a green
3 b green
4 c blue
I want to get a table showing number of cells with green for each kind:
kind color_count
a 2
b 1
c 0
Initially, I tried a query with the where clause for that:
=query(A2:C5, "select B, count(C) where C='green' group by B", -1)
But that does not include the row with zero values. So I tried to add an extra column with values 1 for the green color and 0 otherwise and use SUM over that without the where clause:
=query({A2:C5, if(C2:C5="green", 1, 0)}, "select B, sum(D) group by B", -1)
but that gives the above $REF!
As a workaround I added a column D to the table with the formula
=arrayformula(if(C2:C5="green", 1, 0))
Then the following query works and gives the desired result:
=query(A2:D5, "select B, sum(D) group by B", -1)
But is it possible to avoid this artificial column?

IF returns only 1 cell unless you use ARRAYFORMULA. so the error is on a spot because you have 4 cells on one side and 1 cell on the other side.
try:
=ARRAYFORMULA(QUERY({B:B, IF(C:C="green", 1, 0)},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
label sum(Col2)''", 0))

Related

Informatica : Count the number of rows based on sequence of number

I have table where source has 1 column, like below. for example, column name is A and I have set of records in the source.
A
1
1
1
2
2
3
I want to populate two columns in target, Say columns are A and B.
Column A in the Target has same values as in source and column B has count
A B
1 1
1 2
1 3
2 1
2 2
3 1
Can someone please explain how can i achieve this.
Thanks in advance
If source is a dbms like oracle, you can use source qualifier overwrite sql like below. Use row number and partition by to generate sequence for every A.
Select
A, row_number() over(partition by A order by A) as B
From mytable
If you're looking for infomatica only solution then this is how you can do it.
Sort the data by column A
Use ex transformation, create one in/out two var, and one out port.
We are going to compare first val with prev val, if they r same, add 1to the sequence else start from 1 again.
A in/out
v_B = iif (A=prev_A, v_B +1, 1)
prev_A=A
o_B =v_B
Link A and o_B to the target.

Create an indicator variable for a table relationship after an outer join

I am working with data from a data cube, meaning I cannot easily control the structures of my underlying data. My underlying data looks like the below. As a Cube datasource, each one of these columns also is its own table with define join relationships between one another. For example, to select the Group1 column on its own would look like 'Group1'[Group1].
Group 1
Group2
Group3
Desired Column
1
a
x
1
2
a
y
1
3
a
y
1
4
b
y
1
5
b
y
1
6
c
z
0
I am trying to create a variable that reflects what is shown in "Desired Column" so that I can include results for any value of Group2 that matches to the "y" value of Group3.
I am not very fluent with DAX, but my preliminary thoughts on an approach are to run a FILTER on Group3 for "y", select values of Group2, and then somehow use those Group2 values in a CONTAINS statement in a further FILTER. Here is my current, non-functional attempt:
Is_Y = CONTAINS(NATURALLEFTOUTERJOIN('Group2','Group3'), 'Group3'[Group3], "Y")

Big Query Select rows in between two values

I'm trying to select rows in between two values using big query.
Here the table is:
ID Group values
1 A 10I
1 B 20I
1 C 30I
1 D 40I
1 E 50I
1 F 60I
1 G 70I
1 H 80I
1 I 90I
Here I need to select rows from Group C to G.
The code i'm using is:
select * from data
where Group >= 'C' and Group <='G'
The above code gave no results.
Also i tried:
select * from data
where Group between 'C' and 'G'
This also returned no results.
Someone please provide a solution.
This is because "Group" is a reserved word (the GROUP BY): BQ expects you to group something and didn't understand that here it is the name of a column. To make BQ understand just as backslashes:
SELECT *
FROM data
WHERE `Group` BETWEEN "C" AND "G"

DAX Power BI Conditional filtering

Sample file
The column c of a table T can have the values 1, 2 or 3. I want to filter the table such as when the user selects value 1 nothing is filtered. When the selected value is 2 then show only the rows with the c column containing the value 2 or the value 3 not the value 1 and finally if the selected value is 3 then show only those rows containing 3 in the c column. The slider or a plain filter must be single selection not multi because otherwise it would violate one of the user's business rule.
Selected Show
1 all rows
2 rows with 2 or 3
3 only rows with 3
I tried to create columns and to create measures but I can't get anywhere. Any directions?
I agree that the disconnected table might be the best workaround here. I hope this solution will work with your actual file.
Step 1 - create a new disconnected table
FilterC = DISTINCT(T[c])
Step 2 - make sure that your slicer is sourced from the new table (FilterC)
Step 3 - create a measure that will return 1 for rows that we want to see, and 0 for rows that we want to hide:
mFilter =
var Filter_C = SELECTEDVALUE(FilterC[c])
var Row_C = SELECTEDVALUE(T[c])
return
SWITCH(
Filter_C,
"1", 1,
"2", IF(OR(Row_C = "2", Row_C = "3"), 1, 0),
"3", IF(Row_C = "3", 1, 0)
)
Step 4 - add this measure to the table, or even table filters will suffice.
Step 5 - start switching the values!
First you need to set it to a number column.
Second, you could write complex measures and use a disconnected table, but the best option is to use the correct slicer. There is a Greater than or Equal To option.

If True result based on a query output of 2 results

In column F I have the below formula which references the status of sheet 2 column G. The query formula produces 2 results populating cells F2 & F3. I want an "if" formula that if my query produces 2 cells containing yes, yes then say true but if it produces yes, no then produce false or no, no produce false.
This is the link to the sheet for reference. https://docs.google.com/spreadsheets/d/1C5xWlw9vMZMNhXprSCBZsNp0T9qMxvuooIP8I6JoI2Y/edit#gid=0
delete F2:F range and paste this in F2 cell:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A&E2:E, QUERY({Sheet2!A2:A&Sheet2!B2:B,
IF(Sheet2!G2:G="yes", 1, 0)},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
label sum(Col2)''"), 2, 0)=2))
if Sheet2 jobs are not always in pairs use:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A&E2:E, QUERY({Sheet2!A2:A&Sheet2!B2:B,
IF(Sheet2!G2:G="yes", 1, 0)},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
label sum(Col2)''"), 2, 0)=
IFNA(VLOOKUP(A2:A&E2:E, QUERY({Sheet2!A2:A&Sheet2!B2:B,
IF(Sheet2!G2:G="yes", 1, 0)},
"select Col1,count(Col2)
where Col1 is not null
group by Col1
label count(Col2)''"), 2, 0))))
I was able to come up with an answer with collaboration of some awesome people. The way I solved it, for now, is changing the where clause to focus on the "False" response only and wrapping it in an if and iferror to give me the True response
=iferror(ifna(if(query(IMPORTRANGE("https:/...","range"),"select Col8 where Col1 = '"&A7&"' and '"&E7&"' = Col2 and Col8 = 'No'",0)="No",FALSE,TRUE),TRUE),FALSE)