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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
as7
Advocate II
Advocate II

Problem with Selection in custom visualization

I am developing custom Visual in typescript and want to make dropdown having respected data in it.

Ok with Visual Creation, But having problem to implement Selection ID with the data append to my visual.Whenever i use selection ID with data, then my visual fails to load data in it. Please help me with this , regarding selection in custom visual.

 

My code in Visual.ts file is given below:-

 

module powerbi.extensibility.visual {
    //Array to store URL
    interface imageViewModel {
        
        URL: imageDataPoint[];
      
    };
    //Declare URL as string
    interface imageDataPoint {
        imageUrl: string;
        selectionId: ISelectionId;

        
        
    };
    //Data from Power BI to imageViewModel    
    function visualTransform(options: VisualUpdateOptions, host: IVisualHost): imageViewModel {
                let dataViews = options.dataViews;
                let viewModel: imageViewModel = {
                                                    URL: []
                                                    
                                                 };
       if (!dataViews
           || !dataViews[0]
           || !dataViews[0].categorical
           || !dataViews[0].categorical.categories
           || !dataViews[0].categorical.categories[0].values)
                    return viewModel;
                let imageData = dataViews[0].categorical;
                if (imageData) {
                    var imageElement = imageData.categories;
           

           if (imageElement && imageElement.length > 0) {

               for (var i = 0, catLength = imageElement.length; i < catLength; i++) {
                   var k = 0;

                   viewModel.URL.push({
                       imageUrl: <string>imageElement[0].values[i],
                       selectionId: host.createSelectionIdBuilder()
                           .withMeasure(imageElement[0].values[i])
                           .createSelectionId()
                      })
                  

               }
               
               
           }
       }
    

       return viewModel;
    }
    export class Visual implements IVisual {
        
        private image: d3.Selection<SVGElement>;
          
        private host: IVisualHost;        
        private selectionManager: ISelectionManager;
        
                   
       

        static Config = {
            xScalePadding: 0.1,
            solidOpacity: 1,
            transparentOpacity: 0.5
         };
       
        //Element initialization
        constructor(options: VisualConstructorOptions) {
            

            this.image = d3.select(options.element).append("select");
                               
        }

        //Any change tu visualization executes this upadate function
        public update(options: VisualUpdateOptions)  {
            
            let selectionManager = this.selectionManager;
            let viewModel: imageViewModel = visualTransform(options, this.host);
                       
            var zoomFlag = 0;
           
            let divPop = this.image.selectAll('option').data(viewModel.URL);

            divPop.enter().append('option').text(function (d) { return d.imageUrl; });

            
            divPop.exit().remove();
                     
            
            
         
        }
        
       
        public destroy(): void {
            //TODO: Perform any cleanup tasks here
        }
    }
}

 

8 REPLIES 8
KumarDarmesh
Helper IV
Helper IV

What is the purpose for dropdowning? What does the visual do actually

I want visual to have start time and end time inputs . that's why i thinking of having drop down in both , so that it contain 01,02,03 hrs and so on. So, when user select start time 01 hrs and end time 02 hrs , then data filter according to this time period.

and if any visual available in power BI for this type of need then please tell me, otherwise please help. 

Why not creating a calendar table in hours and slicer the hours then?

Thanks for your suggestion.

 

How can I create calendar table in hours as i have created calendar table for date purpose but how can i put time in it and bifurcate it to hours. It will be very helpful if you can show the DAX for it.  

Eric_Zhang
Microsoft Employee
Microsoft Employee

@as7

You can try

 

calendar table in hours =
SELECTCOLUMNS (
    ADDCOLUMNS (
        CROSSJOIN (
            CALENDAR ( "2016-01-01", "2017-12-31" ),
            SELECTCOLUMNS (
                ADDCOLUMNS ( CALENDAR ( "2016-01-01", "2016-01-24" ), "DAY", DAY ( [Date] ) ),
                "DAY", [DAY]
            )
        ),
        "dateTime", [Date]
            + IF ( [DAY] = 24, 0, [DAY] )
                / 24
    ),
    "Date", [Date],
    "dateTime", [dateTime]
)

thanks.

 

But, my requirement is to have a time slicer that can slice my data from one time to other time . and power BI dosen't have such time slicer.

Eric_Zhang
Microsoft Employee
Microsoft Employee

@as7

Do you want to slice a time window like '02:00' to '15:00' for all days? If so, there's some workaround by creating proper lookup tables and measures. 

 

Check a similar thread Specify custom date periods with predefined date periods. The difference is, you have to create start time and end time and filter the data with them, rather than start date and end date in that link.

 

Thanks for this help. But can you please tell me how can i make a table with range of time only not date.

Because Calendar() function is taking date only.

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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