Display Account Records from 2 PM To 2 PM - salesforce

I need to build a report on Accounts where my criteria should contains
Records that are created from yesterdays 7 AM To todays 7 AM
Please help me to solve

Create a new formula field on the record of type "Checkbox" containing the following:
AND(DATEVALUE(CreatedDate) >= TODAY() -1,
OR(
AND(
DATEVALUE(CreatedDate) = TODAY() -1,
TIMEVALUE(CreatedDate) >= TIMEVALUE("07:00:00.000")
),
AND(
DATEVALUE(CreatedDate) = TODAY(),
TIMEVALUE(CreatedDate) <= TIMEVALUE("19:00:00.000")
)
)
)
(you might need to adjust the time values (07 and 19) to adjust for your time zone as formula's use GMT values)
Next filter your report on accounts that have this checkbox set to TRUE.
If it's child records you are looking for, you need to do a few extra steps, one of the options than is to create a report filter (or type) that only shows accounts that have at least one record with this checkbox set to TRUE.

Related

Modify T-SQL query

I have trouble with this query:
SELECT DISTINCT
r.uuid AS id,
r.uuid,
r.customerId
FROM
IF_reminders r
LEFT JOIN
IF_reminders_sent rs ON rs.reminderUuid = r.uuid
AND rs.event = 'eventName'
WHERE
r.eventNameEnabled = 1
AND (rs.sentAt IS NULL
OR rs.sentAt NOT BETWEEN (DATEADD(DAY, -14, '2022-05-01')) AND (DATEADD(DAY, 1, '2022-05-01')))
The date in DATEADD function is filled programmatically.
Table IF_reminders contain defined reminders of different types for the users.
Table IF_reminders_sent contain records then the reminder for particular event was sent to the user.
The query must return a list of user reminders for the event to which the reminder should be sent. If a reminder has already been sent, this user should be ignored.
The query shown above works as expected if table IF_reminders_sent does not contain any rows from the past years. If table does contain rows from past years, then user will get reminder every day in the specified date range.
How to update the query in a way that if for current year remainders for particular event not yet sent then full list will be returned but if current year has sent reminders for particular event then past years records will be ignored.
Update
Tables structures. Three ... represent additional events columns but birthday and mothersday describe possible structures for all of them.
IF_reminders columns
IF_reminders_sent
uuidcustomerIdsortByfirstNamelastNameemailphoneaddressrelationshipbirthdayEnabledbirthdayDate....mothersDayEnabledcreatedAtupdatedAt....
idcustomerIdreminderUuideventsentAt
The idea of query is to filter out user reminders what the program should sent out. Program will fire function for to send out Mothersday reminders 7 days before event and send it once. While the IF_remiders_sent was empty all works OK. But then it contains records from the past year then query returns always list of reminders to be sent because sentAt for previous year is NOT BETWEEN dates specified in the query and starts spam users. If for the mothersDay event reminders for current year are not sent yet the query has output full list of users who have this reminder active. If for the current year the reminder is sent it should ignore current year records (NOT BETWEEN part of query) and now it has to ignore previous years too. How to add this condition to the query?
Sample IF_reminders_sent:
id
customerId
Uuid
event
sentAt
2
124724
4871a550-0d85-4391-83e0-2fff63e412ae
mothersDay
2021-04-26 16:36:59.877
9
124724
4871a550-0d85-4391-83e0-2fff63e412ae
mothersDay
2022-04-26 16:36:59.877
You can define in your where clause to check the current year:
SELECT DISTINCT
r.uuid AS id,
r.uuid,
r.customerId
FROM
IF_reminders r
LEFT JOIN
IF_reminders_sent rs ON rs.reminderUuid = r.uuid
AND rs.event = 'eventName'
WHERE
DATEPART(year,rs.sentAt)=DATEPART(year,getdate()) and
r.eventNameEnabled = 1
AND (rs.sentAt IS NULL
OR rs.sentAt NOT BETWEEN (DATEADD(DAY, -14, '2022-05-01')) AND
(DATEADD(DAY, 1, '2022-05-01')))
You need to flip the logic around. You are looking for all reminders which do not have a sent reminder since the beginning of the year.. So you need NOT EXISTS
SELECT
r.uuid AS id,
r.uuid,
r.customerId
FROM
IF_reminders r
WHERE NOT EXISTS (SELECT 1
FROM
IF_reminders_sent rs
WHERE rs.reminderUuid = r.uuid
AND rs.event = 'eventName'
AND rs.sentAt >= DATEFROMPARTS(YEAR(GETDATE()), 1, 1)
AND rs.sentAt NOT BETWEEN DATEADD(DAY, -14, '2022-05-01') AND DATEADD(DAY, 1, '2022-05-01')
)
AND r.eventNameEnabled = 1;
Note that if you want to give a date range you should always use rs.sentAt >= SomeDateCalculation AND rs.sentAt < OtherDateCalculation rather than using functions such as YEAR(rs.sentAt) = YEAR(GETDATE())

Is it possible to filter by "DATE or OTHER DATE" in Google Data Studio?

I have a data source that has four different dates associated with each matter- open date, close date, created date, and modified date. I want a scorecard to display the total number of matters that we "worked with" in a set date range, which includes matters that any of those dates are in the correct range for.
I essentially want this:
SELECT COUNTUNIQUE(ID)
WHERE ((openDate >= Date1 AND openDate <= Date2)
OR (closeDate >= Date1 AND closeDate <= Date2)
OR (createdDate >= Date1 AND createdDate <= Date2)
OR (pendingDate >= date Date1 AND pendingDate <= Date2))
Date1 and Date2 should be selected by the viewer in a control.
I have a control that filters by one of the dates, but I'm struggling to find a way to combine all 4 dates using an "OR" operator. I've tried using filters, metrics, and parameters but I haven't been able to get any working properly. Is there a way I can apply a SQL query similar to the one above to a scorecard?
Any ideas/advise is much appreciated!! I'm new to Data Studio so still getting a feel for how it all works. Thanks so much!
If the data source is Big Query, a custom query can be used and use in the where statement #DS_START_DATE and #DS_END_DATE.
Otherwise you need to blend the data with itself for each case.
Use the id to join the data set together. Use for each case the data field and add a metric id_1 , id_2 ... with the formula 1.
Then add a chart and a metric field:
case
when id_1 is not null then id
when id_2 is not null then id
else null end

Multi condition Count Expression in Visual Studio

I am trying to add some report logic to save setting up another dataset. First thing I need is to count the number of rows per date, then number of rows per date where another column states = "Yes".
I have a raw data stored procedure which returns the data as a table, on another tab.
Number of records that match date parameter:
=SUM(IIF(Fields.DATE_FIELD.Value = Parameters!DATE_PARAM.Value, 1, 0))
Number of records that match date parameter where another column = YES:
=SUM(IIF(Fields.DATE_FIELD.Value = Parameters!DATE_PARAM.Value AND Fields.OTHER_FIELD.Value = "Yes", 1, 0))
The IIF checks the conditions and if they are TRUE then return 1 for a record else 0 then it sums these values up.
UPDATE:
I think I misunderstood what you were needing.
You want to have a count of all records by date not just for a particular date.
For that you would want a table that groups by date, then you could just use a COUNTROWS or SUM(1) for the number of records for each date.
Then you could use an IIF to check for the Yes in the other field:
=SUM(IIF(Fields.OTHER_FIELD.Value = "Yes", 1, 0))
Since your table is already grouping by date, you don't need the date in the above expression.

SSRS date/time parameter

I have a ssrs report that has a date/time parameter that allows the user to select the date when running report. how to I get it to exclude the time part of the date field.
currently the field in the db is a date/time field so when I run query
select count(*) from table where date <= #dateparameter
it is not including records where the time part of field is greater than 00.00.00
how can I ignore the time part so all records are returned for that date
The simplest (and probably best performance) solution would be to add a day to the date passed by the user amd change the <= to <:
select count(*) from table where date < DATEADD(DAY, 1, #dateparameter)

SQL Server Retrieving Recurring Appointments By Date

I'm working on a system to store appointments and recurring appointments. My schema looks like this
Appointment
-----------
ID
Start
End
Title
RecurringType
RecurringEnd
RecurringTypes
---------------
Id
Name
I've keeped the Recurring Types simple and only support
Week Days,
Weekly,
4 Weekly,
52 Weekly
If RecurringType is null then that appointment does not recur, RecurringEnd is also nullable and if its null but RecurringType is a value then it will recur indefinatly. I'm trying to write a stored procedure to return all appointments and their dates for a given date range.
I've got the stored procedure working for non recurring meetings but am struggling to work out the best way to return the recurrences this is what I have so far
ALTER PROCEDURE GetAppointments
(
#StartDate DATETIME,
#EndDate DATETIME
)
AS
SELECT
appointment.id,
appointment.title,
appointment.recurringType,
appointment.recurringEnd,
appointment.start,
appointment.[end]
FROM
mrm_booking
WHERE
(
Start >= #StartDate AND
[End] <= #EndDate
)
I now need to add in the where clauses to also pick up the recurrences and alter what is returned in the select to return the Start and End Dates for normal meetings and the calculated start/end dates for the recurrences.
Any pointers on the best way to handle this would be great. I'm using SQL Server 2005
you need to store the recurring dates as each individual row in the schedule. that is, you need to expand the recurring dates on the initial save. Without doing this it is impossible to (or extremely difficult) to expand them on the fly when you need to see them, check for conflicts, etc. this will make all appointments work the same, since they will all actually have a row in the table to load, etc. I would suggest that when a user specifies their recurring date, you make them pick an actual number of recurring occurrences. When you go to save that recurring appointment, expand them all out as individual rows in the table. You could use a FK to a parent appointment row and link them like a linked list:
Appointment
-----------
ID
Start
End
Title
RecurringParentID FK to ID
sample data:
ID .... RecurringParentID
1 .... null
2 .... 1
3 .... 2
4 .... 3
5 .... 4
if in the middle of the recurring appointments schedule run, say ID=3, they decide to cancel them, you can follow the chain and delete the remaining ID=3,4,5.
as for expanding the dates, you could use a CTE, numbers table, while loop, etc. if you need help doing that, just ask. the key is to save them as regular rows in the table so you don't need to expand them on the fly every time you need to display or evaluate them.
I ended up doing this by creating a temp table of everyday between the start and end date along with their respective day of the week. I limited the recurrence intervals to weekdays and a set amount of weeks and added where clauses like this
--Check Week Days Reoccurrence
(
mrm_booking.repeat_type_id = 1 AND
#ValidWeeklyDayOfWeeks.dow IN (1,2,3,4,5)
) OR
--Check Weekly Reoccurrence
(
mrm_booking.repeat_type_id = 2 AND
DATEPART(WEEKDAY, mrm_booking.start_date) = #ValidWeeklyDayOfWeeks.dow
) OR
--Check 4 Weekly Reoccurences
(
mrm_booking.repeat_type_id = 3 AND
DATEDIFF(d,#ValidWeeklyDayOfWeeks.[Date],mrm_booking.start_date) % (7*4) = 0
) OR
--Check 52 Weekly Reoccurences
(
mrm_booking.repeat_type_id = 4 AND
DATEDIFF(d,#ValidWeeklyDayOfWeeks.[Date],mrm_booking.start_date) % (7*52) = 0
)
In case your interested I built up a table of the days between the start and end date using this
INSERT INTO #ValidWeeklyDayOfWeeks
--Get Valid Reoccurence Dates For Week Day Reoccurences
SELECT
DATEADD(d, offset - 1, #StartDate) AS [Date],
DATEPART(WEEKDAY,DATEADD(d, offset - 1, #StartDate)) AS Dow
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY s1.id) AS offset
FROM syscolumns s1, syscolumns s2
) a WHERE offset <= DATEDIFF(d, #StartDate, DATEADD(d,1,#EndDate))
Its not very elegant and probably very specific to my needs but it does the job I needed it to do.

Resources