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

fix (visual.ts): avoid adding overlapping categories from data view #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 28 additions & 3 deletions pbiviz/src/visual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,15 @@ export class Visual implements IVisual {

// Set up NodeFile, and ensure that duplicates across source/destination are resolved.
const sourcePropertyMetadata = view.metadata.columns.filter((c) => c.roles.SourceProperty);
const sourcePropertyValues = view.categorical.categories.filter((c) => c.source.roles.SourceProperty);
const sourcePropertyValues = this.getUniqueCategoryValuesByRole(
view.categorical.categories,
'SourceProperty',
);
console.debug('Source properties', { sourcePropertyMetadata, sourcePropertyValues });
const destinationPropertyMetadata = view.metadata.columns.filter((c) => c.roles.DestinationProperty);
const destinationPropertyValues = view.categorical.categories.filter(
(c) => c.source.roles.DestinationProperty,
const destinationPropertyValues = this.getUniqueCategoryValuesByRole(
view.categorical.categories,
'DestinationProperty',
);
console.debug('Destination properties', { destinationPropertyMetadata, destinationPropertyValues });

Expand Down Expand Up @@ -393,6 +397,27 @@ export class Visual implements IVisual {
ReactDOM.render(this.reactRoot, this.target);
}

/**
* If the same columns are used in different data roles, Power BI will map
* them multiple times, resulting in duplicate value arrays, where we will
* only want them for the specified data role of interest. This method
* iterates the categories for the specified role and only ensures that the
* desired categorical values are only added once per occurrence.
*/
private getUniqueCategoryValuesByRole(categories: DataViewCategoryColumn[], role: string) {
const uniqueCategoryValues: DataViewCategoryColumn[] = [];
categories
.filter((c) => c.source.roles[role])
.forEach((c) => {
const isDuplicate =
uniqueCategoryValues.findIndex((uc) => uc.source.queryName === c.source.queryName) >= 0;
if (!isDuplicate) {
uniqueCategoryValues.push(c);
}
});
return uniqueCategoryValues;
}

/**
* For the supplied source/destination column metadata, values and
* exclusions, flatten the values to an array of formatted values, adjust
Expand Down