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.
I have a data in Sheet1 with Col is A, B, C which C is date format YYYY-MM-DD HH:MM:SS, I need to query the data in Sheet1 where C is today date. My formula is
=query('Sheet1!A1:C',"Select * where C = Date'"&text(today(),"YYYY-MM-DD")&"')
But result return is empty even C is today date.
When I change the formula to this format then it works,
=query('Sheet1!A1:C',"Select * where C < Date'"&text(today()-1,"YYYY-MM-DD")&"'and C > Date'"&text(today()+1,"YYYY-MM-DD")&"')
I wonder what was wrong with my first formula when I use "=" in the query.
Thanks
The first formula never does any manipulation on C:C. This means that the equality filter only matches when the time is also 0:00:00 of the same day. You can use the toDate Query function to convert C like so:
=query(A1:C,"Select * where toDate(C) = Date'"&text(today(),"YYYY-MM-DD")&"'")
See the doc on scalar functions for documentation.
Related
I am pretty sure this is a small issue, but I just can´t figure out how to solve it, as I am new at using Apps script :(.
ISSUE:
I have a sheets consisting of 2 columns: A and B. The column A contains dates written in German format (Day/Month/Year). Example: October 18th, 2021 is written 18/10/2021. Now all I want to do is increment each day from column A by 1 day and write the outcome (so, the incremented days) in column B.
ATTEMPTED SOLUTION:
I´ve figured out how to increment the days and write the outcome in column B. But I am struggling to add the second part of my script, so that the incremented dates are written in German in column B (See scripts below).
QUESTION:
How and where can I include the “Utilities.formatDate()” into my code, so that the dates in column B are formatted in German? (So, DAY/MONTH/YEAR instead of MONTH/DAY/YEAR)?
Thank you so much in advance for your help.
// Increment dates in colum A by 1 day and write this incremented date in colum B
function incrementDateInGermanFormat() {
var ss = SpreadsheetApp.getActiveSheet();
var date = new Date(ss.getRange(1, 1).getValue());
ss.getRange(1, 2).setValue(new Date(date.setDate(date.getDate() + 1)));
}
// This is the line I´m having trouble to include in the script above
Utilities.formatDate(date, Session.getScriptTimeZone(), "dd/MM/yyyy")
It is bad practice to convert date to strings. In this case, a better alternative is to simply change column B's date format to german in the spreadsheet user interface or use .setNumberFormat("dd/mm/yyyy") on column B.
I am newbie to KDB. I have a KDB table which I am querying as:
select[100] from table_name
now this table has got some date columns which have dates stored in this format
yyyy.mm.dd
I wish to query that table and retrieve the date fields in specific format (like mm/dd/yyyy). If this would've been any other RDBMS table this is what i would have done:
select to_date(date_field,'mm/dd/yyyy') from table_name
I need kdb equivalent of above. I've tried my best to go through the kdb docs but unable to find any function / example / syntax to do that.
Thanks in advance!
As Anton said KDB doesn't have an inbuilt way to specify the date format. However you can extract the components of the date individually and rearrange as you wish.
For the example table t with date column:
q)t
date
----------
2008.02.04
2015.01.02
q)update o:{"0"^"/"sv'flip -2 -2 4$'string`mm`dd`year$\:x}date from t
date o
-----------------------
2008.02.04 "02/04/2008"
2015.01.02 "01/02/2015"
From right to left inside the function: we extract the month,day and year components with `mm`dd`year$:x before stringing the result. We then pad the month and day components with a null character (-2 -2 4$') before each and add the "/" formatting ("/"sv'flip). Finally the leading nulls are filled with "0" ("0"^).
Check out this GitHub library for datetime formatting. It supports the excel way of formatting date and time. Though it might not be the right fit for formatting a very large number of objects (but if distinct dates are very less then a keyed table and lj can be used for lookup).
q).dtf.format["mm/dd/yyyy"; 2016.09.23]
"09/23/2016"
q).dtf.format["dd mmmm yyyy"; 2016.09.03] // another example
"03 September 2016"
I don't think KDB has built-in date formatting features.
The most reliable way is to format date by yourself.
For example
t: ([]date: 10?.z.d);
update dateFormatted: {x: "." vs x; x[1],"/",x[2],"/",x[0]} each string date from t
gives
date dateFormatted
------------------------
2012.07.21 "07/21/2012"
2001.05.11 "05/11/2001"
2008.04.25 "04/25/2008"
....
Or, more efficient way to do the same formatting is
update dateFormatted: "/"sv/:("."vs/:string date)[;1 2 0] from t
now qdate is available for datetime parsing and conversion
I'm working on making a replica of sheet1 on to another sheet2 (same document), and query() worked fine until the column i want to filter are formula cells (LONG ones each with query, match, etc).
What i want to do is filter the rows in sheet1 where the event date in column M is upcoming (there are more filter conditions but just to simplify this is the main problem).
I don't want the rows where the date is either empty, in the past (various date formats), or where the formula give a result of empty string "".
The formulas i've tried (which gives error) - note i'm just selecting 2 columns for testing:
=query(sheet1!A3:N, " select I,M where I = 'Singapore' AND DATEVALUE(M)>TODAY() ",0)
=query(sheet1!A3:N, " select I,M where I = 'Singapore' AND M>TODAY() ",0)
This formula doesn't give error but doesnt show correct data - shows all data except Jan 2017 - August 7 2017:
=FILTER(sheet1!A3:N, sheet1!I3:I="Singapore", sheet1!M3:M>TODAY())
This formula gives empty output:
=query(sheet1!A3:N, " select I,M where I = 'Singapore' AND M='22 August' ",0)
There's no today() in Query. Use now() instead:
=query(sheet1!A3:N, " select I,M where I = 'Singapore' AND M > now() ",0)
Or if you want now() without time(equivalent to TODAY()), use:
todate(now())
For this to work, provided you have all the correct dates in M in any format, which Google sheets recognises (i.e., the formula bar shows the correct date or time) regardless of the actual string. If not, You should manually convert all those remaining dates to correct format. The correct format of date to be entered in the query formula is date 'yyyy-mm-dd'. It doesn't matter what format the date is in sheets ( as long as Google sheets recognises this), but the actual formula must only contain date in this format for comparison. For example , to find all dates less than 31,August, 2017,
=query(A2:B6, "select A where A < date '2017-08-31'")
You can use this to figure out all the dates, which Google doesn't recognise: N1:
M:M*1
If you get an error in the helper column N, then those dates are not recognised. Even if you did not get error, it is possible that Google sheets mis-recognizes the date. There is also a more specific function:
=ARRAYFORMULA(ISDATE(M:M))
References:
Scalar functions
I am trying to use a MAX function inside WHERE clause on Qlik Sense.
I have charged a calendar and I have to make the sum of a specific value filtering it by max month of the max year specified. If I do the following statement separately:
Max({<Year={"$(=max(Year))"}>}Month)
it shows me correctly the max month of each row.
Instead of this, if I try to use this filter to filter my value it shows me only null values. This is the expression that I am using by the moment:
Sum({<Month=Max({<Year={"$(=max(Year))"}>}Month)>}Import)
How can I filter by month of the last year specified?
Thanks in advance!
There's two thing that needs to be added for your expression to work: The dollar expansion for the whole max expression and an outer expression for the year (unless you want to sum for all months x for all years, where x is the largest month of the latest year)
sum({< Month = {"$(=max({<Year = {"$(=max(Year))"}>} Month}"},
Year = {"$(=max(Year))"} >} Import }
I want to calculate the number of days passed between past date and a current date. My past date is in the format dd/mm/yyyy format. I have used below mentioned formulas but giving the proper output.
=DAYS360(A2,TODAY())
=MINUS(D2,TODAY())
In the above formula A2 = 4/12/2012 (dd/mm/yyyy) and I am not sure whether TODAY returns in dd/mm/yyyy format or not. I have tried using 123 button on the tool bar, but no luck.
The following seemed to work well for me:
=DATEDIF(B2, Today(), "D")
DAYS360 does not calculate what you want, i.e. the number of days passed between the two dates – see the end of this post for details.
MINUS() should work fine, just not how you tried but the other way round:
=MINUS(TODAY(),D2)
You may also use simple subtraction (-):
=TODAY()-D2
I made an updated copy of #DrCord’s sample spreadsheet to illustrate this.
Are you SURE you want DAYS360? That is a specialized function used in the
financial sector to simplify calculations for bonds. It assumes a 360 day
year, with 12 months of 30 days each. If you really want actual days, you'll
lose 6 days each year.
[source]
Since this is the top Google answer for this, and it was way easier than I expected, here is the simple answer. Just subtract date1 from date2.
If this is your spreadsheet dates
A B
1 10/11/2017 12/1/2017
=(B1)-(A1)
results in 51, which is the number of days between a past date and a current date in Google spreadsheet
As long as it is a date format Google Sheets recognizes, you can directly subtract them and it will be correct.
To do it for a current date, just use the =TODAY() function.
=TODAY()-A1
While today works great, you can't use a date directly in the formula, you should referencing a cell that contains a date.
=(12/1/2017)-(10/1/2017) results in 0.0009915716411, not 61.
I used your idea, and found the difference and then just divided by 365 days. Worked a treat.
=MINUS(F2,TODAY())/365
Then I shifted my cell properties to not display decimals.
If you are using the two formulas at the same time, it will not work...
Here is a simple spreadsheet with it working:
https://docs.google.com/spreadsheet/ccc?key=0AiOy0YDBXjt4dDJSQWg1Qlp6TEw5SzNqZENGOWgwbGc
If you are still getting problems I would need to know what type of erroneous result you are getting.
Today() returns a numeric integer value: Returns the current computer system date. The value is updated when your document recalculates. TODAY is a function without arguments.
The following worked for me. Kindly note that TODAY() must NOT be the first argument in the function otherwise it will not work.
=DATEDIF( W2, TODAY(), "d")
Today() does return value in DATE format.
Select your "Days left field" and paste this formula in the field
=DAYS360(today(),C2)
Go to Format > Number > More formats >Custom number format and select the number with no decimal numbers.
I tested, it works, at least in new version of Sheets, March 2015.