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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Zubair_Muhammad
Community Champion
Community Champion

Sort by using Visuals API

Hi,

 

How can I perform the sort operations (as shown in the pic below) inside the custom visual using Power BI visuals API?

for example if I am creating a Table visual and want to sort Table by clicking on the headers/buttons etc

 

I can use sort function of javascript. But sometimes a column is sorted by another column in Power BI. Javascript's sort function will not respect that. 

@dm-p @v-rzhou-msft @v-yiruan-msft 
sortby2.png


Regards
Zubair

Please try my custom visuals
5 REPLIES 5
dm-p
Super User
Super User

Hi @Zubair_Muhammad,

In custom visuals, sorting just affects the order of the visual's dataset as supplied from the host (as this is set outside the sandbox using the standard header) and there's no API to determine which column or measure is actually the subject of the sort. 

If you want control of sorting (short of a feature request to add such an API, which would admittedly be very useful) then it's probably better to use an option of implicit or custom and managing sorting through your visual events and /or properties. The Violin Plot uses this approach, although the data roles are reasonably predictable to manage. If your data roles are flexible then this is more tricky - I might approach by creating interactive elements to sort by a particular column, sort your view model's dataset based on that, and persist a property containing which field in your view model you're using so that it will remain in-place when the visual is re-initialised (you could remove the property from view during enumeration if you just want to make it accessible from code rather than by user).

Regards,

Daniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Thanks Daniel @dm-p 

 

Actually we can determine which column has been used for sorting from the columns' metadata.

These 2 properties appear if column has been used for sorting

sort.png

 

My predicament is how to know if this column is sorted by another column. For example you will add index column to sort months column {Jan, Feb, Mar}  so that they are not sorted alphabetically as {Feb,Jan,Mar}

In a table visual, users intuitively click on the headers to sort rather than using the native option to sort from visual header. Thats why I was wondering if there is an option there or not in the API

The native Table/ Matrix visual does that

sort1.png

 

 


Regards
Zubair

Please try my custom visuals

Hey Zubair, and thanks very much for teaching me something new (to me!) 🙂

Being as I wasn't too informed on my previous post, I'd take what I say next with a grain of salt, but I haven't been able to find this myself in any previous (or current) projects and have deferred to using the visual header to manage.

I've never seen details on sort by column metadata for a field in the API, and if the sort values aren't in the dataset, they could not be used internally. It is likely that core visuals have more direct access to the data model as they are not sandboxed, or there are APIs that have not been promoted to the custom visuals SDK. 

If there isn't an API to bring in from core, something new on the visual host would be ideal for this, i.e.:

interface IVisualHost {
    ...
    applySort(columns: DataViewSortColumn[]) /* New */
}

interface DataViewSortColumn {
    queryName: string,
    direction: 'ascending' | 'descending'
}

(the param doesn't have to be an array, but sorting by multiple columns would be neat to achieve also)

Either way, I think it's better asked directly to the visuals team, or creating an issue in the GitHub repo for some dedicated assistance.

Regards,

Daniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Thanks Daniel @dm-p 

 

I really like this suggestion of applysort in IVisualHost.
I emailed to pbicvsupport as well for help. Waiting for their reply now.


Regards
Zubair

Please try my custom visuals

Hi @Zubair_Muhammad 

Please kindly share your workaround in your Post if you find the solution to your problem. This will help many other people with the same problem like yours.

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.