Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cubejs-client-core): fill missing dates with custom measure value #8843

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/pages/reference/frontend/cubejs-client-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,8 @@ resultSet.tablePivot({
Name | Type | Description |
------ | ------ | ------ |
aliasSeries? | string[] | Give each series a prefix alias. Should have one entry for each query:measure. See [chartPivot](#result-set-chart-pivot) |
fillMissingDates? | boolean | null | **`true` by default.** If set to `true`, missing dates on the time dimensions will be filled with `0` for all measures. Note: setting this option to `true` will override any `order` applied to the query. |
fillMissingDates? | boolean | null | **`true` by default.** If set to `true`, missing dates on the time dimensions will be filled with `fillWithValue` or `0` by default for all measures. Note: setting this option to `true` will override any `order` applied to the query. |
fillWithValue? | string | null | Value to autofill all the missing date's measure. |
x? | string[] | Dimensions to put on **x** or **rows** axis. |
y? | string[] | Dimensions to put on **y** or **columns** axis. |

Expand Down Expand Up @@ -1055,4 +1056,4 @@ values? | never |
[link-mdn-max-safe-integer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

[ref-query-format]: /product/apis-integrations/rest-api/query-format
[ref-security]: /product/auth
[ref-security]: /product/auth
6 changes: 5 additions & 1 deletion packages/cubejs-client-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,13 @@ declare module '@cubejs-client/core' {
*/
y?: string[];
/**
* If `true` missing dates on the time dimensions will be filled with `0` for all measures.Note: the `fillMissingDates` option set to `true` will override any **order** applied to the query
* If `true` missing dates on the time dimensions will be filled with fillWithValue or `0` by default for all measures.Note: the `fillMissingDates` option set to `true` will override any **order** applied to the query
*/
fillMissingDates?: boolean | null;
/**
* Value to autofill all the missing date's measure.
*/
fillWithValue?: string | null;
/**
* Give each series a prefix alias. Should have one entry for each query:measure. See [chartPivot](#result-set-chart-pivot)
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-client-core/src/ResultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class ResultSet {
const pivotImpl = (resultIndex = 0) => {
let groupByXAxis = groupByToPairs(({ xValues }) => this.axisValuesString(xValues));

const measureValue = (row, measure) => row[measure] || 0;
const measureValue = (row, measure) => row[measure] || pivotConfig.fillWithValue || 0;

if (
pivotConfig.fillMissingDates &&
Expand Down
60 changes: 60 additions & 0 deletions packages/cubejs-client-core/src/tests/ResultSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,66 @@ describe('ResultSet', () => {
]);
});

test('fill missing dates with custom value', () => {
const resultSet = new ResultSet({
query: {
measures: ['Orders.total'],
timeDimensions: [
{
dimension: 'Orders.createdAt',
granularity: 'day',
dateRange: ['2020-01-08T00:00:00.000', '2020-01-11T23:59:59.999']
}
],
filters: [],
timezone: 'UTC'
},
data: [
{
'Orders.createdAt': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt': '2020-01-10T00:00:00.000',
'Orders.total': 10
}
],
annotation: {
measures: {},
dimensions: {},
segments: {},
timeDimensions: {
'Orders.createdAt': {
title: 'Orders Created at',
shortTitle: 'Created at',
type: 'time'
}
}
}
});

expect(resultSet.tablePivot({
'fillWithValue': 5
})).toEqual([
{
'Orders.createdAt.day': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt.day': '2020-01-09T00:00:00.000',
'Orders.total': 5
},
{
'Orders.createdAt.day': '2020-01-10T00:00:00.000',
'Orders.total': 10
},
{
'Orders.createdAt.day': '2020-01-11T00:00:00.000',
'Orders.total': 5
}
]);
});

test('same dimension and time dimension without granularity', () => {
const resultSet = new ResultSet({
query: {
Expand Down