Creating calculated columns and rows with Excel formulas
Excel formulas let you add calculated columns and row totals that do not exist in your original QlikView document or Qlik Sense app.
What you will do
You will:
- Add row totals using the SUM function.
- Add calculated columns. You can do this in two different ways:
-
Excel tables: You create a table using Excel table tools. This method results in better report creation performance, but requires more advanced Excel skills. We recommend using this approach.
- Levels: You insert field tags in a level, and then add Excel formulas. This is easy to implement, but using levels can impact report generation performance.
This page shows how to create calculated columns using both approaches. To demonstrate, a third column will be calculated by subtracting the values in one column from the values in another column. Then the derived values of the third column will be divided by the corresponding values from one of the original two columns.
This tutorial uses QlikView data that can be found in Sample files. You can also use your own QlikView or Qlik Sense data.
About calculations
If you want to minimize report creation time, you can add desired calculations to the QlikView or Qlik Sense reload script.
This example uses simple Excel formulas, but you can insert more complex formulas if needed. You can also insert Qlik formulas. See: Qlik Sense and QlikView formulas.
Adding an Excel table with calculated columns and totals
This is the first way to create calculated columns. You will add the source QlikView object to the Tables node, and then create an Excel table.
Adding the table
Do the following:
-
Create a new Excelreport, or open an existing one.
- Right-click the Tables node, and select Add objects.
-
Select a chart from the list. For example, add Top 10 Products. Click OK.
- Click + on the left to expand the CH184_Table - Top 10 Products table node.
- One at a time, select each column under CH184_Table - Top 10 Products, and clear Keep Source Formats.
-
Hold CTRL and select the table fields you want to add to the sheet.
For example: Product Name, Total Sales and Gross Profit. Drag them onto an empty row of cells.
- Select the cells that contain the heading, the field tags, and one blank row below.
- In the Excel ribbon, click the Insert tab, and then click Table.
-
Make sure that the My table has headers flag is enabled, and then click OK.
-
On the left pane, click Extras. Drag a deleterow tag onto a cell in the empty row below the table.
This will remove the empty row from the final report.
Adding the calculated column
Do the following:
- Select the first empty cell to the right of the field tags.
-
Insert the formula =[@[Total Sales]]-[@[Gross Profit]].
Excel will automatically add this new column to the table.
- Type a column heading. For example: Costs.
-
Then use the Excel table design tool to apply formatting.
Adding totals
Do the following:
- Select an empty cell below the table. For example, D8.
-
Enter a SUM formula in the cell. The range of the SUM function needs to include:
- The address of the cell in the row containing the level opening tag.
- The cell containing the field tag.
- The address of at least one empty cell immediately below it.
In this example, the function would read: =SUM(D4:7).
- Click and drag this formula to E8 and F8.
-
Apply Excel formatting tools to the elements of the row.
For example: Total Sales and Gross Profit totals could have the Currency format.
Previewing the report
Do the following:
-
Click Preview.
Excel launches and displays your report.
-
You will see a table, with the one column containing your calculated results. The bottom row will contain column totals.
- Click Save and Close to save the template, and close the Template Editor.
Using levels to build a table with calculated columns and totals
This is the second way to add calculated columns, using levels. To learn about levels, see: Excel reports with levels.
Adding the level
Do the following:
- Create a new Excel report, or open an existing one.
- Right-click the Levels node, and select Add levels.
-
Select a chart from the list. For example, add Top 10 Products. Click OK.
-
Click the CH184_Level - Top 10 Products node, and drag it onto three empty vertically-aligned cells.
- Click the + on the left to expand the CH184_Level - Top 10 Products level node.
- One at a time, select each column under CH184_Level - Top 10 Products, and clear Keep Source Formats.
-
Hold CTRL and select the level fields you want to add to the sheet.
For example: Product Name, Total Sales, and Gross Profit. Drag them onto the empty row of cells between the level tags.
- Format these fields as you prefer. For example, you could format Total Sales as currency.
Adding formulas and headings
- Select the first empty cell to the right of the field tags. In this example, F4.
- Click on the cell, type = then click on the Total Sales cell.
-
Type -, click on the Gross Profit cell and press Enter. This will calculate costs.
When you insert a formula between level tags, Qlik NPrinting will copy it into every row of the final table.
You can ignore the error #VALUE! in the template. This occurs because the Excel formula references cells that contain field tags.
- In cell G4, type =F5/D5. This will divide costs by sales.
-
Format the formula cells as you prefer using Excel formatting.
For example, costs as currency, and costs/sales as a percentage.
-
Type in headings for each column in a row located one or two rows above the row containing the Top 10 Products opening level tag.
Format these headings as desired.
- Select a cell below the </Top 10 Products_Level> level closing tag so that there is at least one intervening empty cell.
-
In cell D8, enter an Excel SUM formula to create a row of column totals. The sum range should include:
- The field tag cell address.
- The address of the empty cell below it.
In this example, the function would read: =SUM(D4:D6).
- Click and drag this formula to E8 and F8.
-
Apply Excel formatting tools to the elements of the row.
For example: Total Sales and Gross Profit totals could be formatted as currency.
Previewing the report
Do the following:
-
Click Preview.
Excel launches and displays your report.
-
You will see a table, with the two new columns containing your calculated results. The bottom row will contain column totals.
- Click Save and Close to save the template, and close the Template Editor.