Author name: cndro

Blogs

How to Replace Data Source in a Report in Power BI

In this tutorial, we’ll be looking at how we can replace data source in a report in Power BI. Replacing Data Source in a report is common when we decide to alter our data source or probably we want to connect to an online data or a database which is different from the local CSV file or excel running on our machine. We have different approaches we can follow to achieve this which also rely on the type of data we want to replace it with. METHOD 1 Replacing Data with the Same Connector If the data we are connected to locally is an Excel or CSV file and we want to replace it with another file, we can do that by using the same connector. While at it we will ensure the new data has the same columns as the original one used in our report, otherwise it might result in an error. We can replace the data by clicking on File → Options and Settings → Data Source Settings. After clicking on Data Source Settings, it brings up the image below. Under Data Source Settings, select the Change Source button, which brings the Pop-up menu below. Click on the browse button to bring in the new file we want to replace within the report and then click Ok. METHOD 2 Replace data with a different data source connector Here, we want to replace our data with a new data source that is completely different from what is in the report. Let’s say we want to bring this data from SQL Server. We will show you the steps on how to go about that. Go to the home tab and navigate to the Transform Data by clicking on it which opens up the Power Query Editor. Under the Power Query Editor, click on New Source, which opens up a pop-up menu, for which you will locate the database you want to connect to. For us, we’ll be connecting to SQL Server database. After clicking on SQL Server it opens up a new pop up menu to pass our credentials and then we connect. The navigator page is shown below to select the table we want to replace with in our report. We will click on the table and then select OK. This brings us to a new page as seen below. The new data we brought in now is the newfirst_data table which is to replace ListOfOrders table. Now, we can see that the data has been selected. Click on Advanced Editor. This Advanced Editor is where we are to perform the major work on replacing the data. After the Editor has opened, we will copy what we highlighted in the image below. This code we copied will be pasted inside our original data Advanced editor. Now let’s do that. Click on our original data (ListOfOrders) and as well open its Advanced Editor. The code highlighted above will be replaced with the code we copied earlier, so we have the Advanced Editor as shown below. Then, we will click Done, whereby the data we brought in from SQL Server has been replaced with the former we had in our report. We can see the ListOfOrders Data Table has changed as shown below. And that’s a wrap. I hope you enjoyed this post! Thanks for reading.

Blogs

How to Use Inheritance in Python

Photo by Nubelson Fernandes on Unsplash Inheritance is referred to as the act of inheriting something or when it’s passed down to someone else. In this tutorial, we’ll be looking at what inheritance is in Python. That is, how we can use it and how it’s being created. Inheritance in Python is the ability of a class to acquire or inherit properties from another class. The class it inherits is called base (or parent class) and the new class is called derived (or child) class. The capability to inherit properties from another class helps to promote the reusability of code, whereby we don’t repeat code we’ve written earlier. Inheritance is also known as a powerful feature in object-oriented programming. We’ll demonstrate different examples of how we can use inheritance in Python. Single Inheritance In Single inheritance, this is how a class can inherit from only a single parent class of which it has access to properties of the base class. In the example below, we will demonstrate a basic example of how (we) a Student class is inheriting from the base class(Persons), which gave it access to elements in the base class. 1class Person: 2 def __init__(self, firstName, lastName): 3 self.fname=firstName 4 self.lname=lastName 5 6 def printname(self): 7 print(self.fname, self.lname) 8 9 10class Student(Person): 11 pass 12user = Student(“Mike”, “Johnson”) 13user.printname() 14#Result 15Mike Johnson Multiple Inheritance Multiple Inheritance refers to how a class inherits from more than one base class and all features of the base classes are inherited into the derived class. This type of inheritance is called multiple inheritance. In this example, we demonstrated with a bio-data information whereby we have 2 different base classes (Name, Age), which the Bdata inherited from and it also gain access to functions of both classes. 1# Base class1 2class Name: 3 PName = “” 4 def Name(self): 5 print(self.PName) 6 7# Base class2 8class Age: 9 Agevalue = “” 10 def Age(self): 11 print(self.Agevalue) 12 13# Derived class 14class BData(Name, Age): 15 def userData(self): 16 print(“Name :”, self.PName) 17 print(“Age :”, self.Agevalue) 18 19the_data = BData() 20the_data.PName = “Grace” 21the_data.Agevalue = “15” 22the_data.userData() 23 1#Result 2Name : Grace 3Age : 15 We have another example below, where we have the Base class ‘Intern’, ‘FullWorker’  and also a child class TeamLeader  which inherits the 2 base class ( ‘Intern’, ‘FullWorker’ ) and gets to access all properties and elements of each of those classes. 2# Parent class 1 3class Intern(object): 4 def __init__(self, name): 5 self.name = name 6 7# Parent class 2 8class FullWorker(object): 9 def __init__(self, salary, jobtitle): 10 self.jobtitle = jobtitle 11 self.salary = salary 12 13# Deriving a child class from the two parent classes 14class TeamLeader(Intern, FullWorker): 15 def __init__(self, name, jobtitle, salary, experience): 16 self.experience = experience 17 Intern.__init__(self, name) 18 FullWorker.__init__(self, salary, jobtitle) 19 print(“Name: {}, Pay: {}, Exp: {}”.format(self.name, self.salary, self.experience)) 20 21the_teamlead = TeamLeader(‘Bill’,’Data Engineer’, 300000, 4) 1#Result 2Name: Bill, Pay: 300000, Exp: 4   Thanks for reading this article. Feel free to drop your comments and questions in the comments section below.

Blogs

Range Function in Python

Photo by AltumCode on Unsplash The range() method in Python is usually used to return a fixed set of numbers between a given start integer to the end of an integer. We can have numbers like 0, 1, 2, 3, and 4, which is very useful since the numbers can be used to index into collections such as strings. The Syntax of the Range Function The range() function takes in three arguments(start, step, and stop), whereby we have two of these arguments that are optional(start and step) and the mandatory one is the stop. range(start, stop[, step]) We’ll demonstrate further how to use Range in the different examples below. a. Generate Numbers with For Loop using One Parameter We will implement a for loop by using only one parameter value (stop). Now, let’s find the range of numbers within 0 to 7 range, this should return 0 to 6. We might discover we have results showing 0, 1, 2, 3, 4, 5, and 6. The reason for this is that the range function does exclude the last value when computing its result. 1for i in range(7): 2 print(i) Result 10 21 32 43 54 65 76 b. Generate Numbers with For Loop using Two Parameters Now, we’ll generate numbers by using For loop with two parameters value(start and stop). Here, we have no step value. 1for i in range(1,5): 2 print(i) 3 4 Result 11 22 33 44 c. Reversed Range The reversed function can be used with Range to perform the operation from the back. It is also possible to use positive or negative numbers as parameters in the range. 1for i in range(5, 1, -1): 2 print(i) Result 15 24 33 42 Another Example 1s = ‘Python’ 2len(s) 3 4for i in reversed(range(len(s))): 5 print(i, s[i]) Result 15 n 24 o 33 h 42 t 51 y 60 P d. How to create List, Tuple, and Set with Range The range function is also applicable to use with other different methods whereby we have the list, set, and tuple. List 1print(list(range(0, 20, 5))) Result 1[0, 5, 10, 15] Tuple 1print(tuple(range(0, 20, 5))) Result 1(0, 5, 10, 15) Set 1print(set(range(0, 20, 5))) Result 1{0, 10, 5, 15}

Blogs

Tableau Dashboard Parameter Actions

In this tutorial, we’ll look at how to use Tableau dashboard parameter actions. We know Parameters as containers of values used to calculate and enter values not present in the original dataset which can be workbook variables like dates, a string of text, numbers, or calculated fields. We can use the Parameter Actions in Tableau Desktop version 2019.2 upwards, which gives users the ability to change parameter values even more easily. We will demonstrate by using the Superstore dataset. Step 1 Open Tableau on your computer, bring in your Superstore Dataset and open a new worksheet. Go to the top left corner by clicking the small arrow to select create Parameters in order to start off with what we need to do. The type of Parameter we’ll be creating is a float data type format that accepts values from users. The function responsible for that is the Allowable values we can see in the image below. Step 2 The next thing to do is to drag the Parameter to the Marks card under the text because we want to change the current values based on our selection. From what we could see in the image below, we found out that we have the Dollar currency as well. This was added by right-clicking and clicking on format. Go to Fields → Pane → Numbers, select the currency(standard) and select English(United States) as the Locale. Step 3 Next, we will create another chart in a new worksheet to calculate the Sales by Month. Drag the Order Date(change the view to month) and the Sales into the Pane. We can edit the Axis Pane to be in currency format by right-clicking and selecting Format. Then click on Axis → Numbers → Scale to change to standard US currency format. After this, we will place both worksheets in a dashboard to go further with our Parameter Actions. Step 4 To add a Parameter Action, we will click Dashboard in the top menu and then Actions. Click on the Add Action button and select Change Parameter among the options. After clicking Change Parameter, tick the sheet to our sales by month(Sheet 2) and select our Parameter as the target Parameter and the Sales with sum Aggregate as the Source Field. We then have control of the source sheet of the action, whereby whenever we click on the line graph, the Sales value is Overwritten in our my_param Parameter we created, just as what we have below.

Blogs

Parameters in Tableau

Parameters in Tableau are referred to as containers of values used to calculate and effectively enter values not present in the original dataset. These parameters can include workbook variables like dates, a string of text, a number, or a calculated field which can be used to replace the constant value in a filter or calculation. We can also add conditions and put in a range of values for which we want to create a parameter. After that, we then incorporate these values into our reports and dashboards so we can see each change. In this tutorial, we’ll demonstrate what the Date Field Parameter is. Date Field Parameters in Tableau Working with Date Parameters in Tableau helps to give users the flexibility to change date levels from years to months, from months to weeks, or from weeks to days. We’ll show this with an example below; Step one Open Tableau on your computer and open a new worksheet; we will use the Superstore Dataset. Now, drag the Order Date to the Row Pane on the working area and drag the Sales to the Text under the cards. Ensure to select Table under Show Me, so it won’t be displayed as a chart. Step Two We have each sales value per year, what we need to do is create our Parameter. We will do that by clicking the arrow icon in the top left corner and selecting create parameter option as shown in the image below. The dialogue box appears and we will put in a Name(Dates Params) and select the datetype as a string. Select the list option and put in each of the values(Year, Quarter, Month, Week, Day) and press Enter Key. Follow what we did in the image below and select OK. You will see your Parameter below the Measure Fields and right-click on it. Select Show Parameters which will be displayed on your working area for easy navigation. Step Three The Parameters we created aren’t in use yet except that we have to reference them in a calculated field. In this calculated field, we will select each of the fields (Year, Quarter, Month, Weeks, and Day) to match their actual calculation, so it can display each sale we have per year, quarter, month, week, and day respectively. 1CASE [Dates Params ] 2 3WHEN “Year” THEN STR(YEAR([Order Date])) 4 5WHEN “Quarter” THEN STR(YEAR([Order Date]))+”/Q”+ DATENAME(‘quarter’, [Order Date]) 6 7WHEN “Month” THEN DATENAME(‘month’,[Order Date])+””+STR(YEAR([Order Date])) 8 9WHEN “Week” THEN “Week” + STR(DATEPART(‘week’,[Order Date])) 10 11WHEN “Day” THEN STR(DATE([Order Date])) 12 13END   After this, we will click on the Show Me tab to change the view from Table to chart. Thereby, we can navigate to different charts using our Parameters. If you found this post helpful, let us know in the comments box.

Blogs

How to Connect Python with SQL Server Database Using Pyodbc

In this tutorial, we will look at how to connect Python with SQL server using Pyodbc Library. The Pyodbc library is an open-source Python module that makes accessing ODBC databases simple. It is also a community-supported software. We have other Database Management Systems(DMS) that also work with ODBC. These Database Management Systems include; Oracle, MS Access, IBM Db2, MS SQL Server, and MySQL. How to Get Started Step1 The first thing we need to do is to install the Pyodbc Library in order to create a connection between Python and the SQL Server. Installation We can install Pyodbc with the PIP Package Installer. pip install pyodbc Step 2 Now, we need to create a connection using the Pyodbc library we just installed. The credentials we need to use are; Server Name, Database Name, and the table we want to connect to. 1#import the pyodbc library 2import pyodbc 3 4#create a connection with the pyodbc using the needed credentials 5the_conn = pyodbc.connect(‘Driver={SQL Server};’ 6 ‘Server=my-project.xxxxxxxxxxxxxs.amazonaws.com;’ 7 ‘Database=myData;’ 8 ‘Trusted_Connection=yes;’) 9#create an instance and execute using the select command to retrieve your table details 10cursor = the_conn.cursor() 11cursor.execute(‘SELECT * FROM your_table_name’) 12#loop through the result to obtain your table data 13for j in cursor: 14 print(j)   How to Connect Pyodbc with Pandas You can also use Pandas library to connect to SQL Server along with the Pyodbc. The Pandas library is well known for its flexibility and ease of use. You can follow the steps below to use the library with Pandas. Install Pandas We can install Pandas with the Pip Package or can be installed with Anaconda or Miniconda as it is part of the Anaconda distribution:   Install with PIP 1pip install pandas Install with Conda 1conda install pandas From Pandas DataFrame to SQL Here, we will read data from our computer using Pandas to a Dataframe. After then, we convert the Dataframe to a SQL Table. The SQLAlchemy create engine will also be used in the script to pass in our connection URL. 1#import our library 2import pyodbc 3import pandas as pd 4import urllib 5 6#import create engine from sqlalchemy 7from sqlalchemy import create_engine 8 9#this is what the credentials we’ll be passing in looks like 10server = ‘my-project.xxxxxxxxxxxxxs.amazonaws.com’ 11database = ‘myData’ 12username = ‘admin’ 13password = ‘xxxxxxxx’ 14 15#the file we are bringing in, we read it with pandas 16df= pd.read_csv(‘Python_Project.csv’) 17 18#we use the urllib_parse_quote to read in all our credentials to a url string 19quoted = urllib.parse.quote_plus(“DRIVER={ODBC Driver 17 for SQL Server}; SERVER=my-project.xxxxxxxxxxxxxs.amazonaws.com;DATABASE=myData;UID=admin;PWD=xxxxxxxx”) 20engine = create_engine(‘mssql+pyodbc:///?odbc_connect={}’.format(quoted)) 21#we convert the file to SQL Table 22df.to_sql(‘provide_the_name_for_our_table’, schema=’dbo’, con = engine)   From SQL to Pandas DataFrame Earlier we learned how to convert a Pandas DataFrame to SQL Table. We can also read SQL data with Pandas and convert it to a DataFrame. 1#import our libraries 2import pandas as pd 3import pyodbc 4 5#create a connection using your credentials 6the_conn = pyodbc.connect(‘Driver={SQL Server};’ 7 ‘Server=my-project.xxxxxxxxxxxxxs.amazonaws.com;’ 8 ‘Database=myData;’ 9 ‘Trusted_Connection=yes;’) 10#we use the pandas read sql_query along with our connection instance to retrieve our table 11df = pd.read_sql_query(‘SELECT * FROM students’, the_conn) 12#we then print our data 13print(df.head(n=5))  

Scroll to Top