$__timeGroup doesn't work correct. Grafana v8.2.6

Hello,

I need to group data by month using ‘created’ field for timeSeries. For this purpose I am using
$__timeGroupAlias(created,$__interval,0)
and
$__timeGroup(created,$__interval,0) + 14400 as time
The data are grouped but is not correct, e.g. part of January data is grouped in February. The table, graph view and query options on a screenshots below


Graph_view
Query_options
My query is
SELECT
$__timeGroup(created,$__interval,0) + 14400 as time,
COUNT(*),
SUM(CASE WHEN status = ‘Verified’ THEN 1 END) as Verified,
SUM(CASE WHEN status = ‘Opened’ THEN 1 END) as Opened,
SUM(CASE WHEN status = ‘In Progress’ THEN 1 END) as In_Progress,
SUM(CASE WHEN status = ‘In Grooming’ THEN 1 END) as In_Grooming,
SUM(CASE WHEN status = ‘Closed’ THEN 1 END) as Closed
FROM jira_metric_record1
Where
$__timeFilter(created) AND
labels LIKE ‘%prod_defect%’
GROUP BY 1
ORDER BY 1
The question is how to define correct months grouping?

THANK YOU!!

~ the grafana team

Hello,

What is the number “14400”? What database are you using? Why did you set the “Interval” as “30d”, is the data get generated/written to the DB each 30 days?

Hello,

  1. the “14400” was taken as workaround for __timeGroup according to the following discussion and it shows close to real data
    $__timeGroup timezone issue for SQL data sources · Issue #51525 · grafana/grafana · GitHub
    Using $__timeGroupAlias(created,$__interval,0) I got more discrepancies

  2. the data base is MySQL

  3. “Interval” as “30d” was calculated based on selected Min interval 1M.

  4. Yes, it supposed to generate data in the end of each month. For now loaded data for 1 year.
    the real records in table below

Oh the 14400 is -4 US timezone. Is that your timezone?

Did you try this

CONVERT_TZ(FROM_UNIXTIME(
UNIX_TIMESTAMP(CONVERT_TZ(created_at, ‘UTC’, ‘US/Eastern’)) DIV ($__interval_ms / 1000) * ($__interval_ms / 1000)
), ‘US/Eastern’, ‘UTC’) AS time,

1 Like

Thanks, applied your suggestion but got error: db query error: Error 1054: Unknown column ‘‘UTC’’ in ‘field list’
yes am in ET US time zone. The Timezone set Browser Time

is your datetime column in your database UTC?

You forgot to change the single quotes from " ‘ " to " ' ".

Thanks! updated and got No data

and second one question, why following works as expected in outside Grafana for table

DATE_FORMAT(created, ‘%Y-%m’) as time,

but doesn’t work in Grafana. I tried
FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_FORMAT(created ,‘%Y-%m’))) AS time
but got only one record for whole year with weird date

The DATE_FORMAT(created, ‘%Y-%m’) doesn’t work in Grafana because it is a string, but Grafana needs a timestamp column.

You can use this to convert the type to datetime, see if it works:

SELECT STR_TO_DATE(DATE_FORMAT(created, '%Y-%m-01'), '%Y-%m-%d')

Thanks, it groups by month, but there is some offset in data about on 1month, e.g. data for January went to December previous year


the query is
SELECT
STR_TO_DATE(DATE_FORMAT(created, ‘%Y-%m-01’), ‘%Y-%m-%d’) as time,
COUNT(*) AS num_defects,
COUNT(CASE WHEN status = ‘Opened’ THEN 1 ELSE NULL END) AS opened_defect,
COUNT(CASE WHEN status = ‘Verified’ THEN 1 ELSE NULL END) AS verified_defect,
COUNT(CASE WHEN status = ‘In Progress’ THEN 1 ELSE NULL END) AS in_progress,
COUNT(CASE WHEN status = ‘In Grooming’ THEN 1 ELSE NULL END) AS in_grooming,
COUNT(CASE WHEN status = ‘Closed’ THEN 1 ELSE NULL END) AS closed_defect
FROM
jira_metric_record_year
WHERE
labels LIKE ‘%prod_defect%’
AND created >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
GROUP BY
time
ORDER BY
time;

what data type is the created field? varchar, text, or actual time?

Is the data correct?

Because there is no timezone information stored with the timestamp, Grafana treats it as UTC and subtract 5 hours (US Eastern), so 2023-01-01 00:00:00 becomes 2022-12-31 19:00:00.

You need to convert the timezone within the query.

1 Like

That does not help us see the data type. if it is string data type (which it seems like it is) try

convert_tz(STR_TO_DATE(DATE_FORMAT(created, '%Y-%m-01'), '%Y-%m-%d'),'America/New York','UTC')

Hello,
I am sorry for late response, the line below brought only one entry in the table
convert_tz(STR_TO_DATE(DATE_FORMAT(created, ‘%Y-%m-01’), ‘%Y-%m-%d’),‘America/New York’,‘UTC’) as time,

Have you selected time series for the Format or Table?

I only selected UTC for dashboard, I don’t have Format dropdown in my query section in Grafana


1 Like

Please show us the whole query as well as the resulting data of that query

My query is

SELECT
STR_TO_DATE(DATE_FORMAT(created, '%Y-%m-01'), '%Y-%m-%d) as time,
COUNT(CASE WHEN status = 'Opened' THEN 1 ELSE NULL END) AS opened_defect,
COUNT(CASE WHEN status = 'Verified' THEN 1 ELSE NULL END) AS verified_defect,
COUNT(CASE WHEN status = 'In Progress' THEN 1 ELSE NULL END) AS in_progress,
COUNT(CASE WHEN status = 'In Grooming' THEN 1 ELSE NULL END) AS in_grooming,
COUNT(CASE WHEN status = 'Closed' THEN 1 ELSE NULL END) AS closed_defect
FROM
jira_metric_record1
WHERE
labels LIKE '%prod_defect%'
GROUP BY
time
ORDER BY
time;

it bring following graph

but in table view I see the January date shifted to December, and so on

and in Jaunury tips date is 2023-01-01 and yes, Time series selected