Forum Discussion

alexpokerface's avatar
alexpokerface
Frequent Visitor
1 month ago

How to detect drill mode

Hi,

a quick question:

In the Power BI custom visuals API, is there any way to detect whether the visual is currently in drill mode (as opposed to normal selection mode)?

Thanks and kind regards,

Alex

5 Replies

  • dm-p's avatar
    dm-p
    Super User

    Hi alexpokerface,

     

    Unfortunately there's no simple method to check this. I think the only way to detect this would be to check the dataView.metadata for the presence of drillableRoles, and perhaps, based on your knowledge of your setup, whether the resulting state means you're somewhere in the hierarchy (e.g, if there's no DrillType.Up available on a role, then you are at the top of your hierarchy, and therefore you aren't in a drill state.

     

    The pattern for interrogation of drill state here in the docs may provide a starting point for you.

     

    Good luck,

     

    Daniel

  • Hi Alex, alexpokerface 

    Yes, you can detect drill mode in custom visuals.

     

    How it works:


    When a user clicks "Drill Down" in Power BI, the visual gets a new update call. In that update, Power BI tells your visual what mode it's currently in through the DataView metadata.

     

    The Code:


    Put this inside your `update(options: VisualUpdateOptions)` method:

    ```typescript
    import { DrillState } from "powerbi-visuals-api";

    public update(options: VisualUpdateOptions) {
    const dataView = options.dataViews?.[0];

    if (!dataView) return;

    const drillState = dataView.metadata?.drillState;

    switch(drillState) {
    case DrillState.DrillDown:
    // User clicked Drill Down on a category
    // Example: Year -> Quarter -> Month
    this.renderDrillView(dataView);
    break;

    case DrillState.Expand:
    // User clicked + icon to expand tree
    this.renderExpandedView(dataView);
    break;

    case DrillState.None:
    default:
    // Normal selection mode

     

    I hope you got the answer is any problem you can freely to ask here , Thanks 😊

    • alexpokerface's avatar
      alexpokerface
      Frequent Visitor

      Thanks for the snippet! I checked the current and recent versions of the powerbi-visuals-api, and neither DrillState nor dataView.metadata.drillState exists in the public API. If this comes from an internal or experimental build, that would be interesting — but in the official contract, the property isn’t available.

      • dm-p's avatar
        dm-p
        Super User

        Unfortunately that code looks like an hallucination - there's nothing in the APIs matching these types or methods.

  • asafcohen's avatar
    asafcohen
    Microsoft Employee

    Hi alexpokerface ,
    As dm-p said there is no simple straight forward method to check this. However, you can do try the following, all inside update():

    1. dataView.metadata.dataRoles.drillableRoles â€” a map of role name → the DrillType[] currently available for it. When Drill Up is available for a role, the user has drilled down at least one level, which is the reliable "am I in a drilled-down state" signal:

    TypeScript
     
    import powerbi from "powerbi-visuals-api";
    import DrillType = powerbi.DrillType;
    
    public update(options: powerbi.extensibility.visual.VisualUpdateOptions😞 void {
        const dataRoles = options.dataViews?.[0]?.metadata?.dataRoles;
        const available = dataRoles?.drillableRoles?.["category"] ?? []; // role name from capabilities "drilldown".roles
    
        const isDrilledDown = available.indexOf(DrillType.Up) >= 0;   // Drill Up exists => below the top level
        const canDrillDown  = available.indexOf(DrillType.Down) >= 0; // more levels below
        const drillDisabled = dataRoles?.isDrillDisabled === true;    // drilling currently turned off
    }
    

    2. After any drill, Power BI just calls update() again with a new DataView. Read the level from the data — categorical: the number of categorical.categories and each categories[i].source.displayName (e.g., Year → Quarter → Month); matrix: the depth of matrix.rows.levels and each node's level. Note the category value type can change per level (Date at the top, string lower down), so guard with categories[i].source.type before parsing.

    3. To drive drilling from your own UX (e.g., double-click to drill down), call host.drill(args) (API 4.7.0+), and optionally host.setCanDrill(true/false) (5.7.0+) to enable/disable it at runtime.


    Good luck,
    Asaf.

     

    References