Nesting sumif in an if statement - google-sheets-api

I am trying to create a formula that if the value of Release column in a table is "Demo", where the formula would perform a sumif to return result. I have the sumif working, but can't seem to include the additional condition. I thought it would be like this: =if ('Release'!G:G="Demo",(sumif('User Stories'!G:G, A2,'User Stories'!F:F),"NA"))
Tables -
Source Table:
Formula Table - I want the value of the sumif shown ONLY where corresponding Release column of Source Table is = "Demo"

Found it!
=sumif('User Stories'!G:G, A3,'User Stories'!F:F)

Related

How to find the least amount of LIKE patterns to satisfy a list of strings

So lets say I have this list of strings in an Excel file:
33000
33100
33010
33110
45050
45150
45250
45350
45360
45370
55360
55370
And I've got a SQL table that has this list of strings and more and I want to make a SELECT statement that searches only for this list of strings.
I could make a brute force statement like SELECT * FROM Table WHERE field = '33100' OR field = '33010' .... However I could make the WHERE list smaller by using LIKE statements.
I'm trying to find a way to make the number of LIKE statements as small as possible so I need to generate the least amount of SQL patterns to identify the whole list. For the list above, the least amount of SQL patterns would be this:
33[01][01]0
45[0123]50
[45]53[67]0
How could I generate a list of patterns like this dynamically where the input is the list of strings?
An alternative approach might be more "elegant", but it will not be faster. Your strings start with different characters, so the first part of a like pattern would be a wildcard or character range -- effectively precluding the use of an index.
A simple in expression, on the other hand, can use an index:
where col in ('33100', '33010', '33110', '45050', ...)
Okay, let's say you have this data in Excel which starts from cell A2
In cell C1 write this code: create table ##TEMP(STRS varchar(20))
In cell C2 write this code: ="insert into ##TEMP"&" values"&" ('"&A2&"' )"&","
In cell C3 write this code: =" ('"&A3&"')"&","
Now Ctrl+C formula in cell C3 and paste it in range C4-C13
Now you get Excel like this
Copy this code in range C1-C13, open SQL management studio paste it, delete last comma (in this case in cell C13 there is comma at the end you have to delete it for success SQL run) and run, now in you have ##temp table.
INNER JOIN it with your table like
SELECT * FROM MYTABLE M INNER JOIN ##TEMP AS T ON T.STRS = M.COLUMN_NAME_STR
And you should get data which you need, hope it helps.

PowerQuery - Using a cell in a table as part of the code in a query (dynamically or not)

I am trying to use a cell as a parameter in Excel powerquery. The query works without this, but I have to manually input the values, which I need to constantly change them in the query in other to get the results that I want.
Query (Advanced Editor):
let
Criteria01 = Excel.CurrentWorkbook(){[Name="Servers"]}[Content][ServerSearch]{0},
Criteria02 = Excel.CurrentWorkbook(){[Name="Servers"]}[Content][ServerSearch]{1},
Criteria03 = Excel.CurrentWorkbook(){[Name="Servers"]}[Content][ServerSearch]{2},
Source = Sql.Database("SERVERNAMEHERE", "DATABASENAMEHERE", [Query="SELECT DISTINCT [...........] AND (TABLEPREF.COLUMNHERE like '%MANUALVALUE01%' OR#(lf)TABLEPREF.COLUMNHERE like '%MANUALVALUE02%' OR#(lf)TABLEPREF.COLUMNHERE like '%MANUALVALUE03%' OR#(lf)TABLEPREF.COLUMNHERE like Criteria01)#(lf)#(lf)#(lf)order by 1 asc"])
in
Source
"Servers" is the table name and "ServerSearch" is the column header. If I check the step for Criteria01/etc it will show me the correct value of that table that I need to use.
Original query done in Sql-Server. I have no problems when running the query with only LIKE '%MANUALVALUES%' lines.
My main goal is to automatically get N values of "MANUALVALUES" from a table in a sheet, which will be used as an input for comparing WHERE TABLEPREF.COLUMNHERE like '%VALUEHERE%'. I must use this and I can't get the whole table/database because there are way too many results besides the ones that I want.
However for test purposes at this moment, I am trying to use only 1-3 values, the first 3 of this table (Criteria{0}{1}{2} in the query above). However, if I try to do something like TABLEPREF.COLUMNHERE like Criteria01 I get the following error:
DataSource.Error: Microsoft SQL: Invalid column name 'Criteria01'.
Details:
DataSourceKind=SQL
DataSourcePath=dalsql390;itdw
Message=Invalid column name 'Criteria01'.
Number=207
Class=16
So my questions are:
I am getting the table cell value by the right way? Meaning:
Excel.CurrentWorkbook(){[Name="Servers"]}[Content][ServerSearch]{0}.
How do I refer this value in my query? Since the way that I wrote
that query bought me that error.
Also please note that if change TABLEPREF.COLUMNHERE like
Criteria01 to CHG1.CI_Name like "Criteria01" I get the
following error:
Expression.SyntaxError: Token Comma expected.
After fixed 1 and 2, how can I use this dynamically? For
example, instead of getting values of index 1 2 3, what if I want to
use a whole table? I know that using
Excel.CurrentWorkbook(){[Name="Servers"]}[Content] will bring me the whole table of values (1 column, unknown number of rows), but
how do I use this table content 1 by 1 in my query?
That will get the value, but you can't refer to steps inside of text values by putting the step name inside of it.
You have a couple options for doing this dynamically.
Use Value.NativeQuery to create a parameterized query where you can pass in other values as parameters. For example, Value.NativeQuery(Sql.Database("SERVERNAMEHERE", "DATABASENAMEHERE"), "select #a, #b", [a = 1, b = "x"]) will return the table [1, x]. You can put in the step name in the record value to pass that it (e.g. replace "x" with Criteria01).
Add the text values directly in the query field, e.g. [Query = "select " & Criteria01 ";"]. This is highly discouraged since this can lead to SQL injection issues.
For the third question, it depends what you want to do with the list of values. At some point you will likely need List.Accumulate to turn them all into a single text value which can be placed in the query value, and maybe to turn them into a record to place into the parameters value.

DAX - IF lookup value empty return column value

I'm using Power Pivot 2013, I have two table.
(fact)Table A: Name and Value
(dim) Table B: Name and Type
When selecting pivottable, I want to show Type and Value but if Name.TableA can't be found in Name.TableB, instead of returning (blank) i want pivottable to return Name.TableA. I have tried VALUES() IF(VALUES) with no success.
Thank you in advance.
i think that you have created a relation between name.tableA and name.TableB. You can Create a calculated column on TABLEA using related(Type.TableB).
At this point if you have a relation between the table in new column on TableA you have some row blank and some row with the TYpe.TableB. If it's working change the column formula with
=if(ISBLANK(related('TableB'[Type]));'tableA'[name];related('TableB'[Type]))
If there isn't a connection between table you should change related with Lookup.
I use a named variable inside the measure for that:
var mylookup = LOOKUPVALUE(
dim[name]
, dim[id]
, fact_table[id]
)
return IF( NOT ISBLANK (mylookup), mylookup, "UFO")
UFO value will be returned either:
if dim table contains NULL in the dim[name] field,
if there is no match for [id] in dim table.
See more DAX VAR defining named variables in the middle of the measure code

Include numbers in the first column once and transpose them as column headers

I am working with data from the hospital, and when I add the .csv extension to my text files they output in the following way:
It would be much easier to manage if there were a way to only include the numbers in the first column once, and also transpose them as column headers. And go through the first ten in the second column, add and transpose them underneath, then do the next ten. The final product looking like this:
I have tried transposing them manually, but since there are millions of files, the CSV's are quite extensive. I have looked for a way in Excel to do it, but I have found nothing.
Could someone help me with a macro for this?
An excel formula could be used, if the numbers are repeated exactly.
If the data is in Columns A & B, the following formula could be placed in C2:
=INDEX($B:$B,(ROW(C1)-1)*10+COLUMN(A$1))
And then copied to the right and down as far as needed.
You didn't mention whether the sequence of row numbers (1,90,100,120...) is always the same for each "row". From your sample, I will assume that the numbers repeat the same way, ad infinitum.
First, import the CSV into Microsoft Access. Let's assume your first column is called RowID, and your second is called Description. RowID is an integer, and Description is a memo field.
Add a third column, an Integer, and call it "Ord" (no quotes).
In Access's VBA editor add a new module with this GroupIncrement function:
Function GroupIncrement(ByVal sGroup)
Static Num, LastGrp
If (StrComp(LastGrp, Nz(sGroup, "")) <> 0) Then
Num = 0
End If
Num = Num + 1
GroupIncrement = Num
LastGrp = sGroup
End Function
Create a new query, replacing MyTable with the name of your Access table containing the CSV data:
UPDATE (SELECT * FROM [MyTable]
ORDER BY [RowID]) SET [Ord]=GroupIncrement([RowID])
Create a third query:
TRANSFORM First([Description])
SELECT [Ord]
FROM [MyTable]
GROUP BY [Ord]
PIVOT [RowID]
This should put the data into the format you want (with an extra column on the left, Ord).
In Access, highlight that query and choose External Data, and in the Export section, choose Excel. Export the query to Excel.
Open the file in Excel and delete the Ord column.

Using other than first row for headers in SQL

Is is possible to use other than first row for specifying headers in sql query in VBA
in my case the headers are in 4th row(usually they are in 1st row). Is there a way from which vba can detect it.
"SELECT [Sheet1$].ID FROM [Sheet1$] WHERE [Sheet1$].Number IS NULL"
So in simple terms it expects the ID and Number headers to be in row 1 but I have it in row 4, how can I do that. Thanks in advance.
If you have no data in the first three rows, I would have expected your original syntax to work. Perhaps you have data in those rows and just don't want to include it.
In that case you can specify a fixed range, like:
SELECT [Sheet1$A4:J212].ID FROM [Sheet1$A4:J212] WHERE [Sheet1$A4:J212].Number IS NULL
You can also specify a named range, which could be helpful if your list is dynamic:
SELECT myRange.ID FROM myRange WHERE myRange.Number IS NULL
To read more about these options see https://support.microsoft.com/en-us/kb/257819