Qlikview set date range selection in expression independent of selected values - qlikview

I wish to select a date range in a chart expression independent of selections in the document in the year and month date fields. I have two expressions that should work, but are not returning any values:
count({< Discharge_Year=,Discharge_Month=,Discharge = {'>=(Date(Today()-60))and <=Today()'}>}[Row Number])
or
count({1< Discharge = {'>=Date(Today()-60)'}>}[Row Number])
The second expression works when a date is inserted ( '>=01/01/2017'instead of '>=Date(Today()-60)'): the problem seems to be getting the Today()-60 function to work.
Any suggestions out there?

Found the solution:
The quotes in the >= need to be double quotes, and the Today() function needs date formatting:
Finished expression as follows:
count({1< Discharge = {">=$(=Date((Today()-60),'DD/MM/YYYY'))"}>}[Row Number])

Related

Why am I getting a Data Type Mismatch when comparing a date in Microsoft Access even though I am using the # delimiter?

I have a table of people's names who have attended a class and the timestamp of when they attended that class. This is stored in Access. I am trying to get the names of people who attended class on a given day. The code I wrote is as follows:
SELECT [Person Name], DateValue([Activity Start Date]) FROM [2019 Learning]
WHERE DateValue([Activity Start Date]) = #11/14/2019#
Even though the date I am looking for is contained in the table, when I run the query it works for a second and then I get an error message saying
"Data type mismatch in criteria expression."
It then changes all the data returned into "#Name?". Can someone please help explain why I am getting this error?
DateValue() function errors on null. Provide an alternate value. Can use Nz() function.
DateValue(Nz([Activity Start Date], Date())) = #11/14/2019#
Or use a date assumed would not be in the database, such as #12/31/2999#, instead of Date().
If you want to make double sure records with null are not captured, include additional filter criteria but still deal with Null in the equality. And with dynamic input like:
DateValue(Nz([Activity Start Date], Date())) = [Forms]![MyForm]![MyControl] AND NOT [Activity Start Date] IS NULL
Rather than substituting values in place of Null (which could potentially lead to undesired/confusing results, if such values are output by the query), another way to approach this might be to avoid the datevalue function altogether in the where clause.
Since datetimes in MS Access are merely numerical values for which the integer part of the number represents the date component and the decimal part represents the fraction of the day, or time component, you can use the following to obtain all records for #11/14/2019#:
select [Person Name], datevalue([Activity Start Date])
from [2019 Learning]
where [Activity Start Date] >= #11/14/2019# and [Activity Start Date] < #11/15/2019#
Here, Null values are automatically excluded as both comparison operators will return Null when supplied with Null, thereby excluding the record.

Case When X, then sum Y for Results tab

I would like to create a formula field on results tab that sums projected total IF the Opportunity has been won "this month". I'll be doing the same for lost opportunities and created opportunities but i can't get my formula to work:
Using a formula numeric or formula text field-
sum (Case when {opportunity.closedate}= this month), {projectedamount}
Any advice on how to adjust to work?
In a saved search, rather than including sum() in your formula, you should set the Summary Type to "Sum". You should also set the field to Formula (Currency), as that is what's being returned by the formula (sometimes other formula types will work, but the general rule is that the formula type must match the data type of what the formula will return).
With that done, you can compare the month and year of the date field to the current month and year to get this month, or perform simple date arithmetic to get last month, or any other period. For 'this month':
case when to_char({opportunity.closedate}, 'YYYY-MM') = to_char(sysdate, 'YYYY-MM') then {projectedamount} else 0 end
HTH.

Average score for a given name within the current month

I have a table data set of customer service reps score per call for the current year and I'm trying to build a query to calculate the average score for a given name within a given month
Something like calculating the average score for Matt's calls for the month of January.
I've been trying but I'm a begginer at SQL so I'm having trouble.
So far I've got this:
Select avg("general score") as General_Score, avg("Re_score") as Re_score
from Voc2019
Where [Name] ="Matt"
This gives me a error type mismatch
Would greatly appreciate any help you can provide to me.
UPDATE:
the code now works but While this does work I still need to order by a given date, but I I want to use this sql string to retrieve the average for a month specified varying on the month I want from excel but it's not returning any results
For instance I want Avg score for Matt in January
Currently I got
Select avg([general score]) as general_score, ("Re_score") as Re_score
from Voc2019
Where [Name] ="Matt" and Score_Date = Month(2)
The main issue with your code is that you are using a string ("general score") when attempting to reference a field (this is likely producing the type mismatch error).
You haven't indicated the field in your table that contains the date for each record, and so selecting the appropriate set of records for a given month is impossible without this information.
However, to correct your current code, I would suggest the following:
select avg(voc2019.[general score]) as General_Score, avg(voc2019.re_score) as Re_score
from voc2019
where voc2019.name = "Matt"
If you were to identify the field containing the date for each record, you could calculate an average per month using something like:
select
dateserial(year(voc2019.date), month(voc2019.date), 1) as [Month],
avg(voc2019.[general score]) as General_Score,
avg(voc2019.re_score) as Re_score
from
voc2019
where
voc2019.name = "Matt"
group by
dateserial(year(voc2019.date), month(voc2019.date), 1)
The above assumes that your table voc2019 contains a field called date dating each record.
EDIT:
Since your date is actually stored as a Text field rather than a Date field, you can use the DateValue function to parse the text content into a date value.
Hence, to obtain the average score for Matt in January, you might use something like the following:
select
avg(voc2019.[general score]) as General_Score,
avg(voc2019.re_score) as Re_score
from
voc2019
where
voc2019.name = "Matt" and
month(datevalue(voc2019.date)) = 1 and
year(datevalue(voc2019.date)) = 2019
Alternatively, since your date is stored as text, you could use the like operator to match only dates in January, e.g.:
select
avg(voc2019.[general score]) as General_Score,
avg(voc2019.re_score) as Re_score
from
voc2019
where
voc2019.name = "Matt" and voc2019.date like "##-01-2019"
I am way more familiar with Oracle SQL syntax, but pretty sure the problem is with some simple MS Access syntax. Your literal string "Matt" which should be single-quoted as in 'Matt' and the square brackets are used to enclose a column identifier only when it contains a space.
Try:
Select avg([general score]) as General_Score, avg(Re_score) as Re_score
from Voc2019
Where Name = 'Matt'

using a text box in a Query works fine with one table but fails when applied to another

I have created two text boxes on a form that allow me to select a start and end date of a query. My query works great for one of my tables that has a date column (MM/DD/YYYY). I would like to have it work on table2 that has just the month for date i.e; 1 for Jan, 2 for Feb, 3 for Mar, etc.
The Text boxes are formatted as General Date.
The reason for the two tables is that table1 has multiple entries per month and table2 sums that entries and gives a monthly value.
Here is my SQL that I am trying
SELECT [Property],[Value]
From [Table1]
Where [Date] Between [Forms]![Calendar Test]![Start Date] and [Forms]![Calendar Test]![End Date]
This works great with Table1. Since I have multiple entries per month per property table1 gives each entry and this is more than I want. I would prefer getting the summed value from table2 where there is just 1 entry per property a month.
SELECT [Property],[Value]
From [Table 2]
Where [Month] Between [Forms]![Calendar Test]![Start Date] and [Forms]![Calendar Test]![End Date]
The output from this second query is a blank return where the fields Property and Value are not populated with anything as opposed to the Table1 query where the data populates the fields.Is this because I have the "Month" field set up to give just (MM) instead of (MM/DD/YYYY)? was thinking that it might be an issue with the format of the Text Box calendar since it gives a full date and the table2 Month just has a month value?
If the datatype of your [Table 2].[Month] field is numeric, use the Month Function with your text box date values.
Where [Month] Between
Month([Forms]![Calendar Test]![Start Date])
and Month([Forms]![Calendar Test]![End Date])
If that field is text datatype, cast it as a number when you compare to the Month() values ...
Where Val([Month]) Between
Month([Forms]![Calendar Test]![Start Date])
and Month([Forms]![Calendar Test]![End Date])
Beware, if [Start Date] and [End Date] can be from different years, you will need a different strategy.

How to sum a set of values that lie between a start date and an end date?

I have created two dropdown list
1. start date
2. end date
I am using MySQL database as a source and I have a table called generation
having columns like (date , generation, turbine_id).
After selecting the start date and end date, I need to sum the corresponding values that lie between the two dates:
sum({<Date= {"<=$(=vStartDate)>=$(=vEndDate)"}> } Target)
I see that you already made two variables, vStartDate and vEndDate, So you can manipulate them using a Calendar or an Input Box.
In this case your set expression is almost good. It sould look like this:
Sum({$<Date = {">=$(=vStartDate)<=$(=vEndDate)"}>} Target)
So in the curly bracket it starts with a $ sign, which represents the current selection, which will be filtered by the expression. In the double quotes you tell that the value of Date should be bigger or equal than the evaluated =vStartDate expression (evaluation is done by using a dollar-sign expression), and less than vEndDate. This is how it works.