Google Sheets Query With Multiple IF Statements - if-statement

I am trying to run a some nested IF statements with a query (with an import range to another sheet). Such that, if cell B2 is 'ABC', it will query the ABC sheet and return the value of a cell on that sheet. Same for the other two sheets, 'XYZ' and '123', all as one long formula. Here's what I have thus far, any help would be appreciated, thank you!
=IF(B2 = "ABC", (query({IMPORTRANGE("[ABC URL HERE]", "This Sheet!A2:H")}, "Select Col9 where Col5 = '"&$D2&"' LIMIT 1",0,IF(B2 = "XYZ", (query({IMPORTRANGE("[XYZ URL HERE]", "That Sheet Sheet!A2:H")}, "Select Col9 where Col5 = '"&$D2&"' LIMIT 1",0,)IF(B2 = "123", (query({IMPORTRANGE("[123 URL HERE]", "Their Sheet Sheet!A2:H")}, "Select Col9 where Col5 = '"&$D2&"' LIMIT 1",0,)))))
Jerome

try:
=IF(B2 = "ABC", QUERY({IMPORTRANGE("[ABC URL HERE]", "This Sheet!A2:H")},
"select Col9 where Col5 = '"&$D2&"' limit 1", 0),
IF(B2 = "XYZ", QUERY({IMPORTRANGE("[XYZ URL HERE]", "That Sheet Sheet!A2:H")},
"select Col9 where Col5 = '"&$D2&"' limit 1", 0),
IF(B2 = "123", QUERY({IMPORTRANGE("[123 URL HERE]", "Their Sheet Sheet!A2:H")},
"select Col9 where Col5 = '"&$D2&"' limit 1", 0), )))

Related

Azure data explorer kusto regex

I am trying to grab a substring from a queryText column. The queryText column is a SQL query statement. And my goal is to parse and extract specific patterns into a new column called TableName.
parse kind=regex queryText with "[Ff][Rr][Oo][Mm]" TableName
Above is my current Regex statement. It returns all characters after "FROM" or "from". I would like to only grab characters after "FROM" and before the first whitespace or newline. Any idea on what i have to add to the regex expression to do this?
you could use the extract() function.
for example (using the i flag for case-insensitivity):
datatable(input:string)
[
"select * FROM MyTable\n where X > 1",
"SELECT A,B,C from MyTable",
"select COUNT(*) from MyTable GROUP BY X",
"select * FROM MyTable",
"select * from [a].[b]",
]
| extend output = extract(#"(?i)from\s+([^\s]+)\s*", 1, input)
input
output
select * from [a].[b]
[a].[b]
select * FROM MyTable where X > 1
MyTable
SELECT A,B,C from MyTable
MyTable
select COUNT(*) from MyTable GROUP BY X
MyTable
select * FROM MyTable
MyTable

Power BI NATURALINNERJOIN override active relationship with USERELATIONSHIP

I have the following tables with 3 relationships (1 active, 2 inactive):
Table 1 (Relationship: Column 1)
Table 2 (Relationship: Column 1, Column 2, Column 3)
The active relationship is on Column 1 of Table 1 and Table 2. I join these tables like this:
NATURALINNERJOIN(SELECTCOLUMNS('Table 1', "Column 1", 'Table 1'.[Column 1] & ""), SELECTCOLUMNS('Table 2', "Column 1", 'Table 2'.[Column 1] & ""))
Works great!
I want to do another NATURALINNERJOIN but this time on 'Table 1'.[Column 1] and 'Table 2'.[Column 2]. How do I use USERELATIONSHIP to override the active relationship and use the inactive ones?
You don't need to override the relationship if you're creating a calculated table. Just specify your desired columns. e.g.
Table3 =
NATURALINNERJOIN(SELECTCOLUMNS('Table1', "Column 1", 'Table1'[Column1] & ""), SELECTCOLUMNS('Table2', "Column 1", 'Table2'[Column3] & ""))

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)

How to re-use the filters used in CALCULATE function when adding columns

I'm trying to add some columns in DAX using CALCULATE functions, just like this:
ADDCOLUMNS(
Summarize(
CALCULATETABLE(
'MY TABLE'
'MY TABLE'[Year] = 2019,
'MY TABLE'[Month] = January,
'MY TABLE'[Flag] = "N",
),
'MY TABLE'[Column 1],
'MY TABLE'[Column 2],
'MY TABLE'[Column 3],
"Calculation", 'MY TABLE'[My Measure],
),
"My Calculation With Filters 1", CALCULATE('MY TABLE'[My Measure 1],[Status]="Open",[Flag]="N"),
"My Calculation With Filters 2", CALCULATE('MY TABLE'[My Measure 2],[Status]="Open",[Flag]="N"), --Same filters as above
"My Calculation With Filters 3", CALCULATE('MY TABLE'[My Measure 3],[Status]="Open",[Flag]="N"), --Same filters as above
"My Calculation With Filters 4", CALCULATE('MY TABLE'[My Measure 4],[Status]="Open",[Flag]="N"), --Same filters as above
"My Calculation With Filters 5", CALCULATE('MY TABLE'[My Measure 5],[Status]="Open",[Flag]="N"), --Same filters as above
)
I wonder which is the best practice when the filters are the same. I mean, is there a way to avoid adding the same filters to each CALCULATE function? I would like to be able to share the filters so that I don't duplicate code.
Thanks!
The CALCULATE function allows a table to be passed as a filter, so you can define this once as a VAR and reuse it.
CalculatedTable =
VAR FilteredTable =
FILTER (
'MY TABLE',
'MY TABLE'[Status] = "Open" && 'MY TABLE'[Flag] = "N"
)
VAR Summary =
SUMMARIZE (
CALCULATETABLE (
'MY TABLE',
'MY TABLE'[Year] = 2019,
'MY TABLE'[Month] = January,
'MY TABLE'[Flag] = "N"
),
'MY TABLE'[Column 1],
'MY TABLE'[Column 2],
'MY TABLE'[Column 3],
"Calculation", 'MY TABLE'[My Measure]
)
RETURN
ADDCOLUMNS (
Summary,
"My Calculation With Filters 1", CALCULATE ( [My Measure 1], FilteredTable ),
"My Calculation With Filters 2", CALCULATE ( [My Measure 2], FilteredTable ),
"My Calculation With Filters 3", CALCULATE ( [My Measure 3], FilteredTable ),
"My Calculation With Filters 4", CALCULATE ( [My Measure 4], FilteredTable ),
"My Calculation With Filters 5", CALCULATE ( [My Measure 5], FilteredTable )
)

How to group and add similar items with a unit of measurement

I have the following spreadsheets. I need to compact the contents of this spreadsheet. I was wondering if it is possible to group similar items such as 1952 and add the numbers in the next column and output something along 5m / 2w or similar? 1951 would be compacted to 9w. This data is constantly changing and new reference numbers are added often.
Sheets doesn't do well adding numbers when they're right next to letters, so you'll need to split those cells into a number column and letter column using left() and right(). Put the number from left() inside the value() function as well so Sheets knows it's a number.
Example sheet
Once you have the helper columns made, you can use query() to consolidate and sum up your values. Query language can get tricky so I recommend the reference page. Once you have the sums and letters spit out, you can concatenate them in another column (J for me).
=ARRAYFORMULA(SUBSTITUTE(TRIM(SPLIT(TRANSPOSE(QUERY(TRANSPOSE({
SORT(UNIQUE(INDIRECT("B2:B"&COUNTA(B2:B)+1)&"♦")),IF(ISNUMBER(
QUERY(QUERY(TO_TEXT(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(IFERROR({
B2:B&"♦", REGEXEXTRACT(C2:C, "\d+")*1, REGEXEXTRACT(C2:C, "\d+(.+)")}),
"select Col1,sum(Col2),Col3
where Col3 is not null
group by Col1,Col3
label sum(Col2)''", 0)),,999^99)), "♦")),
"select count(Col1)
group by Col1
pivot Col2", 0), "offset 1", 0)), SUBSTITUTE(
QUERY(QUERY(TO_TEXT(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(IFERROR({
B2:B&"♦", REGEXEXTRACT(C2:C, "\d+")*1, REGEXEXTRACT(C2:C, "\d+(.+)")}),
"select Col1,sum(Col2),Col3
where Col3 is not null
group by Col1,Col3
label sum(Col2)''", 0)),,999^99)), "♦")),
"select count(Col1)
group by Col1
pivot Col2", 0), "limit 0", 1), " ", ), )}),,999^99)), "♦")), " ", " / "))
spreadsheet demo