balajij8
Esteemed Contributor II

@treesloth You can handle it by pushing the dynamic grouping down to the dataset level using a query parameter in the dashboard. You can use a parameterized SQL query that uses a CASE expression evaluating a granularity parameter switching between the function to aggregate by month and casting the timestamp directly to a DATE for daily granularity. It outputs a single pre-aggregated period column that you can feed into the visual.

SELECT
  'Monthly' AS granularity
UNION ALL
SELECT
  'Daily'

You can define a string parameter on the main dataset (:granularity, default to Monthly with allowed values Monthly and Daily). In the query, apply a conditional CASE statement on the timestamp column to yield the period column, then aggregate the metrics and group by that period. Alongside it, create a secondary lookup dataset with the two rows (Monthly and Daily) to populate the filter UI.

SELECT
  CASE WHEN :granularity = 'Monthly' THEN DATE_TRUNC('month', files_datetime) ELSE CAST(files_datetime AS DATE) END AS files_period, 
  SUM(fares) AS files_fare, 
  COUNT(*) AS files_count 
FROM 
  workspace.files.files_fares 
WHERE 
  files_datetime >= '2016-01-01' 
  AND files_datetime < '2016-02-01'

Configure a single-select filter using the seeded parameter control pattern on the dashboard. Wire the filter's field encoding to the lookup dataset to display the clean labels and bind its parameter encoding to the :granularity parameter on the primary dataset.

Now set up the bar chart with the period column on the x-axis ensuring the scale type is set to temporal so dates sort and scale chronologically and the aggregated measure on the y-axis. When you switch the filter between Monthly and Daily, the dataset re-executes with the selected parameter value, automatically switching the chart's granularity without distorting the bar widths