Sum values associated with a date equal to or less than today in Google Sheets [duplicate] - date

This question already has an answer here:
How to compare dates or date against today with query on google sheets?
(1 answer)
Closed 5 months ago.
So I have a table that looks like this:
date
goal
10/1/2022
10000
10/2/2022
10000
10/3/2022
10000
10/4/2022
10000
10/5/2022
10000
10/6/2022
10000
I would like to create a formula that Sums the goal column for the dates less than or equal to the current day. I currently have this:
=query(A4:B1000, "select SUM(B) WHERE A <= today()")
But this is throwing the following error: Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "(" "( "" at line 1, column 31. Was expecting one of: <EOF> "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "and"
Any thoughts on how to proceed would be helpful. Thanks!

This is a typical use case for SUMIF, which will sum values in a range that meet a condition (a condition on that same range, or on a corresponding range):
=sumif(A4:A1000,"<="&TODAY(),B4:B1000)
will sum the values in B4:B1000 for which the corresponding value in A4:A1000 is less than or equal to today.

Or you can still use a query if you prefer as long as you get the right syntax - see this for example.
=query(A2:B1000, "select SUM(B) WHERE A <= date '"& text(today(),"yyyy-mm-dd")&"'")
Dates in my locale are in dd/mm/yyyy format so 6/10/2022 is tomorrow at time of writing.

Related

Syntax for Combining BETWEEN and LIKE

I have a syntax, but it doesn't work.
Here is my query:
SELECT *
FROM aqua.reading
WHERE
CAST(reading.pres_date AS VARCHAR)
BETWEEN LIKE '2022-10-18%' AND LIKE '2022-10-18%'
it says:
ERROR: type "like" does not exist
LINE 1: ... WHERE CAST(reading.pres_date AS VARCHAR) BETWEEN LIKE '2022...
^
SQL state: 42704
Character: 77
I am trying to get all the data with timestamp and timezone and implement a date range
Don't compare dates (or timestamps) as strings. Compare them to proper date (or timestamp) values. Given the fact that you use the same "date" but with a wildcard at the end, I am assuming(!) that pres_date is in fact a timestamp column and you want to find all rows with a given date regardless of the time value of the timestamp.
The best approach is to use a range query with >= ("greater than or equal) on the lower value and < (strictly lower than) on the next day:
SELECT *
FROM aqua.reading
WHERE reading.pres_date >= DATE '2022-10-18'
AND reading.pres_date < DATE '2022-10-19'
Alternatively you can cast the timestamp to a date and use the = operator if you really want to pick just one day:
SELECT *
FROM aqua.reading
WHERE cast(reading.pres_date as DATE) = DATE '2022-10-18'
However that will not make use of a potential index on pres_date so is likely to be slower than the range query from the first solution

Tableau: How do I convert date to month-week #?

I have a date column. For example, the date is 8/2/2022, I'm hoping to see that this will convert to Aug Week 1; and if let's say the date is 7/15/2022, then the result should say July Week 3.
Please help, thanks!
You need to use LOD to do so, as default, you can only do week numbers by year in tableau, you need to create two calculated field to do so:
1.Create a [Min-Week] calculation field as shown below:
{ FIXED MONTH([Date]):MIN(DATEPART('week',[Date]))}
Note the calculation above for [Min-Week] assumes that there is at least one record for the first week of each month. If your data does not allow you to rely on that assumption, you could instead use DATEPART('week', DATETRUNC('month', [Date]))
2.Create a [Week-By-Mth] calculation field as shown below :
CASE DATEPART('week',[Date]) WHEN [Min-Week] THEN "Week 1" WHEN [Min-Week]+1 THEN "Week 2" WHEN [Min-Week]+2 THEN "Week 3" WHEN [Min-Week]+3 THEN "Week 4" WHEN [Min-Week]+4 THEN "Week 5" WHEN [Min-Week]+5 THEN "Week 6" END
EDIT good answer - here's just a shorter equivalent expression for [Week-By-Mth]
"Week " + STR(DATEPART('week, [Date]) - [Min-Week] + 1)
If you prefer an integer to a string, you could simplify further by getting rid of the Week prefix and the STR() call.
The [Week-By-Mth] calculation field should be what you need.

SSRS exact month difference

This may have been asked before but I've not been able to find it having searched! In Oracle SQL there's a function called MONTHS_BETWEEN which returns a fractional value if the two dates you are comparing are not both the first day of the month for example. I need to do something similar in SSRS report builder, I've tried using DateDiff("m",Date1,Date2) however this always returns an integer and I think from what I can tell it just compares the two months from the dates, so when I compare 30/09/20 and 01/04/21 I get 7 months when actually it is much closer to 6.
Is there a function or a fix that can be used in SSRS to get that more accurate value?
Thank you!
For example I would like to get the following result:
Difference between 30/09/20 and 01/04/21 = 6.1
Difference between 01/08/20 and 30/09/20 = 1.9
It doesn't have to super accurate as I will be rounding to the nearest integer but I'm looking for something that will recognise that in the second example nearly 2 months have been covered and in the first example it's only just over 6 months.
If you only need an approximation then you could just calculate the number of days difference and divide by 30.
using the following expression...
=DATEDIFF("d", Fields!startDate.Value, Fields!endDate.Value)/30
I put a few more examples into a table and got the following results.
The following code mimics oracle definition of months_between
Public Function MonthsBetween( d1 As Date, d2 As Date) As Decimal
Dim df As Decimal
df = DateDiff("m", d1, d2)
If Day(d1) <> Date.DaysInMonth(Year(d1), Month(d1)) Or Day(d2) <> Date.DaysInMonth(Year(d2), Month(d2)) Then
df = df + Cdec((Day(d2)-Day(d1))/31)
End If
Return df
End Function
Integer result when both dates are last day of month
Negative result when date1 > date2
Decimal part based on 31 days month
For your expression use something like
=Code.MonthsBetween(Fields!date1.Value , Fields!date2.Value)
UPDATE
The following expression works in the same manner
= Cdec(
DateDiff("m", Fields!date1.Value, Fields!date2.Value)
+
Iif (
Day(Fields!date1.Value) <> Date.DaysInMonth(Year(Fields!date1.Value), Month(Fields!date1.Value)) Or
Day(Fields!date2.Value) <> Date.DaysInMonth(Year(Fields!date2.Value), Month(Fields!date2.Value)) ,
Cdec( (Day(Fields!date2.Value) - Day(Fields!date1.Value))/31),
0.0
)
)

How to count events after set date with COUNTIFS?

I'm creating this report in spreadsheet
https://docs.google.com/spreadsheets/d/1U48MybVshKT3eRYE9goRCab9k1KCoXe7Zsr9Ogkkm4Y/edit?usp=sharing
In "BD" you can find the records and in "NB-month" the KPI that I want to analyze; I would like to create DATE filter for the numbers in the columns, ex for the col: "nÂș Contract closed" I'm thinking to use this formula:
=COUNTIFS(BD!R:R;A5;BD!G:G;">"&DATE(B2))
but I get a value: "0" that is incorrect because for this agent "Carla Vaello" the number of contracts closed after 2020-02-01 should be "2".
your usage of DATE formula is wrong (DATE requires 3 parameters). either change DATE to DATEVALUE or use:
=COUNTIFS(BD!R:R; A5; BD!G:G; ">"&B2)

Tableau - Filter based on parameter

I have a dataset of dates. It has just one column CreatedOnDate and its values are in datetime as shown below.
This dataset has 6 months of values as shown. I have a parameter called Report Type which has possible values Monthly, Weekly, Daily (Screenshot below)
I have created a calculated field (called Created On Date) which converts the date based on Report Type selected. The formula is shown below
CASE [Report Type]
WHEN "Monthly" THEN DATENAME('month', [CreatedOnDate])
WHEN "Weekly" THEN "Week " + STR(DATEPART('week',[CreatedOnDate]))
WHEN "Daily" THEN STR(MONTH([CreatedOnDate])) + "/" + STR(DAY([CreatedOnDate])) + "/" + STR(YEAR([CreatedOnDate]))
END
This works perfectly. The result of the calculated field is shown below.
I now need to incorporate the following logic
IFF Report Type = "Daily" Display only the last 30 days in the dataset
Other cases Show all values
How do I achieve this?
woodhead92, I'd suggest using so called Level of Detail expressions that were introduced in Tableau v8. First create a calculated field that will calculate the most recent (=MAX) date available:
{FIXED : MAX(CreatedOnDate) }
Let's call this MaxDate LOD. Then adding a new calculated field Show/Hide:
IF [Report Type] = "Daily" AND
([CreatedOnDate] >= DATEADD('day', -30, [MaxDate LOD]) THEN 'Show'
ELSEIF [Report Type] = "Weekly" OR [Report Type] = "Monthly" THEN 'Show'
ELSE 'Hide'
END
Add this filter and select 'Show' value only. I am assuming that you want to see all dates when Weekly/Monthly date granularity is selected - if that's not the case, simply add more ELSEIF conditions.
The formula above could be simplified, but I wanted to make it as verbose as possible so that it helps you understand how Level of Detail expressions work.
One thing to keep in mind - FIXED LOD calculation overwrites filters, so if you have a date-range filter available, you will have to make sure it's added to context. More details on filter context are available here in this a bit out-dated, but still excellent blog post.
Create a calculated field for your condition and then place it on the filter shelf to include only rows that evaluate to true.
[Report Date] <> "Daily" or
datediff('day', [CreatedOnDate], { max[CreatedOnDate] } < 30
The Curley braces are significant, an LOD calculation