Geting prompt values from another query through functions - cognos

I am a beginner in cognos 10. I want to know how to get the value of a prompt from a Query1 and use it on Query2. My requirement is I want a prompt to ask the year for which I want the data in Query1 and in Query2 I want the data relevant to the previous year to that entered in the prompt. How to do that?

You can use the same parameter (tied to the prompt) in filters in both queries. If your parameter is Parameter1 and contains a four-digit year, and your data item in your filter is [Year] then your Query1 filter might look like this:
[Year] = ?Parameter1?
Your Query2 filter would be:
[Year] = ?Parameter1? - 1
Depending on your data source you may have to cast the string parameter to an integer before doing the subtraction though most SQL implementations will implicitly convert the string parameter to an integer for you.

Related

Excel 2016 query editor, filter/where clause

I'm struggling with probably the simplest thing. How do I filter my data using a where clause in Excel 2016, ODBC sql database source.
I'm new to Excel 2016, I used to just modify the SQL Command via the odbc properties. Now i can't even see the SQL command when I create the odbc.
So from just playing around. I gathered that there is now a series of "Applied Steps", but I struggle to find where I can find how to limit the rows to a date range. (I don't want to display the date because I'm aggregating by inventory type) But there must be somewhere I can say return only for date '2020-05-01' for example. (where date = '2020-05-01')
the next steps will then be making that a dynamic parameter so I can have some sort of input when the data refresh.
Any tips?
The SQL statement is greyed out
Right clicking a step -> view native SQL will gray out when that step cannot be query folded. Previous steps are still viewable.
You can find the sql query and server name if you choose advanced editor -- which displays all steps in one editor.
native sql
If you want raw sql, import the table as a new connection and paste your query into that box.
note: [] in powerquery is a Record literal.
// indented for readability
let
Source = Sql.Database("server", "dbo"),
// using parameterized queries
RawSQL = "SELECT * FROM users as
WHERE EnglishMonthName=#MonthName
AND
EnglishDayNameOfWeek=#DayName",
TableName = Value.NativeQuery(
Source, RawSQL
[
MonthName="March",
DayName="Tuesday"
]
)
in
TableName
I don't want to display the date because I'm aggregating by inventory type
Using the UI, you would import the table keeping the date column. Filter rows. Then choose remove column as your last step. Your model will not display removed columns.
Where clause in PowerQuery aka: Get Data
Where is the function Table.SelectRows The UI will generate the code for you
Right click your date column -> filter -> date/time -> equals
If you want 2020/05/09 It generates:
= Table.SelectRows(#"Source Table", each [Date] = #date(2020, 5, 9))
You may use multiple conditions, like a date range
= Table.SelectRows(#"Table Name", each
[Date] >= #date(2020, 5, 9) and
[Date] <= #date(2020, 6, 1)
)

How to include varying count of variables into a sql IN statement?

I have a number of variables (count varies each time script is executed). I need to insert these variables into a sql statement. For example, let us assume the sql statement to be:
select * from customer where customer_id in (...);
What comes in those dotted lines will be my variables. If I knew the count of variables, I could have done something like:
query='''create table xyz as
select * from customer where customer_id in (?,?);'''
cursor.execute(query,var1,var2)
query='select * from xyz;'
xyz_details=pd.read_sql(query,con)
But since the count varies, I cannot keep a fixed number of '?' in my query. It could be 2 in one run and 4 in another. The script does store the count in a variable 'cnt'. So if cnt is 4, i have var1,var2,var3,var4 as arguments, and i will need to have 4 '?' in my query.
However, since these counts keep changing, I cannot fix a definite value in my query and I am looking on how to proceed.
Any comments are appreciated!
Maybe you can try something like that ?
list_args = [var1, var2, ..., varn]
query='''create table xyz as
select * from customer where customer_id in ({});'''.format(','.join('?' * len(list_args)))
cursor.execute(query,var1,var2)
query='select * from xyz;'
xyz_details=pd.read_sql(query,con)

concatenate string with date in SAS eg

I need to create a computed column in SAS with the combination of the string 'ULPDT_' and the result from the today() function. so that my result looks like this: ULPDT_20190101. Here is my non-functional code for the advanced expression:
t1.SourceFile='ULPDT_'||PUT(today(), yyddmmn8.)
year-day-month, YYYYDDMM, is not a normal representation for date, you might actually want year-month-day YYYYMMDD
t1 is indicative of an EG generated join wherein the t1 is a table alias. If you are editing the code of a join node, and the problematic statement is part of a select clause, the correct SQL syntax might be
'ULPDT_'||PUT(today(), yymmddn8.) as t1.SourceFile
Hand coded example (versus EG visual join):
proc sql;
create table x as
select
'ULPDT_'||PUT(today(), yymmddn8.) as SourceFile
from
sashelp.class(obs=1)
;

Excel Power Query - from web with dynamic worksheet cell value

We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]

Search using LIKE in a table

I want to retrieve data of 'Match' and 'Average' columns from the following table if '2'
exists using LIKE.
I have tried by writing the following query but it failed.
SELECT * FROM batsman_profile
WHERE (Match LIKE '%2%') AND (Average LIKE '%2%');
Can anyone help me how to retrieve data as I want.
In order to treat these numbers as a String you should use function CONVERT(varchar(10), field_name) like this:
SELECT *
FROM batsman_profile
WHERE (CONVERT(varchar(10), Match) LIKE '%2%')
AND
(CONVERT(varchar(10), Average) LIKE '%2%');

Resources