I have a report in SSRS that shows various tablixes based on a date parameter. The user will select a date and the report will populate based on that date. This works fine. However, I would also like the user to be able to click on a "next record" button (and a "previous record" button) and the parameter (and report) will change based on the next date in the database.
The way I would normally accomplish this is by creating a text box with an action. For example, I would create a text box "Next Record", go into text box properties > Action > Go to report >, and send a new parameter that normally would be something like Parameters!survey_id.Value + 1. This works well if I want to increment the numeric ID by a static number (ie, increment by 1). I assume it works for dates too if I want to increment the day by 1 day; but how do I do this if the next record is not one day?
One way I thought would work is to create a row_number field in the dataset that provides the values to the parameter. Here is the code:
select distinct sm.survey_date
,row_number() Over (order by survey_date) as increment
from survey_main as sm
group by sm.survey_date --sometimes there are multiple surveys in one day
order by sm.survey_date desc
which gives the results
survey_date increment
2019-09-16 194
2019-08-24 193
2019-01-14 192
My thought was I could use the increment field in the text box action to find the next survey_date, but I cannot figure out the code (or if it's possible).
Is there a way to make that work? Are there any other suggestions or workaround that you can think of?
Thank you!
There are a few ways to do this, but this is the way I would approach it.
If the reports dataset was something like (this could be refactored but it easier to digest like this)
DECLARE #nextDate date
SELECT #nextDate = MIN(sm.survey_date)
FROM survey_main sm
WHERE sm.survey_date >= #reportParameterDate -- this is the date passed in from the report
SELECT *
FROM survey_main sm
WHERE sm.survey_date = #nextDate
So, first time you run the report, if you passed in 2019-08-24 then you would get data for that date, if you passed in 2019-08-25 you would get data for 2019-09-16
Then on the action for the textbox "button" the date parameter would be and expression something like
=DATEADD("d", 1, FIRST(Fields.survey_date.Value, "myDatasetName"))
This will add 1 to the survey_date that is in the dataset (these should all be the same, hence FIRST() will be fine to use).
Going back to the first run, we set the parameter as normal to 2019-08-24 and get data for that date, now when we click the 'Next Record' button it will take the FIRST survey_date (which will be 2019-08-24) and add 1 day to it and pass it to the report via the action. So we will be passing 201-08-25. The reports dataset will get the MIN survey_date that is >= the date we passed in, in this case 2019-08-25, and return 2019-09-16 which we stick in #nextDate which subsequently filters the data returned.
Hope that makes sense.
Related
I have a report with one data source in Google Data Studio. The data is at month level inside the BigQuery table YYYYMM (for example 202001). I have a filter that is set on the year-month dimension which works ok but I would like to add a default value which changes to last month(max(year_month)) available in the table.
Is this possible? I only found the option to hard-code the default selection but this can't be updated dynamically
as in this screen shot
Jaishree's suggestion
You can create an another field/column as same year-month and always keep the latest year-month value as "latest month" or any name you want. You can use logic like
when existing_column = max(year_month) then "latest month" else existing_column
Something like this
existing_column new_column
022020 latest month
012020 012020
122019 122019
112019 122019
You can pass this "latest month" value in the default selection option this option which is just below the dimension. But each time you upload new data you have to update this field like change the table suppose for next month you table should be like
existing_column new_column
032020 latest month
022020 022020
012020 012020
122019 122019
112019 122019
You can make auto update by scheduling queries as well in big query from where you are loading data to Google data studio.
It can be achieved by setting the Date Range (at the chart, report level, Date Range Control, etc) to Last Month.
First, a YYYYMMDD Date field needs to be created at the Data Source. This can be done using the CONCAT function to add a Day (the 15th is used in the formula) and then using the TODATE function (although in this case the TODATE function is not required, however, adding it as best practice):
TODATE(CONCAT(Date, "15"), "%Y%m%d", "%Y%m%d" )
Adding a link to post on the Google Forums (Nimantha; 10 Feb 2020) which also has a GIF to demonstrate the process above as well as adding the Date field at the Report Level.
You can do it by creating a new field where rows that belong to the current year-month are labeled as 'Current Month' while the others could use your preferred formatting (I'm using "%Y%m - %B %Y" as it allows me to sort report dates in a drop down list).
To do so, you need your date column and CURRENT_DATE() in the same format with the help of FORMAT_DATETIME(). Then, you can use CASE WHEN logic as follows:
CASE
WHEN FORMAT_DATETIME("%Y%m", MyDateColumn) != FORMAT_DATETIME("%Y%m", CURRENT_DATE())
THEN FORMAT_DATETIME("%Y%m - %B %Y", MyDateColumn)
ELSE 'Current Month'
END
Finally, you could use a drop down list control with the new field and use 'Current Month' as the default selection. The main advantage is that the code above will be evaluated every time data is refreshed so no extra automations needed.
Hope it works for you.
I have:
a table with user names
a table indicating actions with columns for user name, action time, action name. Named events unique_events
I started collecting data on January. I want to have a column in my table of user names which indicates how long it has been since a user first used my application and the first of January.
So if a user first logged in in January, the value of the row with that user's name will be 0. If one logged in on March it will be 2.
I tried:
Column = DATEDIFF(01-01-2016, MIN(SELECTCOLUMNS(FILTER('events unique_events','events unique_events'[User Name] = Users[User Name]),"DatedTime", [DatedTime])),MONTH)
which returns an error saying the Min function needs a column reference.
I also tried the same with FirstDate instead of MIN which returned an error saying FirstDate can't be used with summarize functions.
Any other ideas on how to achieve this, or fix what I tried?
(for simplicity, I will call your table 'Events', and user login dates field 'User_Login_Date').
First, define your app start date as a measure:
App_Start_Date:= DATE(2016, 1, 1)
Then, define measure that finds min differences between Application Start Date and User Login dates:
User_Start_Diff=: MINX(Events, DATEDIFF([App_Start_Date], Events[User_Login_Date], Month))
Drop this measure into a pivot table against user names, and you should have your desired result.
How it works:
1) MINX goes record by record and calculates date differences for each customer login. It then finds minimum in the results;
2) When you drop the measure into a pivot table, it splits MINX results by customer, and recalculates min for each of them separately. You don't need to do the grouping.
Creation of [Start_Date] measure is not technically necessary but a matter of good style - don't hardcode values in your formulas, always create measures. You will thank yourself later when you need to make a change.
I have a table like the next one and I would like to obtain the last "measure date" for every "Work date" of a same "ID".
At the end I would like to have this result:
In that example, the last two rows of the initial table disappears in the final one because I just want the last "To do" measure entered in the table for every work date of a same ID.
As you see in the first table, for a same ID I can have 2 differents measure_date for a same work date. However I need only the last measure date a same work date by ID. In that case, I need to get the last measure date to get the good "To Do" to achieve my job .
The table can have a lot of different Work Date and 100 of ID, which are the same for every Work date.
How can I do that?
From what I read the query should go something like this,
SELECT id, work_date, max(measure_date), todo FROM tablename GROUP BY id, work_date;
But notice that the todo value will be quite random (or can have unexpected results) so leave it out from the query. But like with the comments, there is not enough information and it does look like excel.
It's the first time that I am using SQL Server reporting services. I have two date type parameters: Start Date & End Date. I can run the report correctly when I fill these two parameters with the corresponding dataset.
However, when the user fills only the Start Date, I would like to show a report from that date and later. e.g. If the user fills only the Start Date my query will be something like that
WHERE StartDate >= #StartDate
or if the user fills only the EndDate field, my query will be something like that
WHERE EndDate <= #EndDate.
I have the following two questions:
1) Can I use different datasets according to what the user selects using SSRS? e.g if the user do not select anything I would like to run a different query.
2) and if yes how can I handle that ? because filter parameters are mandatory fields and all the time I receive the warning message.
Any advice would be appreciated.
Thank you.
It seems there is a simpler solution to your problem, if I've understood things correctly. You seem to indicate the parameters are optional. In that case, just Allow Null Values for the parameter, and rewrite your WHERE clause as follows:
WHERE (#StartDate IS NULL OR StartDate >= #StartDate)
AND (#EndDate IS NULL OR EndDate <= #EndDate)
In any case, you can't "not execute a dataset" based on the chosen parameters (nor should you usually need to). If you do need something like that, a subreport is probably the way to go. However, based on your question I'd think there's a simpler solution available.
I am using sql server 2005 reporting service to generate report base on a database. There are two columns which are datetime type ColumnA and ColumnB. The report would display a KPI image on this report by comparing these two columns.Below is the expression for selecting image
SWITCH(DateDiff("d",Fields!ColumnA.Value,Fields!ColumnB.Value)<0,"kpi_r",
DateDiff("d",Fields!ColumnA.Value,Fields!ColumnB.Value)>0,"kpi_g",
DateDiff("d",Fields!ColumnA.Value,Fields!ColumnB.Value)=0,"kpi_y")
For most of the records, the image is correct. Only for one record, the result is very strange.
For this record
ColumnA=2010-04-23 08:00:00 ColumnB=2010-04-22 17:00:00
It would display kpi_r, it displayed kpi_y. I have checked the value of DateDiff(d,Fields!ColumnA.Value,Fields!ColumnB.Value) in the SSMS, the value is -1. Why does it display kpi_y? Does anyone meet this problem before?
Best Regards,
The difference is that the SSMS DATEDIFF function counts the interval boundaries between the two dates whereas ReportBuilder counts the actual intervals. Within SSMS if you cross midnight you have triggered a day boundary so in your example you get -1. In ReportBuilder it is looking for 24 hours to be between the two values so you get 0. If you change the time on ColumnA to be '2010-04-23 17:00:00' you will see the value changes to -1 as you expected. For your comparison it would probably make sense to strip the time component from ColumnA and ColumnB when you do this SWITCH statement.
the above answer is spot on.
here's a few ways to strip time off a date depending on your preferences:
1. do it in RS: use and expression like dateserial(year(Fields!ColumnA.Value),month(Fields!ColumnA.Value), day(Fields!ColumnA.Value)) in your switch expression
2. do it in SQL: use an expression like cast(round(cast(ColumnA as float),0,1) as datetime) in your query