I have a parameter date from dataset date:
Sample of data
Key
Month
Year
Row num
20210131
January
2021
3
20201231
December
2020
2
20201130
November
2020
1
I need to do an expression that returns the previous of selected value for example when a user selects January 2021(date parameter) it will return December 2020 in expression in text box.
I try previous(first(fields! Month_year. Value, "group name")) but it returns error, is there any way to resolve it?
I would suggest that you extend the dataset query to include the previous value against each row.
Assuming you dataset query is called DataSet1 and uses a table called 'myTable' then something like this (I changed the sample column name from Key to KeyID to avoid keyword problems).
SELECT *
, LAG(KeyID) OVER(ORDER BY RowNum) as PreviousMonthKeyID
FROM myTable
NOTE: This assumes row number is always in the correct order, if not then you could change this to order by KeyID.
You can then use a LOOKUP() to find the correct value, so if your parameter is called selectedDate and it's value is set to the KeyID column, use something like
=LOOKUP(Parameters!selectDate.Value, Fields!KeyID.Value, Fields!PreviousMonthKey.Value, "DataSet1")
This reads "get the value in selectdDate, find this value in the column KeyID in the dataset called DataSet1 and return the value found in the column called PreviousMonthKeyID"
Related
I'm trying to come up with an SSRS expression to return a previous value from the year prior. As you can see in the screenshot, I'm trying to return the value of 22 in the "Prior Year Actual" column. This value aligns with April 2020. The last month I have in the dataset currently is April 2021. Essentially I want to return the value from the year prior based on the last column of April 2021.
Can anyone help with this? Greatly appreciated!
If your month fields are dates in a column group, you could get the MAX date and add it to the dataset.
DECLARE #MAX_DATE DATE = (SELECT MAX(DATE_FIELD) FROM TABLE)
SELECT *, #MAX_DATE AS MAX_DATE
FROM TABLE
Then your expression could use the new field to make it work like you want.
=MAX(IIF(Fields!Date.Value = DATEADD("y", -1, Fields!MAX_DATE.Value), Fields!COLUMN.Value, NOTHING))
This worked! Thank you very much Hannover for your expression suggestion!!
=MAX(IIF(Fields!DateTest.Value= DATEADD("yyyy", -1, MAX(Fields!DateTest.Value)), Fields!Operating_Days.Value, NOTHING))
I hope someone would be able to assist\help.
I have a Tablix that I'm trying to populate with three separate (summed) values (Current Month, Current Year and Previous Year) from one field based on a parameter. My parameter is set as yyyymm. My expression logic is as follows for each summed value:
Sum Current Month values
=SUM(Fields!Quantity.Value),IIF(Parameters!YearMonth.Value) = CDATE(Now()), "yyyyMM")
Sum Current Year values
=SUM(Fields!Quantity.Value),IIF(Parameters!YearMonth.Value) = CDATE(Now()), "yyyy")
Sum Previous Year values
=SUM(Fields!Quantity.Value),IIF(Parameters!YearMonth.Value) = CDATE(Now()), "yyyy")-1
I'm getting the following error when attempting the above expressions:
The Value expression for the textrun Quantity5.Paragraphs[0].TextRuns[0] contains an error: [BC30205] End of statement expected
As Harry pointed out, the IIF statement syntax is
IIF([Expression to evalute], [Expression to return if true], [Expression to return if false])
Also, I think you need to format the date you get back from now() so it matches the format of your parameter.
So, taking your first expression it should be something like
=SUM(
IIF(Parameters!YearMonth.Value = FORMAT(Now(), "yyyyMM"), Fields!Quantity.Value, Nothing)
)
So starting at the inner expression, we compare the parameter to today's date formatted as yyyyMM. If the match the return the value of the Quantity field, if not return nothing.
The outer SUM then just sums all these results.
I see a mystery behaviour in my *.rdl. I have 2 datasets defined.
I have 4 parameters defined,
StartDate,EndDate, companyid, sitecode
For both datasets,I have defined a select query.
DatasetA uses #Enddate
DatasetB not using it( it uses, startdate, companyid, sitecode)
I want to getrid of #EndDate from datasetA too. So, im using #startdate parameter to define enddate and using that in my sql query.
eg :
DECLARE #eDate AS date =EOMonth(#StartDate)
But after getting rid of this, #EndDate, my second data retrieves less data.(ie: if it should return data from september and october, now it returns only from September)
I checked the rdl, rowgroup/column group everywhere, but there is no any filter defined like that. I see this behaviour, only if i change #EndDate parameter from 1st dataset.
eg:
DECLARE #eDate AS date =EOMonth(#StartDate)
Select ..
Where date>=#StartDate AND date<=#Enddate to date<=#eDate
Is there any place we use parameter as a filter check? If so, how can I check it, where it has been used in the *.rdl?
I figured out the issue. This is due to Lookup function I use between both datasets. So, when dataset2 is trying to retrieve the correct number of rows, dataset1 was controlling it, becuase it didnt return rows for that date.
eg: dataset1 returns September data, while dataset 2 returns September+October data.
Due to lookup function, Dataset1 controls the dataset2's October data.
I have a query in Access where I want to select a range of dates from a calculated field in the query.
The field is populated using the following expression:
DueDate: DateAdd("m",-([PMI job lookup table]![Frequency]),[Date])
I'd like to select everything from a certain month and year from this field.
For example I'd like to list all the jobs in say May 2014.
In your query, add this criteria for the field of DueDate:
Between DateSerial(2014,5,1) And DateSerial(2014,5+1,0)
This will filter for dates between 2014-05-01 and 2014-05-31.
first off: I know virtually nothing about MS Access but now I'm in a situation where I have to use it (dataset is too big for Excel). The data has column names like Customer_Name, Product Name, Amount, Date
Date refers to the last day of a month, so for example for February it's 28/02/2013. Now I want to compare the amount a customer bought in February to the amount he/she bought in January and calculate the difference. So far, I've been able to this by prompting the user to enter the date.
SELECT Data.Customer_Name,
Sum(IIf(Format(Date,"yymm")=[Startdate (yymm)?],Amount,0)) AS Amount_Startdate,
Sum(IIf(Format(Date,"yymm")=[Enddate (yymm)?],Amount,0)) AS Amount_Enddate,
Amount_Enddate-Amount_Startdate AS Difference
FROM Data
GROUP BY Data.Customer_Name;
This works but is it possible for Access to recognize which dates are in the column "Date" (there are only two distinct dates) so the user does not have to enter anything? Also, I tried to replace "Amount_Startdate" with a field that has the respective date in its name (e.g. "Amount_Feb2013") and played around with ampersand but it didn't work.
If you create a new table called tblValues with just 2 fields; ID and TDate (always try to avoid using reserved words like "Date", "System" or other words that Access already assigns a function to), you can fill it like this:
ID TDate
-- ---------
ST 1/31/2014
EN 2/28/2014
Then you could use the DLookup function to make this code generic:
SELECT Data.Customer_Name,
Sum(IIf(Format(Date,"yymm")=DLookup(Format(TDate, "yymm"), tblValues, "ID = 'ST'"),Amount,0)) AS Amount_Startdate,
Sum(IIf(Format(Date,"yymm")=DLookup(Format(TDate, "yymm"), tblValues, "ID = 'EN'"),Amount,0)) AS Amount_Enddate,
Amount_Enddate-Amount_Startdate AS Difference
FROM Data
GROUP BY Data.Customer_Name;
Then you could just update the table with the values you want to use as start and end dates whenever you want.