Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Dan3
Regular Visitor

Adding Date Hierarchy programmatically to PBI Embed using JS

Hello Everyone,

I'm using powerbi JS and powerbi-report-authoring JS to create an embed, where I generate visuals, and to these visuals I want to add in a Date Hierarchy, in this case the visual type is a tableEx, and the data I want to upload is the following:

"prototypeQuery": {
	"Version": 2,
	"From": [{
			"Name": "p",
			"Entity": "potentialTypes",
			"Type": 0
		}
	],
	"Select": [{
			"HierarchyLevel": {
				"Expression": {
					"Hierarchy": {
						"Expression": {
							"PropertyVariationSource": {
								"Expression": {
									"SourceRef": {
										"Source": "p"
									}
								},
								"Name": "Variation",
								"Property": "Date"
							}
						},
						"Hierarchy": "Date Hierarchy"
					}
				},
				"Level": "Year"
			},
			"Name": "potentialTypes.Date.Variation.Date Hierarchy.Year",
			"NativeReferenceName": "Date Year"
}]}

 

and here is the code I use to upload the data into the visual

visual.addDataField(Object.keys(item.singleVisual.projections)[0], {
    $schema: "http://powerbi.com/product/schema#hierarchyLevel",
    table: dataFields.Name.split('.')[0],
    hierarchy: dataFields.HierarchyLevel.Expression.Hierarchy,
    hierarchyLevel: dataFields.HierarchyLevel
});

 

I get the following error when I run this:  

{message: 'FailedToAddDataField', detailedMessage: 'Failed to add data field to role'}

 

I tried it without the hierarchy (replacing it with HierarchyLevel, Expression), I tried it with different Hierarchical data, I tried burning in the data, I went though the whole microsoft learn wiki (https://learn.microsoft.com/en-us/javascript/api/overview/powerbi/data-fields) but to no avail.

 

The only way that works is if I use column instead of the hierarchy, however the problem then that it creates all of the Date Time hierarchy, not just the one I specified, this also means that I am unable to add a normalized Date (that is not a Hierarchy) to a field programmatically, because it turns into a Hierarchy.

 

 

I am unable to add only a specific level of the Date Hierarchy to the visual programmatically; it either fails or adds the entire hierarchy, which is not the desired outcome.

 

Has anyone encountered this issue before or successfully added a specific level of a Date Hierarchy to a visual using Power BI's JavaScript API?

 

Any insights or guidance would be greatly appreciated.

 

Thank you in advance for your help!

Dan

 

 

0 REPLIES 0

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.