How do you escape a column name in Google visualisation API query language? - google-visualization

I have a Google sheet which generates an error in the following expression:
=query(Capacity!A5:FE135,"SELECT C,A WHERE "&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")&" = '"&C2&"' AND "&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0),4),"1","")&" = 1 ORDER BY C")
for a single, specific input value (a date) at D2.
Essentially, the purpose of the code is to find the column location of the date at D2 in a second sheet (Capacity) and put the values of that column in that sheet into column C in the current sheet, while also selecting only rows that match on a second column. When the date is set to a specific value, however, the expression will not evaluate.
On breaking this massive expression down into its component parts, it turns out the problem is caused by this expression:
=SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")
which, for the offending date, is returning column BY.
This means the expression being evaluated in Google Visualization API query language is:
SELECT C,A WHERE BY = '' AND BW = 1 ORDER BY C
but the query language sees BY as a reserved word, not a column, and barfs.
How can I escape the column name somehow to make it clear that it is to be considered a column name?

The way is to surround the offending portion with back-quotes (as I used to make text monospaced here):
=query(Capacity!A5:FE135,"SELECT C,A WHERE `"&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")&"` = '"&C2&"' AND `"&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0),4),"1","")&"` = 1 ORDER BY C")
so the query will look like
SELECT C,A WHERE `BY` = '' AND `BW` = 1 ORDER BY C
I assume this will help when the sheet grows so big that we're on column IF as well.

Related

Copy and Filter out selected fields from 2 tabs in the Same Gsheet to another Tab with common category Fraud = 'Yes'

Copy and Filter out selected fields from 2 tabs in the Same Gsheet to another Tab with Fraud = 'Yes'
I have 2 sets Tabs in the Same Gsheet with different information. I would like to copy them into another tab with Fraud ="Yes". I have an example with formulas in Combine Example from Stall A Example and Stall B Example with some help previously. When I tried to replicated into my actual data (Combine NSU and ACH ) set I can't do it.
Can someone please help and guide on this.
https://docs.google.com/spreadsheets/d/1N35wUB-a7hDHFTzdhajlaCTJf_Ce34Ql3Miwq51JCqY/edit?usp=sharing
Whenever it is Fraud = "Yes", it extracts the necessary information from NSU Tab and ACH Tab with has Fraud = Yes into Combine NSU and ACH Tab
#=SORT(
LAMBDA(DATA,
LAMBDA(DATE,TYPE,AMOUNT,FRAUD,ID,ERP
FILTER({TEXT(DATE,"dd/mm/yyyy"),TYPE,AMOUNT,FRAUD,ID,ERP},FRAUD="YES")
)(INDEX(DATA,,1),INDEX(DATA,,2),INDEX(DATA,,8),INDEX(DATA,,13),INDEX(DATA,,15),INDEX(DATA,,16))
)({'NSU'!A2:P27;{'ACH'!A2:A8, 'ACH'!B2:B8,'ACH'!N2:N8,'ACH'!L2:L8,'ACH'!F2:F8,'ACH'!H2:H8 }})
,1,TRUE)
Code:
Output:
Get all data of sheet NSU and ASH, and re-arrange their orders by QUERY.
Group them up with another QUERY to filter the data you want such as Col4 = 'Yes'.
All the formating and sort can also be done with query.
=ArrayFormula(
LAMBDA(NSU,ACH,
QUERY({NSU;ACH},
" WHERE Col1 IS NOT NULL "
&" AND Col4 = 'Yes' "
&" ORDER BY Col1 ASC"
&" LABEL Col1 'Date',Col2 'Type',Col3 'Amount',Col4 'Fraud',Col5 'ID',Col6 'ERP' "
&" FORMAT Col1 'yyyy-mm-dd' "
)
)(
LAMBDA(COLS,
QUERY({NSU!$A:$P},
" SELECT "&JOIN(",","Col"&COLS)
&" LABEL "&JOIN(",","Col"&COLS&" '"&REPT(" ",COLS)&"'"),1)
)({1,2,8,13,15,16}),
LAMBDA(COLS,
QUERY({ACH!$A:$N},
" SELECT "&JOIN(",","Col"&COLS)
&" LABEL "&JOIN(",","Col"&COLS&" '"&REPT(" ",COLS)&"'"),1)
)({1,2,14,12,6,8})
)
)
Well, in-case you really want to have the ability to add some input fields in-between the output QUERY.
The answer is you can't, but also you can.
Basically, you cannot insert anything in-between any kind of array outputs in google spreadsheets, the array-formula will return an error '#ref' mentioning that there are other values inside the output range which fail it from showing the output, BUT...
you can always get around this issue by simply seperate the output array by an other QUERY, such as:
This simple array in google sheet will reference range A1:C10 and place the data into where-ever you type this formula into, which output a 10 rows by 3 columns array.
={A1:C10}
We assumne that you put this in cell 'E1', that makes the output covers range 'E1:G10'.
If you what to have a column of fields which allow you to input new data in-between the output range, for example, you want to add a new column in F:F.
In that case, you can put 2 formula seperatly into cell 'E1' and cell 'G1', which contains the following formulas:
in cell 'E1':
=QUERY({A1:C10},"SELECT Col1")
in cell 'G1':
=QUERY({A1:C10},"SELECT Col2,Col3")
Since the output arrays are seperated, that makes the column(s) between the column E and column G a normal empty column, which allows you to input anything into the cells.
The beauty of QUERY function is that you can select any column(s) of a given reference array as an output.
In this case the output data will be seperated into 2 parts, one contains only the first column of the reference, the second one contains the rest of them.
It doesn't matter how many empty columns you insert between the 2 outputs since they are 2 outputs of one identical reference.
The draw back is, if your reference data are results of calculations, the sheet will have to do all those calculations two times, even if they gives the same results, which is why I say this can and will slow things down and are not very recommended.

if and search function on power BI token literal expected

I have a table called "food suppliers" with a column called "sites" that have multiple countries ending in ".com", ".es", ".co.uk".
I want to create a new column that separates these sites into their corresponding country names using the if and search function on power query.
so far in power query custom column I have:
Country = IF (SEARCH ("*.com", foodsuppliers[sites],,0) = 0, IF (SEARCH ("*.es", foodsuppliers[sites],, 0)= 0, "Spain","UK"),"USA")
But I am getting a "token literal expected" under the first = sign in "IF (SEARCH ("*.com", foodsuppliers[sites],,0) = 0"
does any one have ideas why or a better way to run this code on power query/power bi?
thanks.
Nothing in your code seems relevant
There is no SEARCH function, you want to use Text.Contains
You cant write foodsuppliers[sites] or you will be getting the entire column of all rows. You probably want each current row, which you would get with [sites]
This is not excel where you can do =if (xxx,fff,zzz) the format is if x then y else z
I recommend some tutorials

Conditionally Filtering Out Rows based on 2 Parameters in w/ Power Query

I have a table similar to the one attached below:
What I would like to do, using power query, is conditionally filter out (remove) rows where CenterNum = 1101 and DepCode = 257. I figured Table.SelectRows() would work but it doesn't and the query just returns, this table is empty. The #"Expanded AccountLookup" ,in my formula below, is referencing the power query applied step before the one I am trying to create. I'm hoping to get some input on how to remove rows based on these two paramters.
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = "1111001" and [NoteTypeCode] = "257")
Thank you!
You didn’t post a screenshot so it is hard to tell if the column format is text or numerical but try removing the quotes around the numbers
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = 1111001 and [NoteTypeCode] = 257)
If that doesn't work, check the actual column names against what you are using, especially for case (upper/lower) and leading/trailing spaces. The best way to do that is to temporarily rename it, and look at the code for the "from name"

evaluating a textual equation into a number

AS a newbie to m I still can't get my head around it. Here is the query I have. It's gone through a number of steps to get to the below. How do I use Expression.Evaluate against the whole query against the column EntryFee, which for the sake of simplicity the query is called #"Nearly There". I want to Evaluate the entire column Entry Fee. To reiterate it needs to be done in Power Query "M"
Snapshot of table/query
You can either add a new custom column with the code
Expression.Evaluate([Entry Fee])
or do a column transformation
= Table.TransformColumns(#"Nearly There", {{"Entry Fee", Expression.Evaluate, type number}})
To generate this step, you can select the column and then Transform tab > Format > Trim and then replace Text.Trim with Expression.Evaluate in the generated code.

Power Query / Power BI - How to move a cell value to a separate cell the easiest way?

I want to move a single value from column B to column A, how can I achieve it in the most simplest way in Power Query / Query Editor (Power BI)?
Please see attached images.
I know I might need to declare a variable so please enlighten me. By the way, I will delete row 1 afterwards, promote my headers, and rename column2 as PERIOD.
Thank you.
This might be along the lines of what you want to do.
If I start with this table named as Table1:
Then I click on the fx to the left of the formula bar:
And type = Table.InsertRows(Source, Table.RowCount(Source), {[Column2 = Source[KP20 rate]{0}, KP20 rate = null, Column4 = null]}) into the formula bar:
I used Table.InsertRows to create a new row in Table1. Source is the name of the latest state of Table1 after it is pulled into Power Query and before I do this step. So I actually use Source as the name of the table for this step instead of Table1. (Each applied step basically results in its own table. You probably know this already, but others may not.) So for this step I use Source as the table name in the Table.InsertRows statement. Then, since I want the new row to appear at the bottom of Source, I just enter the Table.RowCount of Source as the row number location for the new row. Then I enter each of the Columns' names and their values to be added. For Column2, I entered the value "Source[KP20 rate]{0}." Source[KP20 rate]{0} basically treats column KP20 rate as a list, where {0} serves as a pointer to the first item in the list. To target the second item in Source[KP20 rate] you would use Source[KP20 rate]{1}. You can see that I set the values for the other two columns (KP20 rate and Column4) to null.
The result:
Here's the M code in case you want to see it:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Custom1 = Table.InsertRows(Source, Table.RowCount(Source), {[Column2 = Source[KP20 rate]{0}, KP20 rate = null, Column4 = null]})
in
Custom1