Forum Discussion
Deneb Sorting Issue when "Datum" is included
- 1 year ago
If you want to use one instance of the value and it's repeated for each row, can you use average or min as an aggregate instead of sum?
Hi vamshi_pbims, and thanks for the details.
I'm not good enough at DAX to solve the first problem, but solving the second may help. You'll need to validate this, though.
The strategy here of using datum to substitute the value of 'Target' is an interesting approach, and will sort of work, but doesn't fulfill the requirement of having a sort order that can be unioned also. Additionally, because of how Vega-Lite unions scales with a sort clause, you will need an aggregate of min, max, count or a boolean value in there. This is provided as a warning in Vega Editor:
For some reason, this doesn't appear in Deneb; I'll have to have a look at why that is.
As such, the approach I've taken is to:
- Aggregate the target value using a transform
- Calculate a field called MonthYear for this layer after aggregation, with a value of 'Target'
- Calculate a field called SortOrd for this layer after aggregation with a value of 999912 (which would be akin to a 'no date' row in a date dimension).
This layer's dataset will look as follows in the debugger:
These fields and values now match the "shape" of the primary layer and will resolve. The only other thing to do is to add an aggregate to the sort clause. Because the scale is grouped by the granularity of the data, either a max or a min will be OK.
Here's the resulting output:
I've made a couple of additional changes to the encoding channels, as they work hierarchicallyin Vega-Lite. Revised spec is as follows:
{
"data": {
"name": "dataset"
},
"transform": [
{
"filter": "datum['Sum Of Price'] != null "
}
],
"layer": [
{
"mark": {
"type": "bar",
"tooltip": true
},
"encoding": {
"y": {
"field": "Sum Of Price"
}
}
},
{
"transform": [
{
"aggregate": [
{
"field": "Target_m",
"op": "sum",
"as": "Target_m"
}
]
},
{
"calculate": "'Target'",
"as": "MonthYear"
},
{
"calculate": "999912",
"as": "SortOrd"
}
],
"mark": {
"type": "bar",
"tooltip": true
},
"encoding": {
"y": {
"field": "Target_m"
}
}
}
],
"encoding": {
"x": {
"field": "MonthYear",
"sort": {
"field": "SortOrd",
"op": "min"
},
"type": "ordinal"
},
"y": {
"type": "quantitative"
}
}
}
I've also attached an updated copy of your workbook, so hopefully you can have a look, test and see if this solves problem #1 as well.
Thanks!
Daniel
Hi dm-p ,
The way sorting works now is correct, however if you see the Target bar, in the PBIX or the screen shot you attached, it is way higher than the overall sum of profit in Target Table (15 ), and its changing when i change dates in my slicer and reaching around 135 too. It is supposed to be 15 and not more that.
Please let me know, If you need any other details.
Thanks,
Vamshi.
Thanks.
- dm-p1 year ago
Super User
If you want to use one instance of the value and it's repeated for each row, can you use average or min as an aggregate instead of sum?
- vamshi_pbims1 year ago
Microsoft Employee
Hi dm-p ,
Thank you very much for the help,
I had to modify the code like below from your code, to get the output for my requirement. And I have commeneted the aggregated part inorder to make my legend working, else its showing as "Undefined" for "Target" bar. And I had to remove "SortOrd" field from visual otherwise the datum bar is coming at starting in the chart instead of end. The trick for my requirement was to use sort: {"op":"min"} in the encoding.
{ "data": { "name": "dataset" } , "transform": [ { "filter": "datum['Sum Of Price'] != null " } ] , "layer": [ { "mark": { "type": "bar", "tooltip": true }} , { "mark": { "type": "bar", "tooltip": true }, "encoding": { "x":{"datum":"Target","type": "nominal"}, "y": { "aggregate":"min", "field": "Target_m", "type": "quantitative" } } } ] , "encoding": { "x": { "field": "MonthYear", "sort":{"field":"SortOrd","op": "min" }, "type": "nominal" }, "y": { "field": "Sum Of Price", "type": "quantitative" },"color": {"field":"Product"} } }Thanks Again,
Vamshi.