Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d6d61a7

Browse files
docs: adds custom formatting to docs (#45)
* adds custom formatting to docs * removes subheaders from legacy formatting options * Update docs/references/metrics.mdx Co-authored-by: Javier Rengel Jiménez <[email protected]> * resolves comments * adds example format expressions * adds more examples * fixes errors in formatting currencies --------- Co-authored-by: Javier Rengel Jiménez <[email protected]>
1 parent c5f4e1e commit d6d61a7

File tree

2 files changed

+240
-112
lines changed

2 files changed

+240
-112
lines changed

docs/references/dimensions.mdx

Lines changed: 115 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ models:
5050
description: 'Total estimated revenue in GBP based on forecasting done by the finance team.'
5151
meta:
5252
dimension:
53-
type: string
53+
type: number
5454
label: 'Total revenue' # this is the label you'll see in Lightdash
5555
description: 'My custom description' # you can override the description you'll see in Lightdash here
5656
sql: 'IF(${TABLE}.revenue_gbp_total_est = NULL, 0, ${registered_user_email})' # custom SQL applied to the column from dbt used to define the dimension
5757
hidden: false
58-
round: 2
59-
format: 'gbp'
58+
format: '[$£]#,##0.00' # GBP rounded to two decimal points
6059
groups: ['finance']
6160
- name: forecast_date
6261
description: 'Date of the forecasting.'
@@ -76,11 +75,16 @@ models:
7675
dimension:
7776
type: date
7877
additional_dimensions:
79-
dimension_name_one:
78+
day_of_week_num:
8079
type: number
81-
label: 'Day of Week'
80+
label: 'Day of Week (number)'
8281
sql: 'day_of_week(${date})'
83-
dimension_name_two:
82+
day_of_week_day:
83+
type: date
84+
label: 'Day of Week (day)'
85+
sql: ${date}
86+
format: 'dddd'
87+
is_weekday_or_weekend:
8488
type: boolean
8589
label: 'Weekday or Weekend'
8690
sql: "case when day_of_week(${date}) < 5 then 'weekday' else 'weekend' end"
@@ -96,9 +100,7 @@ All the properties you can customize:
96100
| sql | No | string | Custom SQL applied to the column used to define the dimension. |
97101
| [time_intervals](#time-intervals) | No | `'default'` or `OFF` or an array[] containing elements of [date](#date-options), [numeric](#numeric-options) or [string](#string-options) options | `'default'` (or not setting the `time_intervals` property) will be converted into `['DAY', 'WEEK', 'MONTH', 'YEAR']` for dates and `['RAW', 'DAY', 'WEEK', 'MONTH', 'YEAR']` for timestamps; if you want no time intervals set `'OFF'`. |
98102
| hidden | No | boolean | If set to `true`, the dimension is hidden from Lightdash. By default, this is set to `false` if you don't include this property. |
99-
| round | No | number | Rounds a number to a specified number of digits |
100-
| [format](#format) | No | string | This option will format the output value on the result table and CSV export. Currently supports one of the following: `['km', 'mi', 'usd', 'gbp', 'eur', 'percent', 'id']` |
101-
| [compact](#compact-values) | No | string | This option will compact the number value (e.g. 1,500 to 1.50K). Currently supports one of the following: `['thousands', 'millions', 'billions', 'trillions']` |
103+
| [format](#format) | No | string | This option will format the output value on the results table and CSV export. Supports spreadsheet-style formatting (e.g. #,##0.00). Use [this website](https://customformats.com/) to help build your custom format.` |
102104
| [groups](#groups) | No | string or string[] | If you set this property, the dimension will be grouped in the sidebar with other dimensions with the same group label. |
103105
| [urls](#urls) | No | Array of { url, label } | Adding urls to a dimension allows your users to click dimension values in the UI and take actions, like opening an external tool with a url, or open at a website. You can use liquid templates to customise the link based on the value of the dimension. |
104106
| [required_attributes](#required-attributes) | No | Object with { user_attribute, value } | Limits access to users with those attributes |
@@ -185,7 +187,80 @@ and it will stay on multiple lines
185187

186188
You can also use dbt docs blocks in descriptions, [more on that here](/guides/cli/how-to-auto-generate-schema-files#using-doc-blocks-to-build-better-yml-files).
187189

188-
## Compact values
190+
## Format
191+
192+
You can use the `format` parameter to have your dimensions show in a particular format in Lightdash. Lightdash supports spreadsheet-style format expressions for all dimension types.
193+
194+
To help you build your format expression, we recommend using https://customformats.com/.
195+
196+
```yaml
197+
- name: us_revenue
198+
meta:
199+
dimension:
200+
type: number
201+
description: 'Revenue in USD, with two decimal places, compacted to thousands'
202+
format: '$#,##0.00," K"' # 505,430 will appear as '$505.43 K'
203+
additional_dimensions:
204+
percent_of_global_revenue:
205+
type: number
206+
description: 'Percent of total global revenue coming from US revenue.'
207+
sql: ${us_revenue} / ${global_revenue}
208+
format: '0.00%' # 0.67895243 will appear as '67.89%
209+
```
210+
211+
Example format expressions:
212+
213+
| Raw Value | Formatted value | Format expression |
214+
| ----------- | --------------- | ------------------ |
215+
| 100000.00 | 100,00.00 km | '#,##0.00" km"' |
216+
| 12345232 | 12345232 | '0' |
217+
| 56.7856 | 57 | '#,##0' |
218+
| 15430.75436 | $15,430.75 | '[$]#,##0.00' |
219+
| 15430.75436 | $15K | '[$]#,##0,"K"' |
220+
| 15430.75436 | $15.43K | '[$]#,##0.00,"K"' |
221+
| 13334567 | $13.33M | '[$]#,##0.00,,"M"' |
222+
| 0.6758 | 67.58% | '#,##0.00%' |
223+
224+
<details>
225+
<summary>(Legacy) format, round, and compact options</summary>
226+
227+
Spreadsheet-style format expressions are the recommended way of adding formatting to your metrics in Lightdash. There are legacy formatting options, listed below, which are less flexible than the spreadsheet-style formatting.
228+
229+
:::info
230+
231+
If you use both legacy and spreadsheet-style formatting options for a single dimension, Lightdash will ignore the legacy `format`, `compact`, and `round` options and only apply the spreadsheet-style formatting expression.
232+
233+
:::
234+
235+
#### Format (legacy)
236+
237+
```yaml
238+
version: 2
239+
240+
models:
241+
- name: sales_stats
242+
columns:
243+
- name: revenue
244+
description: 'Total estimated revenue in GBP based on forecasting done by the finance team.'
245+
meta:
246+
dimension:
247+
format: 'gbp'
248+
```
249+
250+
These are the options:
251+
252+
| Option | Equivalent format expression | Description | Raw value | Displayed value |
253+
| ------- | ------------------------------ | ----------------------------------------------------------------------------------- | ---------- | --------------- |
254+
| km | '#,##0.00" km"' | Adds the suffix `km` to your value | 10 | 10 km |
255+
| mi | '#,##0.00" mi"' | Adds the suffix `mile` to your value | 10 | 10 mi |
256+
| usd | '[$]#,##0.00' | Adds the `$` symbol to your number value | 10 | $10.00 |
257+
| gbp | '[$£]#,##0.00' | Adds the `£` symbol to your number value | 10 | £10.00 |
258+
| eur | '[$€]#,##0.00' | Adds the `€` symbol to your number value | 10 | €10.00 |
259+
| jpy | '[$¥]#,##0.00' | Adds the `¥` symbol to your number value | 10 | ¥10 |
260+
| percent | '#,##0.00%' | Adds the `%` symbol and multiplies your value by 100 | 0.1 | %10 |
261+
| id | '0' | Removes commas and spaces from number or string types so that they appear like IDs. | 12389572 | 12389572 |
262+
263+
#### Compact (legacy)
189264

190265
You can compact values in your YAML. For example, if I wanted all of my revenue values to be shown in thousands (e.g. `1,500` appears as `1.50K`), then I would write something like this in my .yml:
191266

@@ -197,15 +272,32 @@ You can compact values in your YAML. For example, if I wanted all of my revenue
197272
- name: revenue
198273
meta:
199274
dimension:
200-
compact: thousands # You can also use 'K'
275+
compact: thousands # You can also use 'K'
201276
```
202277

203-
| Value | Alias | Example output |
204-
| --------- | ------------------ | -------------- |
205-
| thousands | "K" and "thousand" | 1K |
206-
| millions | "M" and "million" | 1M |
207-
| billions | "B" and "billion" | 1B |
208-
| trillions | "T" and "trillion" | 1T |
278+
| Value | Alias | Equivalent format expression | Example output |
279+
| --------- | ------------------ | --------------------------------------- | -------------- |
280+
| thousands | "K" and "thousand" | '#,##0," K"' or '#,##0.00," K"' | 1K |
281+
| millions | "M" and "million" | '#,##0,," M"' or '#,##0.00,," M"' | 1M |
282+
| billions | "B" and "billion" | '#,##0,,," B"' or '#,##0.00,,," B"' | 1B |
283+
| trillions | "T" and "trillion" | '#,##0,,,," T"' or '#,##0.00,,,," T"' | 1T |
284+
285+
#### Round (legacy)
286+
287+
You can round values to appear with a certain number of decimal points.
288+
289+
```yaml
290+
version: 2
291+
models:
292+
- name: sales
293+
columns:
294+
- name: revenue
295+
meta:
296+
dimension:
297+
round: 0 # equivalent format expression: '#,##0.0'
298+
```
299+
300+
</details>
209301

210302
## Time intervals
211303

@@ -222,6 +314,12 @@ Lightdash breaks this out into the default intervals automatically. So, this is
222314

223315
![screenshot-default-intervals](assets/screenshot-default-intervals.png)
224316

317+
:::info
318+
319+
[Formatting](#format) added to a date or timestamp dimension will be applied to all of the time intervals for that dimension. If you want to apply different formats for different time intervals, we recommend creating [additional dimensions](#additional-dimensions) for time intervals where you want to customize the format.
320+
321+
:::
322+
225323
### By default, the time intervals we use are:
226324

227325
**Date**: ['DAY', 'WEEK', 'MONTH', 'YEAR']
@@ -295,35 +393,6 @@ You can see all of the interval options for date and timestamp fields below.
295393
| MONTH_NAME | Month name | String | March |
296394
| QUARTER_NAME | Quarter name | String | Q3 |
297395

298-
## Format
299-
300-
You can use the `format` parameter to have your dimensions show in a particular format in Lightdash.
301-
302-
```yaml
303-
- name: revenue
304-
description: 'Timestamp when the user was created.'
305-
meta:
306-
dimension:
307-
format: 'usd'
308-
metrics:
309-
total_revenue:
310-
type: sum
311-
format: 'usd'
312-
```
313-
314-
These are the options:
315-
316-
| Option | Description | Raw value | Displayed value |
317-
| ------- | ----------------------------------------------------------------------------------- | ---------- | --------------- |
318-
| km | Adds the suffix `km` to your value | 10 | 10 km |
319-
| mi | Adds the suffix `mile` to your value | 10 | 10 mi |
320-
| usd | Adds the `$` symbol to your number value | 10 | $10.00 |
321-
| gbp | Adds the `£` symbol to your number value | 10 | £10.00 |
322-
| eur | Adds the `€` symbol to your number value | 10 | €10.00 |
323-
| jpy | Adds the `¥` symbol to your number value | 10 | ¥10 |
324-
| percent | Adds the `%` symbol and multiplies your value by 100 | 0.1 | %10 |
325-
| id | Removes commas and spaces from number or string types so that they appear like IDs. | 12,389,572 | 12389572 |
326-
327396
## Groups
328397

329398
You can group your dimensions and metrics in the sidebar using the `groups` parameter.

0 commit comments

Comments
 (0)