ByHariharan Rajendran

Bing Image Search API – Python Script

This article explains how to use a python script to interact with Azure cognitive services – Bing Image Search.


  1. Python 3 & libraries
  2. Bing Search API key – Azure Subscription

Bing Search API

Follow the below steps to create Bing Search API.

Step 1: Visit

Step 2: Create a new resource – search for “Bing Search v7”

Step 3: Once created the API, you need to get API key. The base URL is same for everyone which is


Python script

Use the below python script and replace the key.

import http.client, urllib.request, urllib.parse, urllib.error, base64

import requests, json

desc_url = ‘’

headers = {

    # Request headers

    ‘Ocp-Apim-Subscription-Key’: ‘795bff7c586c43749d79a746dcebe12b’,


params = {

    # Request parameters

    ‘q’: ‘Hariharan Rajendran’,

    ‘count’: ‘5’,

    ‘offset’: ‘0’,

    ‘mkt’: ‘en-us’,

    ‘safeSearch’: ‘Moderate’,


desc_response = requests.get(

              desc_url, headers=headers, params=params)



You can change the parameter values to get the different result.


ByHariharan Rajendran

Raspberry Pi 3

This post helps you to identify the pins in raspberry pi.

In an earlier version of Raspberry Pi, the pin details are clearly specified on the board itself but it is not there on the latest version of Raspberry Pi.

To get the pin details you can type the below command on the console.


Running the above code will give the details as like below image.

I will be posting IoT related scripts and how Raspberry Pi can be integrated with Azure and Power BI.

ByHariharan Rajendran

PowerBI – Python Support

Microsoft introduced Python support from Power BI on August 2018 update.

PowerBI is supporting R visuals and PowerBI producing custom R visuals too. PowerBI team extended the data science capability of PowerBI by supporting python.

In this post, I am explaining the simple python plot in Power BI.

Download the latest version of Power BI. As python support in preview support, we need to enable the python support preview feature.


You should install the Python on your machine so that you can point the python home directory which is mandatory to run any python scripts inside PowerBI.

Once you have configured the setup then you need to test with simple code. Use the below code.

import matplotlib

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 3*4, 500)

plt.plot(x, np.sin(x**2))

plt.title(‘Test Plot’)


The above code works perfectly and produced the plot on jupyter notebook and need to get the same result on PowerBI also.

Drag and drop the python visual into the canvas area. In the below script editor, reference any dummy column and then copy and paste the above script.

Finally, execute the script and it will produce the plot as like below.


Now, you can play with your python scripts.

ByHariharan Rajendran

DAX – RelatedTable Scenario

There is a scenario to find the total count of vendor based on the current year and the same customer should exist on the main transaction table.

Let me explain in detail.

Consider, I have a table called vendors which have very simple columns like below.

Vendor ID Vendor Name Created Date
101 V1 Wednesday, February 22, 2017
102 V2 Thursday, December 28, 2017
103 V3 Friday, February 2, 2018
104 v4 Monday, May 7, 2018


And Transaction table like below.

Trans ID Vendor ID QTY Sale Amount OrderDate
1 101 5 1500 Tuesday, August 22, 2017
2 101 6 2588 Saturday, December 30, 2017
3 102 8 4500 Monday, April 2, 2018
4 103 12 8000 Thursday, June 7, 2018


On the above tables, I want to show the total count of vendors where they are available in the transaction table and their created date should be the current year.

The expected result is “1” as Vendor 103 only available in Transaction table and created date is the current year 2018.

Rest of the rows are not matched with this scenario.

There are various methods are available to achieve the solution. In this post, I am explaining the very simple method.

Step 1: Create a new calculated column on the table Vendors using below script.

IsMatch = IF(Format(TODAY(),”YYYY”) = FORMAT(‘Customer (2)'[Date],”YYYY”), “Match”, “NoMatch”)

It is adding a new column with values like match and nomatch.


Step 2: Create another calculated column on the vendor table using below script. We need to use the above-created column.

Count = CALCULATE(IF(ISBLANK(MINX(RELATEDTABLE(‘FACT’),”1″)),0,1),’Customer (2)'[Test]=”Match”)

That’s it. We can use the count column for the result.


ByHariharan Rajendran

Import + DirectQuery in Power BI

Microsoft Power BI team has released a new update called “Composite Models” which is in preview now. This post talks about the composite models.
If there is a requirement to use SQL server and Excel for Power BI report, it was possible only with import mode. It means both the data sources should be used as an import option then we can combine the sources and build the report.
What will happen if I want to use SQL Server as a direct query on the above scenario? We couldn’t combine those two data sources together, but it is possible with this composite model.
How to use?
As this is a preview feature, you need to enable this feature on “Preview Features” section.
Once enabled then you can add SQL Server Direct Query and Excel together within Power BI desktop and start building the reports.
1. When you decide to go with this feature, you need to ensure to accept the below message. The information of the one data source might have sent to other data source while fetching the data from different sources.

Still, there are few limitations with this composite model.
This composite model consists of below features which we will discuss in our next post.
1. Many-to-Many relationships
2. Storage Mode

ByHariharan Rajendran

Capture the Selected Filter in PowerBI – DAX

As PowerBI has various options to slice and dice the data, it is very mandatory to understand the filter context on the PowerBI report.

Filter Context can be applied on various levels

  1. Row & Column Headers
  2. Slicers
  3. Filters (Visual, Page & Report level)

Let us take a scenario where the user wants to capture the filters applied to the specific field.

To demonstrate this scenario, I create a simple table as like below.

I need to use Description field on the table to filter the data.

Description field on Slicer

If the user selects the checkbox then it should show the selected value. If it is a single selection, then show the value. If users select multiple values, then show as “Mulitple”.

Description field on Filter (Page level Filter)

Place the description field on the page level filter and see the outcome.

Use the below DAX script to achieve the result.

Parameter Selection =
IF (
HASONEVALUE ( Parameters[Value] ),
“Selection” & VALUES ( Parameters[Description] ),
IF (
NOT ( ISFILTERED ( Parameters[Description] ) ),
“No Selection”,
“Mulitple Selection”

ByHariharan Rajendran

Year to Date calculation in DAX

This post explains how to achieve Year to Date calculation using DAX script.

In DAX, there is a pre-defend function available to get the Year to Date calculation. Also, for the quarter to date & month to date.

Those functions are




Instead of using the pre-defend functions, we can use our own defined function like below. To achieve the YTD calculations, use the below functions

  3. ALL

Let us consider a field called “Total Sales”. We will be used the Total Sales & “Date” table.

YTDSales = CALCULATE([Total Sales],


                          ‘Date'[Calendar Year] = MAX(‘Date'[Calendar Year]) &&

                          ‘Date'[Date] <= MAX(‘Date'[Date])))

Compare the result TOTALYTD() with our script.

Explaining the script.

Let us consider that we are in row 2014 – March row on the above table.

YTDSales = CALCULATE([Total Sales],


‘Date'[Calendar Year] = MAX(‘Date'[Calendar Year]) &&

‘Date'[Date] <= MAX(‘Date'[Date])))


‘Date'[Date] <= MAX(‘Date'[Date]) => This line of code returns maximum date. In our case, It will return 31/3/2014

‘Date'[Calendar Year] = MAX(‘Date'[Calendar Year]) => This line of code returns maximum year.  In our case, it will return 2014

ALL(‘Date’) => This line of code returns all the dates from date table.


‘Date'[Calendar Year] = MAX(‘Date'[Calendar Year]) &&

‘Date'[Date] <= MAX(‘Date'[Date])))

The above line of codes returns, current row year and last date of the current row month.


ByHariharan Rajendran

Connect Live option disabled

This post solves the very simple problem in Power BI.

If you are trying to access the Power BI datasets to create reports then you may encounter this below error message.

Error: “The connect live option for this file is disabled because it already contains data from another data source.”

“Power BI datasets” is one of the options from “Get Data”.  Whenever you publish a report from Power BI desktop to Power BI services, it is publishing the dataset and report.

You can create multiple reports using the same published datasets from Power BI services. This is something like a shared dataset. In case, if you are experiencing the above error then you need to ask your administrators to fix the issues.

The administrator has to fix this issue on the admin side of the Power BI services. Follow the below steps.

  1. Go Power BI cloud services
  2. Go to the Admin portal
  3. Click Tenant settings on the left side and check the export and sharing settings
  4. Enable the export data feature.

That’s it. The issue has been fixed.