Creating Custom Columns

Introduction to PowerQuery Custom Calculation Definitions
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.93
List Price:  €92.76
You save:  €27.83
£55.85
List Price:  £79.79
You save:  £23.94
CA$95.75
List Price:  CA$136.80
You save:  CA$41.04
A$105.88
List Price:  A$151.27
You save:  A$45.38
S$94.72
List Price:  S$135.33
You save:  S$40.60
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.46
List Price:  CHF 90.66
You save:  CHF 27.20
NOK kr757.20
List Price:  NOK kr1,081.77
You save:  NOK kr324.56
DKK kr484.44
List Price:  DKK kr692.10
You save:  DKK kr207.65
NZ$116.32
List Price:  NZ$166.18
You save:  NZ$49.85
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,685.71
List Price:  ৳10,980.05
You save:  ৳3,294.34
₹5,844.92
List Price:  ₹8,350.25
You save:  ₹2,505.32
RM331.68
List Price:  RM473.85
You save:  RM142.17
₦99,352.90
List Price:  ₦141,938.80
You save:  ₦42,585.90
₨19,422.12
List Price:  ₨27,747.07
You save:  ₨8,324.95
฿2,569.35
List Price:  ฿3,670.66
You save:  ฿1,101.30
₺2,257.93
List Price:  ₺3,225.75
You save:  ₺967.82
B$359.91
List Price:  B$514.18
You save:  B$154.27
R1,286.96
List Price:  R1,838.60
You save:  R551.63
Лв127.01
List Price:  Лв181.45
You save:  Лв54.44
₩95,596.49
List Price:  ₩136,572.27
You save:  ₩40,975.78
₪260.69
List Price:  ₪372.44
You save:  ₪111.74
₱4,021.38
List Price:  ₱5,745.07
You save:  ₱1,723.69
¥10,900.73
List Price:  ¥15,573.14
You save:  ¥4,672.41
MX$1,172.87
List Price:  MX$1,675.60
You save:  MX$502.73
QR254.79
List Price:  QR364.01
You save:  QR109.21
P953.47
List Price:  P1,362.16
You save:  P408.69
KSh9,270.57
List Price:  KSh13,244.24
You save:  KSh3,973.67
E£3,317.36
List Price:  E£4,739.29
You save:  E£1,421.93
ብር4,002.19
List Price:  ብር5,717.66
You save:  ብር1,715.47
Kz58,536.37
List Price:  Kz83,626.97
You save:  Kz25,090.60
CLP$64,740.75
List Price:  CLP$92,490.75
You save:  CLP$27,750
CN¥505.65
List Price:  CN¥722.39
You save:  CN¥216.74
RD$4,053.41
List Price:  RD$5,790.83
You save:  RD$1,737.42
DA9,418.25
List Price:  DA13,455.22
You save:  DA4,036.97
FJ$158.92
List Price:  FJ$227.04
You save:  FJ$68.12
Q543.91
List Price:  Q777.05
You save:  Q233.14
GY$14,591
List Price:  GY$20,845.18
You save:  GY$6,254.17
ISK kr9,761.50
List Price:  ISK kr13,945.60
You save:  ISK kr4,184.10
DH700.89
List Price:  DH1,001.31
You save:  DH300.42
L1,242.28
List Price:  L1,774.76
You save:  L532.48
ден4,000.60
List Price:  ден5,715.39
You save:  ден1,714.79
MOP$561.14
List Price:  MOP$801.67
You save:  MOP$240.52
N$1,290.58
List Price:  N$1,843.76
You save:  N$553.18
C$2,565.92
List Price:  C$3,665.76
You save:  C$1,099.83
रु9,317.52
List Price:  रु13,311.31
You save:  रु3,993.79
S/259.50
List Price:  S/370.73
You save:  S/111.23
K270.29
List Price:  K386.14
You save:  K115.85
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,908.05
List Price:  ZK2,725.90
You save:  ZK817.85
L323.12
List Price:  L461.62
You save:  L138.50
Kč1,619.42
List Price:  Kč2,313.56
You save:  Kč694.13
Ft25,184.29
List Price:  Ft35,979.10
You save:  Ft10,794.81
SEK kr758.43
List Price:  SEK kr1,083.53
You save:  SEK kr325.09
ARS$61,764.46
List Price:  ARS$88,238.72
You save:  ARS$26,474.26
Bs483.72
List Price:  Bs691.07
You save:  Bs207.34
COP$271,807.63
List Price:  COP$388,313.26
You save:  COP$116,505.63
₡35,677.52
List Price:  ₡50,970.07
You save:  ₡15,292.55
L1,718.38
List Price:  L2,454.93
You save:  L736.55
₲523,713.94
List Price:  ₲748,194.84
You save:  ₲224,480.90
$U2,690.81
List Price:  $U3,844.18
You save:  $U1,153.37
zł279.12
List Price:  zł398.76
You save:  zł119.64
Already have an account? Log In

Transcript

This chapter has been about combining columns in different ways, either through mathematical computations or logical decisions, but so far, never both simultaneously. as powerful as the individual features can be. They add a bunch of steps if used individually and can make arriving at a desired outcome cumbersome and difficult if the number of steps and order requirements are high, but those issues are moot with the custom column formula wizard. For this data set, we have a series of transactions, some coupons and a pair of requirements for each coupon, a minimum transaction amount for the coupon to apply and a coupon expiration date. For our coupon to be applicable, the amount must be greater than the minimum and our date must be on or before the expiration date. Even though this is a combination of logical outcomes, we can still use a multi step process to calculate whether the coupon applies by creating two columns and multiplying them together.

The first column would calculate whether the coupon has expired by taking the date and determining whether it comes after the expiration date. In such a scenario, it would return zero, otherwise it would return a value of one. The second column would calculate whether the coupons minimum amount has been met by comparing the transaction amount to that coupon minimum. When the transaction exceeds the minimum, it'll return one, otherwise return zero. Taking the product of these two fields will only result in one when both the coupon is not expired and the minimum has been reached. But this three step process may not be too obvious for all users.

And alternative and in this case, easier approach would be to use a cascading if then else conditional column to apply the rules one after the other and return a zero whenever a rule is broken. This checks to see if the expiration date has passed and returns zero then, if it hasn't passed, then it checks to see if the transaction sufficient and return zero if it isn't. Otherwise, it assumes the coupon is applicable in return. So one. For simple and logical requirements, this is too challenging. However, any significant complexity, or either or requirements will create a long series of statements can quickly become unmanageable.

In this scenario, we're looking at a calculation that can be handled far better and far easier. With a custom column formula, we need to create a custom conditional formula. And to do this, let's use our formula bar as a learning tool. I'm going to click on the added conditional column command and see what it did. Initially, your formula bar may not be visible, so you'll need to turn it on by going to the view ribbon and selecting the check mark next to Formula Bar. Once visible, the formula bar will show the full command performed along the step.

In this case, hit called table dot Add Column and did a bunch of stuff inside that function. The power that we're interested in is the part that comes immediately after the each keyword and before the close parenthesis. This is the function that we created with our conditional formula wizard. The command reads if amount greater than or equal to minimum, then one else zero. Moving to our latest step, we can go on and create a conditional command using the custom function that performs our data exploration calculation like so. We'll give the column a name, and then we'll write if, and just a quick note, keywords must be lowercase capital if would generate an error.

And then to insert the date field, I'll double click on it in my list of available columns. All right, less than or equal to and double click my expires one column. I'll add then one else zero to finish out my statement. And we can see that I have no syntax errors detected which is good. And then I'll press OK to create the column just as we've defined Even adds the table column extra piece to so that it looks identical to our prior command. Since the command does look identical to our prior one, what happens if we try to edit it?

Well, the query editor rightly recognizes it as a simple conditional statement. And so we'll open it back up in the conditional editor wizard. So let's go ahead and delete this command and add a slightly more complex if statement. So this is statement, I'm going to combine our two logical conditions into a single statement using the Add keyword. There's both an and and an or keyword, which can be used in conjunction with parentheses to construct as elaborate a logical statement as needed to determine whether to execute one command or another. In this statement, I want to evaluate whether the date is less than or equal to the expiration date, and whether the amount is greater than or equal to the minimum requirement.

When both of those statements are true, I want to return a one. Otherwise I'll return zero quickly comparing this against our other approach, we can see an identical set of results. But now, if we re edit our custom calculation, we were put back into the add custom calculation wizard, rather than our conditional column wizard, since our logic is more complex, since we're back in this editor, there are other commands that we can apply quickly and easily within these formulas as well like simple arithmetic, rather than calculating whether the coupon applies. But if we calculated the coupons value, instead of than one, all we need to do is replace the one with the calculation that we desire when the statement is true. In this case, that's the amount of the coupon coupon one, multiply it against the original amount field. The other standard operators such as addition, subtraction, and division can all be done this way as well.

So let's rename this to coupon one value. Now as with any other calculations, order of operations can be key. This is very simple calculation. However, I can add parentheses around my coupon one times amount To specify that the calculation belongs as a single unit. Now I'll press OK to create the column. Since I want this column for my secondary coupon to, I can simply re edit my coupon one calculation, copy the formula, create a new custom column, and paste that formula for editing.

In this case, the only things that need modification are the column names, I do not need to delete the column names entirely and re double click on the correct columns. Instead, what I can do is I can simply modify the column names since they are just like references in an excel formula. pressing OK quickly gives me my coupon to value. This covers the initial basics of creating custom formulas using if then else statements with and or logical constructions and basic mathematical calculations. With this, you'll be able to create the custom calculations necessary to solve the following Practice set designed around scoring a blackjack light card game and the follow up lesson that will include this chapter. I'll open Pandora's box by introducing you to the function library, showing you some ways to use the prior wizards to learn about the functions, and finally leaving you with Microsoft's m Reference Library, which lists all of the available formula functions.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.