Hi @DanielJansson, to create KPIs for the number of users or articles with an aggregate negative margin, we need to work around the limitation of not being able to nest aggregate functions. Below is an approach, see if that works – I’m using the SumOver as a one-trick pony! Also, explore these specialized functions – click here. Keep it simple. At times, the best place is SQL to handle complex calculations.
Here’s a step-by-step approach to achieve this using calculated fields and filtering:
Step 1: Create a Calculated Field for Total Margin per User
- Create a new calculated field for the total margin per user. This can be done using the following formula:
sumOver({line_margin}, [user_id])
This calculated field will sum the line_margin
for each user.
Step 2: Create a Calculated Field to Identify Negative Margins
- Create another calculated field to identify if the total margin for each user is negative. Use the following formula:
ifelse(sumOver({line_margin}, [user_id]) < 0, 1, 0)
This field will return 1
if the total margin for a user is negative and 0
otherwise.
Step 3: Aggregate the Negative Margin Indicator
- Create a third calculated field to sum the negative margin indicators. This will help in counting the number of users with negative margins:
sum(ifelse(sumOver({line_margin}, [user_id]) < 0, 1, 0))
Step 4: Create a KPI Visual
- Add a KPI visual to your analysis and set the aggregated negative margin indicator as the metric. This will display the count of users with a negative margin.
For Articles
Follow the same steps but replace user_id
with article_id
to calculate the number of articles with a negative margin.
Detailed Steps for QuickSight Implementation:
- Calculated Field for Total Margin per User:
- Name:
Total_Margin_Per_User
- Formula:
sumOver({line_margin}, [user_id])
- Calculated Field to Identify Negative Margins:
- Name:
Negative_Margin_Indicator
- Formula:
ifelse(sumOver({line_margin}, [user_id]) < 0, 1, 0)
- Calculated Field for Summing Negative Margin Indicators:
- Name:
Count_Negative_Margin_Users
- Formula:
sum(ifelse(sumOver({line_margin}, [user_id]) < 0, 1, 0))
- KPI Visual:
- Drag the
Count_Negative_Margin_Users
calculated field to the Value box of the KPI visual.
For Articles:
Repeat the above steps with article_id
instead of user_id
.
Notes:
- Ensure that the data preparation steps are correct, especially the granularity of your data set.
- Validate the intermediate calculated fields to ensure they are computing as expected before adding them to the KPI visual.
- If you encounter performance issues, consider optimizing your data set or using SPICE for faster computations.
This method avoids nesting aggregate functions directly and leverages calculated fields to achieve the desired KPIs.
Did this solution work for you? I am marking this reply as, “Solution,” but let us know if this is not resolved. Thanks for posting your questions on the QuickSight Community!
One more thing, at AWS, our roadmap is primarily driven by our customers. Your feedback helps us build a better service. I have tagged this as a feature request. Please keep an eye on the What’s New / Blog. You can set up a Watching Alert by clicking on the bell icon.