在Genexus Query Viewer中如何基于行总计(而非列总计)显示百分比?
Hey there! I’ve worked with GeneXus Query objects extensively, so let me break down exactly how to configure your query to show the percentage each attraction contributes to its parent row total (using the continent/country/attraction example from the Genexus Query Usage Example docs).
Step 1: Confirm Your Base Query Setup
First, make sure your query already has:
- Grouping levels configured: Continent → Country → Attraction
- A numeric measure field (e.g.,
TotalVisitors,Revenue) that you’re aggregating for each attraction.
Step 2: Add a Row Total Calculation Field
You’ll need to calculate the total for the parent group (in this case, the total for the country each attraction belongs to) using GeneXus’s built-in RowTotal() function:
- In your Query object, navigate to the Calculations tab.
- Create a new calculated field with this formula:
ReplaceRowTotal(YourMeasureField)YourMeasureFieldwith your actual aggregated numeric field (likeTotalVisitors). Name this something clear, e.g.,CountryTotal.
Step 3: Calculate the Percentage
Next, create another calculated field to compute the attraction’s share of the row total:
- Add a second calculated field with this formula (to avoid division-by-zero errors):
IIF(RowTotal(YourMeasureField) = 0, 0, (YourMeasureField / RowTotal(YourMeasureField)) * 100)- The
IIFcheck handles cases where a country has no recorded data for the measure. - Name this field
AttractionPercentageor similar.
- The
- Set the field’s display format to Percentage (adjust decimal places in the format settings—2 decimal places works well for readability).
Step 4: Configure Query Output
- Go to the Output tab of your Query.
- Add the new
AttractionPercentagefield alongside your existing columns (Continent, Country, Attraction, YourMeasureField, CountryTotal). - Rearrange columns to match your desired layout (e.g., Attraction → YourMeasureField → AttractionPercentage → CountryTotal).
Step 5: Test and Verify
Run the query to confirm accuracy:
- For any attraction, its percentage should equal
(AttractionMeasure / CountryTotal) * 100 - Percentages for all attractions under a single country should sum to ~100% (accounting for minor rounding differences).
That’s it! This setup will give you the exact breakdown you’re looking for, matching the example result you referenced.
内容的提问来源于stack exchange,提问作者Rogelio Arosemena

