Sounds like you could make a derived table to calculate the total that youâll use in the percent of total calculation, and use bind_filters:
to limit the filters that are applied to that derived table.
Hereâs an example using dummy data:
view: total_profit_dt {
derived_table: {
explore_source: order_items {
column: profit_per_category { field: order_items.profit_total }
column: category { field: products.category }
# This will return total profit across all categories by running a window function
derived_column: total_profit {
sql: SUM(profit_per_category) OVER() ;;
}
# This will only carry through the filters that you tell it to, you can have as many
bind_filters: params as you need.
bind_filters: {
from_field: order_items.created_date
to_field: order_items.created_date
}
}
}
Then, you can make the percent_of_total calculation within this derived table view based on the numbers that are pulled into the derived table (only affected by the date filter):
dimension: profit_per_category {
label: "Order Items Total Profit"
description: "
Item's sale price minus its cost.
"
value_format: "$#,##0.00"
type: number
}
dimension: category {}
dimension: total_profit {
type: number
value_format: "$#,##0.00"
}
measure: percent_of_total {
type: number
sql: SUM(profit_per_category) / SUM(total_profit) ;;
value_format_name: percent_1
}
}
And lastly, youâd join the derived table back to the original explore on category, and only pull in the field you need, percent_of_total
:
# Using a refinement here, but you can add to the original explore
# Like you normally would, if you prefer!
explore: +order_items {
join: total_profit_dt {
sql_on: ${products.category} = ${total_profit_dt.category} ;;
relationship: many_to_one
type: left_outer
fields: [total_profit_dt.percent_of_total]
}
}
Now, the date field affects the total_profit
and percent_of_total
calculations, but the category filter doesnât affect them (I added the Total Profit field in for demo outside of the code Iâm sharing, but you probably wouldnât want this in production):
Full code for reference:
include: "/_layers/_basic.layer"
view: total_profit_dt {
derived_table: {
explore_source: order_items {
column: profit_per_category { field: order_items.profit_total }
column: category { field: products.category }
# This will return total profit across all categories by running a window function
derived_column: total_profit {
sql: SUM(profit_per_category) OVER() ;;
}
# This will only carry through the filters that you tell it to, you can have as many bind_filters: params as you need.
bind_filters: {
from_field: order_items.created_date
to_field: order_items.created_date
}
}
}
dimension: profit_per_category {
label: "Order Items Total Profit"
description: "
Item's sale price minus its cost.
"
value_format: "$#,##0.00"
type: number
}
dimension: category {}
dimension: total_profit {
type: number
value_format: "$#,##0.00"
}
measure: percent_of_total {
type: number
sql: SUM(profit_per_category) / SUM(total_profit) ;;
value_format_name: percent_1
}
}
explore: +order_items {
join: total_profit_dt {
sql_on: ${products.category} = ${total_profit_dt.category} ;;
relationship: many_to_one
type: left_outer
fields: [total_profit_dt.total_profit, total_profit_dt.percent_of_total]
}
}