Sunday, November 1, 2020

Timeline View Using HTML Content Visual in PowerBI


We are creating an HTML based 'Timeline' visual in PowerBI.
We have a dataset in Excel file that looks like this:

We are going to use Python to add clock icons according to time recorded in "dt_ts" column. Go to "Home >> Transform Data >> Transform >> Run Python Script". Ignore any Privacy warnings that you might get while running script the first time. Code for the "Python Editor": # 'dataset' holds the input data for this script from dateutil.parser import parse def get_icon(x): icon_dict = { "1:0" : "&#128336;", "2:0" : "&#128337;", "3:0" : "&#128338;", "4:0" : "&#128339;", "5:0" : "&#128340;", "6:0" : "&#128341;", "7:0" : "&#128341;", "8:0" : "&#128343;", "9:0" : "&#128344;", "10:0" : "&#128345;", "11:0" : "&#128346;", "12:0" : "&#128347;", "1:30" : "&#128348;", "2:30" : "&#128349;", "3:30" : "&#128350;", "4:30" : "&#128351;", "5:30" : "&#128352;", "6:30" : "&#128353;", "7:30" : "&#128354;", "8:30" : "&#128355;", "9:30" : "&#128356;", "10:30" : "&#128357;", "11:30" : "&#128358;", "12:30" : "&#128359;", } hour = parse(x['dt_ts']).hour min = parse(x['dt_ts']).minute if min >= 0 and min < 30: min = 0 else: min = 30 return icon_dict[str(hour) + ":" + str(min)] dataset['icon'] = dataset.apply(get_icon, axis = 1) The above code creates a new column "icon". Then, "Close and Apply" 'Transform Window' and come back to "Home" tab. There on the right hand side, you can see your 'Sheet1' (Sheet1 in our case, this is the name of your Excel sheet or Database table). Click on "Ellipsis" and "New Column".
What we are going to write next is a mix of HTML, CSS and DAX code. HTML = "<p style='font-size: 25px'>" & Sheet1[icon] & ": Message: " & Sheet1[read_time] & "; Char: " & IF( OR(ISBLANK(Sheet1[col_with_empty_cells]), LEN(Sheet1[col_with_empty_cells]) = 0) , "NA", Sheet1[col_with_empty_cells] ) & "</p>" To get the HTML Visual, we get the HTML Visual from GitHub Load the Visual using the downloaded "pbiviz" file. (Ref: PowerBI's HTML Content Visualization) To get the HTML content in the Visual: Either: Drag the column into the 'HTML Content' Visual. Or: Click on the 'Tick' mark before the "HTML" column in the side bar on the right named "Fields". Output:
References % GitHub Link to PowerBI Notebook and Excel used for this demo % Intergration of Anaconda and PowerBI % Get 'HTML Content Visual' from store.office.com % OR Function DAX (Microsoft) % DAX Operator Reference (Microsoft) % Clock Symbols

No comments:

Post a Comment