Power BI Tutorial - Power BI Tutorial on Calculated Fields
Posted by Superadmin on August 20 2020 04:45:38
Power BI Tutorial - Power BI Tutorial on Calculated Fields
Sometimes, we don’t get the columns that we need to perform visualization. In these situations, we can create Power BI calculated fields that we have shown below. Within these columns, we can write simple arithmetic operations or the most powerful Power BI DAX functions.
- Calculated Columns
- Conditional Columns
- Calculated Measures
- Calculated Tables
- Custom Columns
Create Calculated Columns in Power BI
Microsoft Power BI allows you to create custom columns (or calculated columns) from existing columns. In this section, we show you how to create calculated columns in Power BI with example.
To demonstrate this Power BI calculated columns, we are going to use the SQL Data Source that we created in Connect Power BI to SQL Server article. So, refer to the same.
How to Create Calculated Columns in Power BI
In order to create a calculated column, please click on the New Column option under the Modeling tab.
Clicking the New Column tab opens the Power BI DAX formula bar, as we shown below.
You can rename this calculated column as per your requirement. Let me rename the column as Profit
For this Calculated Columns demo purpose, we select Sales Amount and Total Product Cost from Fact Internet Sales table. As you can see from the screenshot below, while I was typing, Power BI IntelliSense is showing the suggestions.
From the screenshot below, you can see, we are calculating the Profit by removing Total Product Cost from Sales Amount. Next, by clicking the enter, a new column called profit created.
Let me add the calculated column that we created earlier to the existing Table. Remember, this is the table that we created in the Create Table Report article.
Add Conditional Column in Power BI
Power BI Conditional column generates a new column based on the condition that we specify — for example, creating an appraisal column based on employee performance, etc. Let me show you how to add a Conditional Column in Power BI with example.
How to add Conditional Column in Power BI
To demonstrate this Power BI Conditional Column, we are going to use the SQL table that we imported in Enter Data article.
To add a conditional column, please click the Edit Queries option under the Home tab.
Clicking the Edit Queries option opens a new window called Power BI Power Query Editor. In this example, we are going to use the Sales column.
First, go to Add Column Tab. Under this tab, you can see the Conditional Column button. You can use this button to add the conditional column to this table.
Clicking the Power BI Conditional Column button opens the following window.
We changed the Column name as Rating. Next, we selected the Column name as Sales
Next, we selected the Operator as greater than, and value as 3000. Within the Values or Output text box, either you can give the static values, or you can select the column.
We added the first Rule – If Sales is greater than 3000, then return Excellent inside the rating Column. Next, click the Add rule button to add a new rule.
As you can see from the screenshot below, we added two more rules. Remember, this is the same as the Else If Statement in SQL.
You can use the ….. button to move selected Rule to up and down, or you can delete it as well. For now, we are leaving as it is, and click the OK button
From the screenshot below, you can see the new column with the information that we specified in the conditional column. Next, hit the Close & Apply option under the Home tab to apply these changes.
Now you can see the new Power BI Conditional Column called rating under the SQL Employees table.
Create Calculated Measures in Power BI
In Microsoft Power BI, you can create a custom calculated measure (or numeric aggregated calculation) from existing columns. In this article, we show you how to create calculated measures in Power BI with example.
To demonstrate this Power BI calculated measures, we are going to use the Data Source that we created in Connect Power BI to SQL Server article.
How to Create Calculated Measures in Power BI
In order to create a calculated measure, please click on the New Measure option under the Modeling tab.
Clicking the Power BI New Measure tab opens the DAX formula bar with default measures name, as we showed below.
You can rename this calculated measure as per your requirement. Let me rename the measure as Total Sale
For the Power BI calculated measures demo purpose, we add the Internet Sales Amount, Reseller Sales Amount from Fact Internet Sales, and Fact Reseller Sales tables. Unlike calculated columns, you have to use the aggregated function to create a measure.
As you can see from the screenshot below, while I was typing, Power BI IntelliSense is showing the suggestions.
By clicking the enter or any key, a new Measure called TotalSale created.
Let me add the calculated Measure that we created earlier to a Table. Please refer Create Table Report article to understand the steps involved in creating a table
Let me minimize the Fields tab and Visualization so that you can focus on table values.
You can also apply filters on these calculated measures too. From the screenshot below, you can see that we selected a few areas in our Pie Chart, and the same has been reflected in the table as well.
Create Calculated Tables in Power BI
In Power BI, you can create a custom table or calculated table from the existing table. In this article, we show you how to create calculated tables in Power BI with an example.
To demonstrate this Power BI Calculated Tables, we are going to use the SQL Data Source that we created in Connect Power BI to SQL Server article. So, please refer to the same.
How to Create Calculated Tables in Power BI
In order to add the calculated table, please click on the New Table option under the Modeling tab.
It will open the Power BI DAX formula bar to write DAX expression, as we have shown in the screenshot below.
Let me rename the default table name as Countries
For this Calculated Tables demo purpose, we are selecting Countries from the DimGeography table. As you can see from the screenshot below, while I was typing, Power BI IntelliSense is showing the suggestions.
From the screenshot below, you can see, we are selecting the DISTINCT (non-duplicates or unique) countries’ names from the Countries column.
By clicking the enter or any key for that matter will create a new table called Countries holding distinct country names.
Let me create a Map using that calculated table that we created earlier.
Create a Custom Column in Power BI
Power BI Custom column generates a new column based on the existing columns. For example, you can use this to calculate the profit or loss, etc. Let us see how to create a custom column in Power BI with an example.
How to create custom column in Power BI
To demonstrate this custom Column, we are going to use the Internet Sales table that we imported in Connect to SQL Data Source article.
To add or create a custom column, please click the Edit Queries option under the Home tab.
Clicking the Edit Queries option opens a new window called Power Query Editor.
In this example, we are going to calculate the profit using the Total Product Sale and the Sales Amount.
To add a custom column in the Power BI report, go to Add Column Tab. Under this tab, please click on the Custom Column button, as shown below.
Clicking the Custom Column button opens the following window.
We changed the Column name as Profit. Next, we are subtracting the total product from the sales amount.
Now you can see the new column profit
Let me rearrange columns so that you can see them side-by-side
Hit the Close & Apply option under the Power BI Home tab to apply these changes.