In this section, we will learn how to create an expression to retrieve the sales amount of a specific country so that we can use this value to compare it against all other countries.
To start, we will add a Table visualization to the sheet, Table chart with dimension Country, and master measure Sales $.
Follow these steps to create the visualizations:
- Click on the Edit button to enter the edit mode.
- Click on the Charts button on the asset panel, which is on the left-hand side of the screen, and find the Table chart.
- Click on the Table chart, and then drag and drop it into the empty space at the top of the sheet:
- Click on the Add dimension button and select Country in the Dimensions section.
- Click on the Data heading in the properties panel to expand it.
- Find the Add column button, click on it, and choose Measure.
- Select Sales $ in the Measure section.
- Fix the measure Number formatting to show a number with two decimal places.
- Click on the Add column button again to add the second measure.
- Click on the fx button to open the expression editor, and then type the following expression for the new measure: sum({<Country={Brazil}>} SalesAmount).
- Click on the Apply button to close the expression editor and save the expression.
- In the Label of the measure, type Brazil Sales $.
- Fix the measure Number formatting to show a number with two decimal places.
- Click on the Done button to enter the visualization mode. The Table chart will look like this:
We can only see Brazil with its value. To create the calculation of the value on Brazil, the Sales $ column needs to be repeated along all other countries. We will fix this by adding the TOTAL qualifier, as follows:
- Click on the Edit button to enter the edit mode.
- Go to the properties panel and click on the Data heading to expand it.
- Find the Brazil Sales $ measure.
- Click on the fx button to open the expression editor, and then replace the expressions with the following: sum(TOTAL {<Country={Brazil}>} SalesAmount).
- Click on the Apply button to close the expression editor and save the expression.
- Find the Totals function property in the measure, and then select None to not display a value in the total heading. The Table chart will look like this:
To create the comparison calculation, add another measure column:
- Click on the fx button to open the expression editor, and then replace the expressions with the following: Column(1)-Column(2).
- Click on the Apply button to close the expression editor and save the expression.
- In the Label of the measure, type Variance.
- Fix the measure Number formatting to show a number with two decimal places.
- Click on the Done button to enter the visualization mode. The Table chart will look like this:
We learned how to use a Set Analysis modifier, {<Country={Brazil}>}, in the sum() function to summarize sales only for Brazil, but the resulting value was only applied to a single row. To repeat the result on all the other rows in the table, we used the TOTAL qualifier to disregard the dimension. To calculate the variance between Sales $ and Brazil's Sales $, we used the Column() function to reuse the calculations already created in the first two measures of the table, so we don't need to repeat all the formulas again.