Creating Conditional Columns

Introduction to PowerQuery Custom Calculation Definitions
4 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

In this lesson, we'll be diving into the conditional column command, and using it to create new columns that are based upon conditions in one or more other columns. So let's take a look at this data set and construct a basic conditional test. Want to see if the sale price was greater than the wholesale price? We can use a series of steps to define this first by subtracting wholesale price from sales price, identifying the sign of the results column, and then grouping by that side column to see how many gains losses and breakeven transactions were made. This gives us 3020 gains, 2844 losses, and 118 breakevens across three calculation steps with little customization allowed. Now let's look at how we can construct this with our conditional column command and possibly create some more flexibility So let's go ahead and outline our logic.

If our sale price is greater than our wholesale price, then we've gained otherwise we've lost. With our initial logic outlined, let's create our conditional column. Step one is to simply give our new column a name, like was gained. Now we need to translate our conditional statement into the various inputs. The first part says if sales price is greater than wholesale price, so the sales price and wholesale prices are both columns. So I can select sales prices, the first entry, the second entry, while I said greater than I actually want greater than or equal to capture breakevens is a game so I'll select is greater than or equal to this leaves wholesale price is the third entry so I can select column from this drop down and then select wholesale prices.

My comparison column when this combination of statements is true, that is when sales price is greater than or equal to wholesale price that I want my column to equal plus one so that goes in the output field. In a Any other scenario, I want my column to equal minus one. So that goes in the otherwise field. This conditional column recreates most of what we had previously done with the subtraction and sign commands, I can recreate the group by to see our gains of 3138, and losses of 2844. Our breakevens have been tied into our gains. This was a simple statement.

So let's go back and revise it a bit. Let's add a new rule which gives us a whole additional line for a new condition when sales price equals wholesale price, then let's equals zero pressing OK to update the results in going to the group by I can see that nothing has changed. Why is that? Well, the conditional column was it will interpret the conditions in order from top to bottom since our greater than or equal to encompasses the equals condition, it will never trigger. So we need to change it to a simple greater than this somewhat demonstrates the importance of order, but let's expand our conditional calculation one More time and add a rule specifically for losses. So if sales price is less than or equal to wholesale price, then I'll return negative one.

Otherwise, I'll return 999. This way, I would be able to quickly see if any comparisons failed as an error. Our results are exactly as we'd expect. But suppose they weren't, we can edit our conditional column and quickly move our command up or down with the ellipsis commands to the right, move to our newest addition up one will cause the less than or equals to condition to prevent our equals condition from triggering. I can press OK, and go to the group rows to quickly confirm those results. Fixing the command is as easy as re editing and moving the new command back down.

Or if I so desired, I could delete an entire condition with the Delete command within the ellipsis. As you can see, the conditional column wizard is really quite easy and intuitive to use. It makes it very easy to create Simple if then else statements for creating conditional outcomes. While we did not do so in this lesson, you could include other columns as the outputs or the otherwise output instead of a basic value. In our next lesson, we'll dive into the more generalized add custom column command and start learning how to write our own calculation equations.

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.