Hi is there anyway to build a metric that return t...
# gooddata-cloud
c
Hi is there anyway to build a metric that return the number of days in the current date period? I am trying to work out a percentage of days with booking against total days there could be booking. I can't use the booking date as I can't guarantee that will have dates at the beginning or end of the period
👀 1
m
Hi Chris, I am not sure if I understood completely, but please allow me to refer our documentation related the DATETIME_DIFF function, please check the Example 2. Give it a try and let us to know.
c
Hi DATETIME_DIFF works fine but what I need is something that will give the time between start of and end of period. For example if I have "This Month" selected I would want to see 31 days come from the metric if I do a date diff on the min and max dates on the data in the table it might not bring me the correct number as the minimum date could be on the 2nd of the month
m
Got it, can you share please, which MAQL metric you have tried using MIN() and MAX()?
j
Hi Chris, I can see that your metric is defined as follows:
Copy code
select sum(DATETIME_DIFF(MIN({label/Hire_Start_Date.dayOfMonth}),MAX({label/Hire_Start_Date.day}) ))
using
DATETIME_DIFF(MIN(date), MAX(date))
will only give you the difference between the earliest and latest dates in the data, which can undercount days if, for example, there's no data on the 1st of the month. There are a few options to achieve your goal — calculating the full number of days in the selected period, like returning 31 for July even if data starts on July 2nd: Option 1: Use a reference dataset with precomputed values You can create a simple dataset that maps each month (or filter period) to the correct number of days. Then link that dataset in your model and create a metric like:
SELECT MAX({fact/days_in_month})
This ensures it always returns the correct number, regardless of whether there’s data on the first or last day. Option 2: Dashboard plugin (advanced) If you’re embedding the dashboard, a plugin can read the active date filter and dynamically calculate the difference between its start and end. This value can then be shown in a KPI or text widget. It’s more complex but allows full control. Option 3: Multiple metrics to define the min and max dates first, and then use those in a separate metric to calculate the
DATETIME_DIFF
. Here’s how you could approach it: Define the min and max date metrics:
Copy code
-- Min Date in filter context
SELECT MIN({label/Hire_Start_Date.date})

-- Max Date in filter context
SELECT MAX({label/Hire_Start_Date.date})
Save these as separate metrics, for example:
Min Hire Date
and
Max Hire Date
. 2. Use those metrics in a DATETIME_DIFF metric:
Copy code
SELECT DATETIME_DIFF({metric/Min Hire Date}, {metric/Max Hire Date})
Just a heads-up though: this will still return the difference based on data, not necessarily the full filter range — meaning it will only work correctly if there’s data present on both boundary dates. If your goal is to calculate the difference based on the selected filter period, regardless of whether data exists on the 1st or last day, then one of the following alternatives is better: • Use a reference table mapping periods to total days • Use a dashboard plugin to extract the filter range dynamically
c
Thanks I think A reference table is probably the way to go