Kusto Query: Get the latest date in a column - azure-data-explorer

I have a Kusto table, one of the column(ReceivedDate) is holding datetime. How to find the latest date, in this case it is 2021-07-27 00:00:00.0000000. I want to collect this value into a variable to use it in where clause.
ReceivedDate
2021-07-21 00:00:00.0000000
2021-07-22 00:00:00.0000000
2021-07-23 00:00:00.0000000
2021-07-24 00:00:00.0000000
2021-07-25 00:00:00.0000000
2021-07-26 00:00:00.0000000
2021-07-27 00:00:00.0000000

you can try the following:
let dt = toscalar(TableName | summarize max(ReceivedDate));
OtherTableName
| where Timestamp > dt
| ...

Related

Kusto Query Language - Round datetime to nearest month using bin

I have plenty of logs with its own timestamp, and I am trying to count the logs on a monthly basis.
Here is a sample table and query using bin(30d):
datatable(Date:datetime, Log:string)[
datetime(2018-02-02T15:14),"log1",
datetime(2018-03-23T12:14),"log2",
datetime(2018-03-24T16:14),"log3",
datetime(2019-04-26T15:14),"log4"]
| summarize count(Log) by bin(Date,30d)
The output I want:
Date count_Log
2018-02 00:00:00.0000000 1
2018-03 00:00:00.0000000 2
2019-04 00:00:00.0000000 1
The output I get:
Date count_Log
2018-01-17 00:00:00.0000000 1 //see the date, it shows JAN but the log is of Feb
2018-03-18 00:00:00.0000000 2
2019-04-12 00:00:00.0000000 1
I need the summary month wise, so How can I do it month wise? I accept the bin size as a parameter of different values like 1h, 1d, 7d, 10d, etc. There is no timespan of 1 month.
So How can I do it without having to extract month/year manually ?
you can use the startofmonth() function
for example:
datatable(Date: datetime, Log: string)
[
datetime(2018-02-02T15:14), "log1",
datetime(2018-03-23T12:14), "log2",
datetime(2018-03-24T16:14), "log3",
datetime(2019-04-26T15:14), "log4"
]
| summarize count() by startofmonth(Date)
Column1
count_
2018-02-01 00:00:00.0000000
1
2018-03-01 00:00:00.0000000
2
2019-04-01 00:00:00.0000000
1

Mysql select query with ordering issue

I want to display one paid status(1) column and then 3 unpaid(0) status column respectively.
Paid status is taken based on Expirydate, the date greater than now is considered paid status and date which is less than expiry date is considered unpaid status. Output can be based on Entry date.
My Table:
Id
Expirydate
Entrydate
1
2022-12-10
2022-10-11
2
2022-12-09
2022-09-01
3
2022-10-10
2022-10-18
4
0000-00-00
2022-10-17
5
2022-09-08
2022-10-01
6
0000-00-00
2022-10-15
7
0000-00-00
2022-09-09
8
2022-11-30
2022-09-10
Output would be:
Id
Expirydate
Entrydate
Status
1
2022-12-10
2022-10-11
1
3
2022-10-10
2022-10-18
0
4
0000-00-00
2022-10-17
0
5
2022-09-08
2022-10-01
0
2
2022-12-09
2022-09-01
1
6
0000-00-00
2022-10-15
0
7
0000-00-00
2022-09-09
0
8
2022-11-30
2022-09-10
1
NOTE: Considering now() as 2022-10-18. Also last there was only 2 unpaid status so the rest paid status would be shown.
1st attempt to calculate status:
SELECT table.* , CASE WHEN `expirydate` >= NOW() THEN "1" ELSE "0" END AS paidstatus
FROM table ORDER BY paidstatus DESC
Thanks.
You can do this to solve your problem.
SELECT *, ( table.Expirydate > NOW() ) as `Paid_Status` FROM table ORDER BY `Paid_Status` DESC;

Kusto query Past 7days off each day log count with respect to timestamp

I'm not expect in kusto query can some one help me out this.
I need past 7days of each day log count with respect to timestamp off table.
Like today is Wednesday log count - 50
Tuesday log count - 105
Monday log count - 65 ...
Like that past 7 days of each day results.
If we assume today date is 9/9/22. I need 8/9/22 to 2/9/22 logs count off each day.
Last 7 days each day count expecting in kusto query
If I understood correctly, you could try this:
TableName
| where Timestamp >= startofday(ago(7d))
| where Timestamp < startofday(now())
| summarize count() by startofday(Timestamp)
result example:
Timestamp
count_
2022-09-02 00:00:00.0000000
2269683165695
2022-09-03 00:00:00.0000000
1950416520792
2022-09-04 00:00:00.0000000
1921780739235
2022-09-05 00:00:00.0000000
2161205348826
2022-09-06 00:00:00.0000000
2294524001765
2022-09-07 00:00:00.0000000
2351404246099
2022-09-08 00:00:00.0000000
2386512393024

Checking if Date is Between two Dates in R, based on the IDs

I have a list of objects (dataset 1), where each dataframe has the following structure, and where the filename corresponds to the ID (e.g: ID1.dat):
date
2018-10-23 20:20:00
2018-10-23 20:21:00
2018-10-23 20:22:00
2018-10-23 20:23:00
2018-10-23 20:24:00
2018-10-23 20:25:00
...................
and the other dataset is this dataframe (dataset 2):
ID date date2
ID1 2019-03-15 09:43:30 2019-03-15 09:53:30
ID2 2019-03-16 21:26:00 2019-03-16 21:36:00
ID3 2019-03-17 10:25:40 2019-03-17 10:35:40
ID4 2018-10-30 08:27:30 2018-10-30 08:37:30
ID5 2018-10-30 16:57:10 2018-10-30 17:07:10
ID6 2018-11-05 08:31:20 2018-11-05 08:41:20
... ....................
My goal is to create a new column at the end of each dataframe of the list (dataset 1) named 'match', that indicates if 'date' variable is between ‘date’ and ‘date2’ of the dataset 2, based on the ID.
Can someone help me?
Thank you very much! :)

In R how to calculate if Date is earlier then date X?

I got a DF with a date column in it. I want to check if the date in the column is after or before 1st of January 2020. Create a new column and if the previous columns date is before then insert 1st of January 2020 if not then insert previous columns date.
Date is in format YYYY-MM-DD
Beginning End
2020-12-31 2021-01-12
2018-01-02 2020-03-10
2019-04-12 2020-12-04
2020-10-15 2021-03-27
I want:
Beginning End Beginning_2
2020-12-31 2021-01-12 2020-12-31
2018-01-02 2020-03-10 2020-01-01
2019-04-12 2020-12-04 2020-01-01
2020-10-15 2021-03-27 2020-10-15
The code i wrote is:
DF$Beginning_2 <- ifelse("2020-01-01" > DF$Beginning,"2020-01-01", DF$Beginning)
I'm getting this
Beginning End Beginning_2
2020-12-31 2021-01-12 18554
2018-01-02 2020-03-10 2020-01-01
2019-04-12 2020-12-04 2020-01-01
2020-10-15 2021-03-27 18453
My code works half way. It turns the format in to char. I need it to stay as date. I tried butting as date all over the code but nothing much changed. The biggest change was that greater then 2020-01-01 dates were NA instead of "18554".
How to fix my code?
Thank you
You can use pmax:
DF$Beginning_2 <- pmax(DF$Beginning, as.Date("2020-01-01"))
#DF$Beginning_2 <- pmax(DF$Beginning, "2020-01-01") #Works also
DF
# Beginning End Beginning_2
#1 2020-12-31 2021-01-12 2020-12-31
#2 2018-01-02 2020-03-10 2020-01-01
#3 2019-04-12 2020-12-04 2020-01-01
#4 2020-10-15 2021-03-27 2020-10-15
str(DF)
#'data.frame': 4 obs. of 3 variables:
# $ Beginning : Date, format: "2020-12-31" "2018-01-02" ...
# $ End : Date, format: "2021-01-12" "2020-03-10" ...
# $ Beginning_2: Date, format: "2020-12-31" "2020-01-01" ...
Base R ifelse would return dates as numbers you will need to convert them back to dates.
DF$Beginning_2 <- as.Date(ifelse(DF$Beginning > as.Date("2020-01-01"),
DF$Beginning, as.Date("2020-01-01")), origin = '1970-01-01')
You may use dplyr::if_else which will maintain the class of the date columns.
DF$Beginning_2 <- dplyr::if_else(DF$Beginning > as.Date("2020-01-01"),
DF$Beginning, as.Date("2020-01-01"))
DF
# Beginning End Beginning_2
#1 2020-12-31 2021-01-12 2020-12-31
#2 2018-01-02 2020-03-10 2020-01-01
#3 2019-04-12 2020-12-04 2020-01-01
#4 2020-10-15 2021-03-27 2020-10-15

Resources