I would like my SQL query in PBI to be defined by parameter. The Parameter1 is a list of two values Big and Small. If I set up Parameter1 to Small then I want only a sample of data to be loaded. I am doing it this way:
let
ReturnQueryVar = (optional x) =>
if Parameter1 = "Small"
then " and Product in (1, 2, 3)"
else "",
Source = Sql.Database(
"myservername",
"mydatabase",
[
Query = "
select *
from table
where
1=1"
& ReturnQuery()
]
)
in
Source
I get this error:
Expression.Error: The name 'ReturnQuery' wasn't recognized. Make sure it's spelled correctly.
Update.
Facepalm! It was a typo. ReturnQuery should be ReturnQueryVar.
However, I would like to leave the question open for the part:
ReturnQueryVar = (optional x) =>
if Parameter1 = "Small"
then " and Product in (1, 2, 3)"
else ""
Does PowerQuery have a syntax for IF:
TextVar = IF(Parameter1 = 'S', 'This', 'That' )
I haven't seen syntax like that, but if you like you can create a function that does that, basically a "container" for the IF statement of Power Query.
For example, you can create a Blank Query called IF with the following contents.
(cond as logical, if_true as any, if_false as any ) =>
if cond then if_true else if_false
Using that function, anywhere it would have similar results and an IF statement.
I am just sharing here a PowerQuery snippet I ended up with. This code allows you to choose with a parameter between two alternate queries - the first for a sample and the other for the full rowset. It allows to download only a small sample of rows from a large table into Power BI Desktop.
let
QueryCode = if Query_Parameter = "Sample"
then
"select 1"
else
"select 2",
Source = Sql.Database(
"MyServer",
"MyDataBase",
[ Query = QueryCode ]
)
in
Source
Prior to that in Power Query Editor > Manage Parameters > Add New Parameter named Query_Parameter with 2 text values as a list: "Sample" and "Full" and based on this parameter the source of the query changes. Just replace "select 1" with anything narrowing your rows like "select top 1000..." or add WHERE condition. "select 2" is your query with full row set.
I'd like to ask about a problem related to regexp_extract in Hive. So, I'd like extract a value from json object
{"data":[{"key":"text","value":"12345"},{"key2":"text2","value":""}],"key3":time1,"key4":time2}
I use the regexp_extract query like this:
max(if(index=0, regexp_extract(column1, '"key":"text","value":"(.+?)"', 1), '')) as extract1,
max(if(index=1, regexp_extract(column1, '"key":"text","value":"(.+?)"', 1), '')) as extract2
but I got duplicate data for each, so let say it should return only 2 rows (row 1, row 2) but it gave me 4 rows instead (row 1, row 1, row 2, row 2) with completely identical data.
However, I've solved this by changing the regexp_extract into get_json_object like this:
max(if(index = 0, get_json_object(column1, '$.data[0].value'), '')) as extract_1,
max(if(index = 1, get_json_object(column1, '$.data[0].value'), '')) as extract_2
but still couldn't find the reason. Does anyone know why? Really appreciate your help!
I have a dataset like this:
2019-01-02-03-04-05-06-07-08-09-10-11-12
2020-01-02-03-04-05-06-07-08-09-10-11-12
2021-01-02-03-04-05-06-07-08-09-10-11-12
2022-01-02-03-04-05-06-07-08-09-10-11-12
2023-01-02-03-04-05-06-07-08-09-10-11-12
2024-01-02-03-04-05-06-07-08-09-10-11-12
All of the content, for each row, in in one sing column.
I want to apply a regexreplace() to have a result like this:
2019-01,2019-02,2019-03,2019-04,2019-05,2019-06,2019-07,2019-08,2019-09,2019-10,2019-11,2019-12
2020-01,2020-02,2020-03,2020-04,2020-05,2020-06,2020-07,2020-08,2020-09,2020-10,2020-11,2020-12
2021-01,2021-02,2021-03,2021-04,2021-05,2021-06,2021-07,2021-08,2021-09,2021-10,2021-11,2021-12
2022-01,2022-02,2022-03,2022-04,2022-05,2022-06,2022-07,2022-08,2022-09,2022-10,2022-11,2022-12
2023-01,2023-02,2023-03,2023-04,2023-05,2023-06,2023-07,2023-08,2023-09,2023-10,2023-11,2023-12
2024-01,2024-02,2024-03,2024-04,2024-05,2024-06,2024-07,2024-08,2024-09,2024-10,2024-11,2024-12
That is basically replacing each "-" by the first 4 numbers in the corresponding row.
As I know those are the year in the first part and the months in number in the second part, I know I can used the following formula to have the expected result:
=regexreplace(A1,"^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})","$1-$2,$1-$3,$1-$4,$1-$5,$1-$6,$1-$7,$1-$8,$1-$9,$1-$10,$1-$11,$1-$12,$1-$13")
PS.: my data is in A1
But how to have something more dynamic, to replace several parts of the string with one portion of the same string?
try:
=ARRAYFORMULA(IFERROR(TEXT(INDEX(SPLIT(A1:A, "-"),,1)&"-"&
TRANSPOSE(QUERY(TRANSPOSE(SPLIT(A1:A, "-")), "offset 1", 0)), "yyyy-mm")))
then:
=ARRAYFORMULA(REGEXREPLACE(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(
TEXT(INDEX(SPLIT(A1:A, "-"),,1)&"-"&
TRANSPOSE(QUERY(TRANSPOSE(SPLIT(A1:A, "-")), "offset 1", 0)),
"yyyy-mm")&",")),,999^99)), ",$", ))
or without spaces:
=ARRAYFORMULA(REGEXREPLACE(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(
TEXT(INDEX(SPLIT(A1:A, "-"),,1)&"-"&
TRANSPOSE(QUERY(TRANSPOSE(SPLIT(A1:A, "-")), "offset 1", 0)),
"yyyy-mm")&",")),,999^99)), " |,$", ))
Would you really want REGEXREPLACE? Alternatively:
=ARRAYFORMULA(TEXTJOIN(",",TRUE,LEFT(A1,5)&SPLIT(MID(A1,6,LEN(A1)),"-")))
Using REGEXREPLACE, maybe something like:
=LEFT(A1,7)®EXREPLACE(A1,"(\d{4}-\d{2})?(-)",","&LEFT(A1,5))
I am trying to format the information from a column that I am querying and compare that to information in a cell. I have tried to hack together various ways to do this, but I am not a proficient SQL/spreadsheet user.
In COLUMN I there is nothing.
In COLUMN K there is a match on A2.
In COLUMN N there is Information formatted like 31'-40' and 41'+.
I would prefer to use = instead of contains.
The REPLACE Function seems to work when I substitute N for a String and run it on the W3 School Website.
The REGEXREPLACE seems to work on D2. I would expect them to match, but they do not.
COUNT( QUERY( '2019'!A2:P, "select D where I='' and upper(K) contains '" & UPPER(A2) & "' and REPLACE(REPLACE(REPLACE(N, '-', ''), '''', ''), '+','') contains '"& Regexreplace(D2,"[[:punct:]]","") &"' ")
I get 0 matches.
you almost had it, but try like this:
=COUNTA(FILTER(2019!D2:D, I2:I="",
REGEXMATCH(UPPER(K2:K), UPPER(A2)),
REGEXMATCH(UPPER(N2:N), UPPER(D2))))
I have a string as such:
string query;
query = "insert or replace into TABLEA (a,b,c) values (#a,\"#b\",\"#c\");";
that way i can insert strings into B and C with just a simple replace:
string instring("I have a 3\" gauge");
string instring2("I am looking for 1/8\" thickness");
Replace(&query, "#a", to_string(1));
Replace(&query, "#b", instring);
Replace(&query, "#c", instring2);
So now my query string is:
"insert or replace into TABLEA (a,b,c) values (1,\"I have a 3\" gauge\",\"I am looking for 1/8\" thickness\");";
SQLITE3 gets it and it looks like:
insert or replace into TABLEA (a,b,c) values (1,"I have a 3" gauge","I am looking for 1/8" thickness");
The issue is that the strings end prematurely. I tried to add additional escape characters but that wasnt seeming to work either.
Right now i am using sqlite3_exec() to carry out everything. Is there something else i should do? Does a prepared statement handle what i am trying to do?
Should i just try it with prepared_v2 and that might resolve issues?
How should i be approaching this?
In SQL, strings use single quotes, and are escaped by using two single quotes. (Double quotes are accepted for compatibility with MySQL, but should not be used.)
Your query should look like this:
INSERT OR REPLACE INTO TableA(a, b, c)
VALUES (1, 'I have a 3" gauge', 'I am looking for 3/8" thickness')
or like this:
INSERT OR REPLACE INTO TableA(a, b, c)
VALUES (1, "I have a 3"" gauge", "I am looking for 3/8"" thickness")
However, to avoid string formatting problems, it is recommended to use parameters.
This is how it works with direct SQLite function calls (wrappers might work differently):
const char *sql = "INSERT OR REPLACE INTO TableA(a, b, c) VALUES (1, ?, ?)";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, "I have a 3\" gauge", -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, "I am looking for 3/8\" thickness", -1, SQLITE_TRANSIENT);
You need single quotes around the each inner string:
string query;
query = "insert or replace into TABLEA (a,b,c) values (#a,'\"#b\"','\"#c\"');";