Using the Concat() function to display a string of field values as a dimension

A line-level table is normally the most granular data in a Data model. For example, consider an Order Line table. The orders for each customer are stored one row per product line, and we have corresponding costs for each product on each line. When we generate a Table report for such data, we will have a separate line for each product which in itself is not wrong. But recently a customer asked me to initiate an export for the Table report in such a way that all the products for a particular order are contained in a single cell and the sales column should show the aggregated figure for all the products under OrderID. To tackle this requirement, I created a calculated dimension using the Concat function. The process is explained in the following recipe.

Getting ready

  1. Create a new Qlik Sense application.
  2. Add the following INLINE table that contains the Order Line table details:
    Orders:
    LOAD * INLINE [
        Customer,OrderID,Product,Cost
        1,201,Chain,20
        1,201,Seat,40
        1,201,Mudguard,50
        2,202,Gloves,15
        2,202,Basket,60
        3,203,Helmet,70
        ];
  3. Load the data and save the file. Open App overview by clicking on the Navigation dropdown Getting ready in the top-left corner.

How to do it…

  1. Create a new sheet.
  2. Drag the Table object from the left-hand side Assets panel on to the sheet. Name it Sales by Order.
  3. Add OrderID and Customer as dimensions.
  4. Add the following as a third, calculated dimension and label it Products:
    =AGGR(Concat(DISTINCT Product,','),OrderID)
  5. Add the following expression as the measure. Label it Total Sales:
    Sum(Cost)
  6. Click on Save and click on How to do it….
  7. The resulting table on the screen will look like this:
    How to do it…
  8. As you can see, all the products for a particular OrderID value are stringed together in a single cell and the sales figures are the aggregated figures for each OrderID value.

How it works…

The Concat() function gives us the aggregated string concatenation of all the product values separated by the , delimiter. The Concat() function is an aggregation function and hence needs to be used with AGGR in order to be used as a dimension. For the sake of our dimension, the products are grouped by the OrderID.

The same functionality could have been achieved by defining products within a calculation in a measure as follows:

Concat(DISTINCT Product,',')

But by doing so, we won't be able to select the products for a particular OrderID value inside the table.

When we use the calculated dimension, we get the advantage of selecting all the products for the OrderID value in a single go by selecting a cell in the products column.

There's more…

The Concat() function can also be used in the script along with the Group By clause.

See also

  • Using the Fractile() function to generate quartiles
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset