Getting minimum values out of calculated table - database

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.

Related

SSRS - Next/Previous Record Button Based on Date

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.

Return matching check dates for employees but getting previous check dates that match the gross amount of the checks SQL

I am trying to display the payroll date for employees who have received a check on the most current check date. However, when I enter the following, it is returning employees multiple times if the amount of their check matches a previous check. Shouldn't this be returning dates that match the current check date as opposed to also returning previous check dates with the same gross pay?
WHEN ext.TerminationDate IS NULL AND #CheckDate = f.CheckDate THEN format(#CheckDate,'yyyyMMdd')
f is a table I am pulling the dates from and #checkdate is a parameter. Anyone know what is causing this issue?
From the way you have your WHEN statement set up, it looks like you have this set up as a column in the SELECT statement. If that is where this sits, then your script is not really filtering out anything. You are simply checking if the current row matches your variable and then passing your variable (once formatted) back.
This is how I would look at forming your query:
select OtherColums, format(f.CheckDate, 'yyyyMMdd') from f where f.CheckDate = #CheckDate
This will filter out to only records that have checkDate columns equal to your variable. If this does not help, please provide more information because the code you provided does not give us much context.

How can I split data from a second dataset in SSRS

I have two tables in SSRS. One holds the amount of insurance claims in a given month and one holds the amount of insurance complaints in a given month
Each table is calculated by either =COUNT(Fields!Claims.Value) for claims and =COUNT(Fields!Complaints.Value) that is simple enough and is split over the current 10 months of a year
Where it gets tricky though is that the claims table has an additional line where it calculates complaints as a ratio of claims. My current expression reads as follows:
=COUNT(Fields!Complaints.Value, "Complaints"/=COUNT(Fields!Claims.Value)
but the problem I have is that it's taking the full YTD value of the complaints and dividing by the monthly amount of claims.
ASK:
How can I get a calculation similar to above but only dividing complaints by month and claims by month - but keeping in mind that the complaints data comes from a different table
You can use LookupSet function to get the claims in the same month.
Create a tablix and use the Complaints dataset in the DataSetName property.
Add Month as Row Group.
For complaints column use:
=Count(Fields!Complaints.Value)
For claims column use:
=LookupSet(Fields!Month.Value,Fields!Month.Value,Fields!Claims.Value,"Claims").Length
For Ratio column use:
=IIF(
ReportItems!Textbox69.Value=0,0,
Count(Fields!Complaints.Value)/ReportItems!Textbox69.Value
)
Replace Textbox69 by the name of the textbox where Claims (LookupSet) expression is placed.
Note the validation for zero denominator in case there is no claims in a given month.
It should produce:
Let me know if this helps.

MDX - How to know if a measure has been pivoted with a dimension?

I was hoping someone could help me with this problem.
In a sum type measure, i need to know if this measure has been pivoted with my time dimension. If is not, the measure must show the sum of the last day. In other case, the measure must show the sum of the day with has been pivoted.
I have the measure "Users", if this measure hasn't been pivoted with my time dimensiĆ³n, it shows the sum of all users of all the time. For example it shows: 5,000,000. When it has pivoted with the time dimension, it shows de users from the day. For example: 100,000 for today 21/05/2015.
When this measure hasn't been pivoted with the "time" dimension, the measure must show 100,000 and not the total sum of all days.
I've been trying white some MDX formulas but i have not found some solution for this.
Thanks
I've interpreted this as when pulling your Users measure without pivoting on the Time dimension you would prefer to see the value that corresponds to the current date.
This can be achieved by modifying the Default Member of your Time dimension. Setting the default date to this last available date
Within your Dimension in SSAS, browse to the Dimension Structure tab and select the attribute which represents the key column (ie. the Usage property is set to Key for only one column)
Within the Properties pane (strike F4 if you do not see it already to open it) select the Default Member property, then select the elipses (...)
The Set Default Member - [Dimension Name] pane will have 3 options, select the last Enter an MDC expression that specifies the default member
Use ONE of the following expressions
TAIL([Date].[DateAttribute].Members).Item(0)
TAIL(NONEMPTY([Date].[DateAttribute].Members)).Item(0)
TAIL(EXCEPT([Date].[Date].Members,{[Date].[Date].[Unknown]}),1).Item(0)
In my case I used the last as I only want to exclude my UnknownMember member
Now when you pull the measure down the value will be for the last date in your cube. This will affect other cubes built on this date dimension of course.

Criteria to get last not null record present

I have a daily record table where records are stored date wise. I am using hibernate criteria to access data. How do i get the last date till which records are present continuously (date wise continuity) by providing a date range. For example, say records are there from 21-09-2012 to 25-09-2012 , again from 27-09-2012 to 31-09-2012. I want to form a query using criteria to get record of date 25-09-2012 because for 26-09-2012 there are no records (by passing date ge 21-09-2012 and date le 31-09-2012) . I want to know the last date till which records are present continuously. Say the table has three fields - 1.recordId (AI) 2.date 3.Integer record.
Its not a proper solution to your question. But it may be scenario specific.
How about getting the data for a date range and show then on a calender. Change the color of date if the corresponding value is null.
I think HQL will be better way to this in Hibernate:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

Resources