cube-js / cube

📊 Cube — Universal semantic layer platform for AI, BI, spreadsheets, and embedded analytics
https://cube.dev
Other
17.97k stars 1.78k forks source link

Use different dimension for display other than the one for grouping #1140

Closed mnifakram closed 4 years ago

mnifakram commented 4 years ago

Problem

How can I use another dimension for display?

Related Cube.js schema

cube(`Participations`, {
  sql: `SELECT * FROM public.participations`,

  joins: {

  },

  measures: {
    count: {
      type: `count`
    }
  },

  dimensions: {    
    stepDefinitionId: {
      sql: `activity_id`,
      type: `string`
    },

    id: {
      sql: `id`,
      type: `string`,
      primaryKey: true
    },

    participantName: {
      sql: `participant_name`,
      type: `string`
    },

    stepDefinitionName: {
      sql: `activity_name`,
      type: `string`
    },

    participantId: {
      sql: `participant_id`,
      type: `string`
    },
  }
});

Cube.js JSON query

{
      measures: ["Participations.count"],
      timeDimensions: [],
      dimensions: [
        "Participations.participantId",
        "Participations.activityId"
      ],
      filters: [],
      order: {}
    }

I'm wondering if there is a way to select the participantName and activityName instead of participantId and activityId. In other words use participantId and activityId for grouping and use others for display purpose.

Thanks

RusovDmitriy commented 4 years ago

Hey @mnifakram! Please see following links about ungrouped option:

Also you could add these fields as dimensions and config display in your client code:

{
      measures: ["Participations.count"],
      dimensions: [
        "Participations.participantId",
        "Participations.activityId",
        "Participations.participantName",
        "Participations.stepDefinitionName"
      ]
}
mnifakram commented 4 years ago

Hey @RusovDmitriy! Thank you for the quick response

RusovDmitriy commented 4 years ago

two participants named Jhon Doe but with different id - it is no problem, because participantId and activityId also included in the grouping.

If the possible situation with diff name but the same id - you could use aggregation funcs and rewrite base SQL for cube schema:

SELECT 
 id
 participantId,
 activityId,
 STRING_AGG(participantName) as participantName,
 STRING_AGG(stepDefinitionName) as stepDefinitionName
FROM public.participations
GROUP BY id, participantId, activityId
mnifakram commented 4 years ago

Got it, thank you so much for your help @RusovDmitriy.