Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs-mintlify/docs/data-modeling/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ measure via an API, the following SQL will be generated:

```sql
SELECT
100.0 * COUNT(
1.0 * COUNT(
CASE WHEN (users.paying = 'true') THEN users.id END
) / COUNT(users.id) AS paying_percentage
FROM users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ within the `measures` block.
```yaml
- name: completed_percentage
type: number
sql: "(100.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
sql: "(1.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
format: percent
```

Expand Down Expand Up @@ -156,7 +156,7 @@ cubes:
- name: completed_percentage
type: number
sql: "(100.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
sql: "(1.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
format: percent
```

Expand Down
4 changes: 2 additions & 2 deletions docs-mintlify/recipes/data-modeling/cohort-retention.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ cubes:
- users.email

- name: percentage_of_active
sql: "100.0 * {total_active_count} / NULLIF({total_count}, 0)"
sql: "1.0 * {total_active_count} / NULLIF({total_count}, 0)"
type: number
format: percent
drill_members:
Expand Down Expand Up @@ -168,7 +168,7 @@ cube(`monthly_retention`, {
},

percentage_of_active: {
sql: `100.0 * ${total_active_count} / NULLIF(${total_count}, 0)`,
sql: `1.0 * ${total_active_count} / NULLIF(${total_count}, 0)`,
type: `number`,
format: `percent`,
drill_members: [
Expand Down
8 changes: 4 additions & 4 deletions docs-mintlify/recipes/data-modeling/event-analytics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ cubes:
- - sql: "{is_bounced} = 'True'

- name: bounce_rate
sql: "100.00 * {bounced_count} / NULLIF({count}, 0)"
sql: "1.0 * {bounced_count} / NULLIF({count}, 0)"
type: number
format: percent
```
Expand Down Expand Up @@ -770,7 +770,7 @@ cube("sessions", {
},

bounce_rate: {
sql: `100.00 * ${bounced_count} / NULLIF(${count}, 0)`,
sql: `1.0 * ${bounced_count} / NULLIF(${count}, 0)`,
type: `number`,
format: `percent`
}
Expand Down Expand Up @@ -846,7 +846,7 @@ cube("sessions", {

repeat_percent: {
description: `Percent of Repeat Sessions`,
sql: `100.00 * ${repeat_count} / NULLIF(${count}, 0)`,
sql: `1.0 * ${repeat_count} / NULLIF(${count}, 0)`,
type: `number`,
format: `percent`
}
Expand Down Expand Up @@ -875,7 +875,7 @@ cubes:

- name: repeat_percent
description: Percent of Repeat Sessions
sql: "100.00 * {repeat_count} / NULLIF({count}, 0)"
sql: "1.0 * {repeat_count} / NULLIF({count}, 0)"
type: number
format: percent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const createPercentageMeasure = (status) => ({
sql: (CUBE) =>
`ROUND(${CUBE[`total_${status}_orders`]}::NUMERIC / ${
CUBE.total_orders
}::NUMERIC * 100.0, 2)`
}::NUMERIC, 2)`
Comment on lines 42 to +44
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential precision loss: With the old code, ROUND(ratio * 100.0, 2) gave 2 decimal places in the percentage space (e.g., 33.33). Now ROUND(ratio, 2) rounds the raw ratio to 2 decimal places (e.g., 0.33), and d3's .2% format will display it as 33.00% instead of 33.33%.

Since the format: percent (d3 .2%) already handles rounding when displaying, ROUND is arguably unnecessary now. But if you want to keep it for clean SQL output, consider increasing the precision:

Suggested change
`ROUND(${CUBE[`total_${status}_orders`]}::NUMERIC / ${
CUBE.total_orders
}::NUMERIC * 100.0, 2)`
}::NUMERIC, 2)`
`ROUND(${CUBE[`total_${status}_orders`]}::NUMERIC / ${
CUBE.total_orders
}::NUMERIC, 4)`

}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ cubes:

- name: completed_percentage
type: number
sql: "(100.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
sql: "(1.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
format: percent
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ cubes:

- name: completed_percentage
type: number
sql: "(100.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
sql: "(1.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
format: percent
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ cube(`users`, {
},

purchases_to_users_ratio: {
sql: `100.0 * ${orders.purchases} / ${CUBE.count}`,
sql: `1.0 * ${orders.purchases} / ${CUBE.count}`,
type: `number`,
format: `percent`
}
Expand Down
2 changes: 1 addition & 1 deletion docs/content/product/data-modeling/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ measure via an API, the following SQL will be generated:

```sql
SELECT
100.0 * COUNT(
1.0 * COUNT(
CASE WHEN (users.paying = 'true') THEN users.id END
) / COUNT(users.id) AS paying_percentage
FROM users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ cubes:
- users.email

- name: percentage_of_active
sql: "100.0 * {total_active_count} / NULLIF({total_count}, 0)"
sql: "1.0 * {total_active_count} / NULLIF({total_count}, 0)"
type: number
format: percent
drill_members:
Expand Down Expand Up @@ -168,7 +168,7 @@ cube(`monthly_retention`, {
},

percentage_of_active: {
sql: `100.0 * ${total_active_count} / NULLIF(${total_count}, 0)`,
sql: `1.0 * ${total_active_count} / NULLIF(${total_count}, 0)`,
type: `number`,
format: `percent`,
drill_members: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ cube("sessions", {
},

bounce_rate: {
sql: `100.00 * ${bounced_count} / NULLIF(${count}, 0)`,
sql: `1.0 * ${bounced_count} / NULLIF(${count}, 0)`,
type: `number`,
format: `percent`
}
Expand Down Expand Up @@ -770,7 +770,7 @@ cubes:
- - sql: "{is_bounced} = 'True'

- name: bounce_rate
sql: "100.00 * {bounced_count} / NULLIF({count}, 0)"
sql: "1.0 * {bounced_count} / NULLIF({count}, 0)"
type: number
format: percent
```
Expand Down Expand Up @@ -843,7 +843,7 @@ cube("sessions", {

repeat_percent: {
description: `Percent of Repeat Sessions`,
sql: `100.00 * ${repeat_count} / NULLIF(${count}, 0)`,
sql: `1.0 * ${repeat_count} / NULLIF(${count}, 0)`,
type: `number`,
format: `percent`
}
Expand Down Expand Up @@ -872,7 +872,7 @@ cubes:

- name: repeat_percent
description: Percent of Repeat Sessions
sql: "100.00 * {repeat_count} / NULLIF({count}, 0)"
sql: "1.0 * {repeat_count} / NULLIF({count}, 0)"
type: number
format: percent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const createPercentageMeasure = (status) => ({
sql: (CUBE) =>
`ROUND(${CUBE[`total_${status}_orders`]}::NUMERIC / ${
CUBE.total_orders
}::NUMERIC * 100.0, 2)`
}::NUMERIC, 2)`
Comment on lines 39 to +41
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same precision concern as the mintlify version — ROUND(ratio, 2) truncates to 2 decimal places before the frontend multiplies by 100, so 0.3333 becomes 0.33 → displayed as 33.00% instead of 33.33%.

Suggested change
`ROUND(${CUBE[`total_${status}_orders`]}::NUMERIC / ${
CUBE.total_orders
}::NUMERIC * 100.0, 2)`
}::NUMERIC, 2)`
`ROUND(${CUBE[`total_${status}_orders`]}::NUMERIC / ${
CUBE.total_orders
}::NUMERIC, 4)`

}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ within the `measures` block.
```yaml
- name: completed_percentage
type: number
sql: "(100.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
sql: "(1.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
format: percent
```

Expand Down Expand Up @@ -154,7 +154,7 @@ cubes:

- name: completed_percentage
type: number
sql: "(100.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
sql: "(1.0 * {CUBE.completed_count} / NULLIF({CUBE.count}, 0))"
format: percent
```

Expand Down
2 changes: 1 addition & 1 deletion examples/recipes/active-users/schema/ActiveUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cube(`ActiveUsers`, {

wauToMau: {
title: `WAU to MAU`,
sql: `100.000 * ${weeklyActiveUsers} / NULLIF(${monthlyActiveUsers}, 0)`,
sql: `1.0 * ${weeklyActiveUsers} / NULLIF(${monthlyActiveUsers}, 0)`,
type: `number`,
format: `percent`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const createPercentageMeasure = (status) => ({
format: `percent`,
title: `Percentage of ${status} orders`,
sql: (CUBE) =>
`ROUND(${CUBE[`Total_${status}_orders`]}::numeric / ${CUBE.totalOrders}::numeric * 100.0, 2)`,
`ROUND(${CUBE[`Total_${status}_orders`]}::numeric / ${CUBE.totalOrders}::numeric, 2)`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same precision concern as the docs: ROUND(ratio, 2) pre-truncates before the frontend's ×100 formatting. A raw ratio of 0.3333 becomes 0.33 → displayed as 33.00% rather than 33.33%. Consider ROUND(..., 4) to preserve two visible decimal places after the frontend multiplies, or remove the ROUND entirely and let the format: percent (d3 .2%) handle all rounding.

Suggested change
`ROUND(${CUBE[`Total_${status}_orders`]}::numeric / ${CUBE.totalOrders}::numeric, 2)`,
`ROUND(${CUBE[`Total_${status}_orders`]}::numeric / ${CUBE.totalOrders}::numeric, 4)`,

},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ${eventJoin.join('\nLEFT JOIN\n')}
shown: false
},
conversionsPercent: {
sql: (conversions, firstStepConversions) => `CASE WHEN ${firstStepConversions} > 0 THEN 100.0 * ${conversions} / ${firstStepConversions} ELSE NULL END`,
sql: (conversions, firstStepConversions) => `CASE WHEN ${firstStepConversions} > 0 THEN 1.0 * ${conversions} / ${firstStepConversions} ELSE NULL END`,
type: 'number',
format: 'percent'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cubes:
- sql: "{CUBE}.status = 'completed'"

- name: completed_percentage
sql: "({completed_count} / NULLIF({count}, 0)) * 100.0"
sql: "1.0 * {completed_count} / NULLIF({count}, 0)"
type: number
format: percent

Expand Down
Loading