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

Merged
merged 8 commits into from
Dec 18, 2024
Merged
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;
KSDaemon marked this conversation as resolved.
Show resolved Hide resolved
KSDaemon marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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
120 changes: 120 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,126 @@ 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('fill missing dates with custom string', () => {
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': 'N/A'
})).toEqual([
{
'Orders.createdAt.day': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt.day': '2020-01-09T00:00:00.000',
'Orders.total': "N/A"
},
{
'Orders.createdAt.day': '2020-01-10T00:00:00.000',
'Orders.total': 10
},
{
'Orders.createdAt.day': '2020-01-11T00:00:00.000',
'Orders.total': "N/A"
}
]);
});

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