Need assistance with Power Query If Statement for custom columns

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I have a column that has case quantities in them, due to the nature of the data entry sometimes this column has some text in it which messes up the conversions. I am trying to make an if statment that will see if the value contains text and transform it appropriately.

Quick example, the three columns that wold be in use (3rd row after the headers is where I need the if statement to work):

CasesPack SizePots
51575
1018180
12 potsOtherERROR
1018180

What I would want the if statement to do is the following: If [pack size] = "Other" then Pots = [cases] but removing the 5 rightmost characters so that it would just be 12
Else
[cases]*[pack size]

Hoping someone could help me out in the proper syntax for achieving the above.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try ... Otherwise, can work like this
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cases", type any}, {"Pack Size", type any}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Pots", each try [Cases]*[Pack Size]
otherwise Number.FromText(Text.Select([Cases],{"0".."9"})))
in
    #"Added Custom"
 
Upvote 0
Try ... Otherwise, can work like this
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cases", type any}, {"Pack Size", type any}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Pots", each try [Cases]*[Pack Size]
otherwise Number.FromText(Text.Select([Cases],{"0".."9"})))
in
    #"Added Custom"
When I paste the code it tells me the column cases wasn't found because the original data source is a dynamic range and I clean it up to get the proper headers through transformation.
 
Upvote 0
What are the column names in your Power Query?
The formula should work in an added custom column:
Power Query:
try [Cases]*[Pack Size]
otherwise Number.FromText(Text.Select([Cases],{"0".."9"}))
 
Upvote 0
What are the column names in your Power Query?
The formula should work in an added custom column:
Power Query:
try [Cases]*[Pack Size]
otherwise Number.FromText(Text.Select([Cases],{"0".."9"}))
The column names in the Power Query are as shown in the table above, Cases, Pack Size, Etc.
 
Upvote 0
What are the column names in your Power Query?
The formula should work in an added custom column:
Power Query:
try [Cases]*[Pack Size]
otherwise Number.FromText(Text.Select([Cases],{"0".."9"}))
I got this to partially work, it now puts the number for pots (12 in the example I had placed above) but it gives Error for everything else that is a straight multiplication. It says

Expression.Error: We cannot convert the value 2222 to type Text.
Details:
Value=2222
Type=[Type]
 
Upvote 0
Hi,

Do you have a step with Change Type? Delete it, so we keep it a variant, or better any, type. Numbers are treated as numbers and text as text. Else you need to use
Power Query:
Number.FromText (Column)
for every single argument of the formula.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,713
Messages
6,174,041
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top