dbt-labs / dbt_metrics

Macros for calculating metrics
Apache License 2.0
212 stars 45 forks source link

[SEMANTIC-97] [Feature] Support alias for date_{{grain}} output field #194

Closed apd-bscally closed 1 year ago

apd-bscally commented 1 year ago

Is this your first time submitting a feature request?

Describe the feature

The name of the date field that is output from the metrics.calculate macro varies depending on the time grain specified - for example, when grain is set to 'year', the date field in the output is called 'date_year'. While this is no doubt useful for end-users to understand the time grain specified, there are some situations where optionally allowing this field to be aliased could be beneficial.

For example:

Having the option to alias the date field would get around this problem

Describe alternatives you've considered

It's possible to circumvent this issue with a query like

select $1 as date, * from {{ metrics.calculate( ... ) }}

where you make an assumption that the date field will always be in the first field position. However as @callum-mcdata pointed out, for some configurations this will not be true. Additionally, relying on field positions for processes is never a best practice.

Who will this benefit?

This will benefit anybody using or creating an integration with dbt Metrics, allowing their integrations to be more stable. This issue arose for me in Tableau for example, so any and all Tableau users connecting to a Metrics query could benefit from this feature and avoid a potentially brittle workaround. For anybody building an integration, it could make their integration more stable by avoiding workarounds.

Are you interested in contributing this feature?

No

Anything else?

https://getdbt.slack.com/archives/C02CCBBBR1D/p1669746957363789

callum-mcdata commented 1 year ago

@apd-bscally this has been added as part of the 1.4 release candidate 2! Thank you for the wonderful suggestion.