Power Query Custom Column error: Token Eof expected

Frank J

Board Regular
Joined
Feb 17, 2011
Messages
104
Office Version
  1. 365
Platform
  1. Windows
I've got a formula in Power Query I'm trying to use to read another column and assign a number for each quarter based on month. I can't figure out what's wrong, any help is appreciated.


=if [Month]="Jan" then 1 else [Month]="Feb" then 1 else [Month]="Mar" then 1 else [Month]="Apr" then 2 else [Month]="May" then 2 else [Month]="Jun" then 2 else [Month]="Jul" then 3 else [Month]="Aug" then 3 else [Month]="Sep" then 3 else [Month]="Oct" then 4 else [Month]="Nov" then 4 [Month]="Dec" then 4 else null


1623433468736.png
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Are you actually importing a field called [Month] ?
If your "Month" is based on a date field and given that your quarters are calendar quarters it would be much cleaner to use the quarter function.

The formula will look something like this:-

Power Query:
= Table.AddColumn(#"Removed Columns", "Quarter", each Date.QuarterOfYear([Invoice_Date]), Int64.Type)

Generated via the menu as follows:

1623485674622.png
 
Upvote 0
Solution
Are you actually importing a field called [Month] ?
If your "Month" is based on a date field and given that your quarters are calendar quarters it would be much cleaner to use the quarter function.

The formula will look something like this:-

Power Query:
= Table.AddColumn(#"Removed Columns", "Quarter", each Date.QuarterOfYear([Invoice_Date]), Int64.Type)

Generated via the menu as follows:

View attachment 40664
That worked perfectly, thx so much. I'm new to this and self taught.
 
Upvote 0
Are you actually importing a field called [Month] ?
If your "Month" is based on a date field and given that your quarters are calendar quarters it would be much cleaner to use the quarter function.

The formula will look something like this:-

Power Query:
= Table.AddColumn(#"Removed Columns", "Quarter", each Date.QuarterOfYear([Invoice_Date]), Int64.Type)

Generated via the menu as follows:

View attachment 40664
I have another now giving me the same error for a different reason, it's hung up on the first "Roulette". The Game Type is an added column from the imported Asset Number and using Text.Start pulling out the first 2 letters. I'm trying to categorize all the different types to make another filter column for later use, after I create this column I'll then make another to further refine the game locations by joining the Category with the Area.

Game Category:

=if [Game Type]="MB" then "Baccarat" else [Game Type]="RW" then "Roulette" else [Game Type]="RT" then "Roulette" else [Game Type]="CR" then "Craps" else [Game Type]="CC" then "Craps" else [Game Type]="BJ" then "Blackjack" else [Game Type]="BP" then "Blackjack" else [Game Type]="JP" then "Blackjack" else [Game Type]="DD" then "Double Deck" else [Game Type]="SD" then "Blackjack" [Game Type]="PJ" then "Blackjack" else [Game Type]="JS" then "Blackjack" else [Game Type]="HH" then "Novelty" then [Game Type]="HC" then "Novelty" else [Game Type]="TC" then "Novelty" else [Game Type]="TP" then "Novelty" else [Game Type]="PG" then "Novelty" else [Game Type]="BS" then "Novelty" [Game Type]="RP" then "Novelty" else [Game Type]="RF" then "Novelty else null

1623512654402.png
 
Upvote 0
In both Excel and Power Query it is bad practice to hard code a whole heap of alternatives into an "if statement". It makes the formula hard to read and high maintenance.
The minute you see an if statement like that you should immediately be thinking of using a lookup table.
In Excel you would then use vlookup, xlookup or index/match.
In Power Query, you would pull in the lookup table into PQ as a connection only query. Select your main query, merge it with the lookup query by doing a left join to the lookup table, using your Game Type field to link the 2 tables.
One of the significant benefits of using Query tools such as PQ, it the ability to join information from multiple sources ie do lookups. It is much better at doing lookup than Excel is and can easily do lookups requiring multiple fields to do the lookup. (Even the Data Model only does single field lookups)
 
Upvote 0
In both Excel and Power Query it is bad practice to hard code a whole heap of alternatives into an "if statement". It makes the formula hard to read and high maintenance.
The minute you see an if statement like that you should immediately be thinking of using a lookup table.
In Excel you would then use vlookup, xlookup or index/match.
In Power Query, you would pull in the lookup table into PQ as a connection only query. Select your main query, merge it with the lookup query by doing a left join to the lookup table, using your Game Type field to link the 2 tables.
One of the significant benefits of using Query tools such as PQ, it the ability to join information from multiple sources ie do lookups. It is much better at doing lookup than Excel is and can easily do lookups requiring multiple fields to do the lookup. (Even the Data Model only does single field lookups)
So I should just start a new sheet and make it a table with those statements in a column format 2 wide, then import it to the PQ as a list? I get the connection only part and what you mean but to bring it in...
 
Upvote 0
So I should just start a new sheet and make it a table with those statements in a column format 2 wide, then import it to the PQ as a list? I get the connection only part and what you mean but to bring it in...
I got it to work, thanks so much. this really will save me a lot of time as I need to do it 12 times.
 
Upvote 0
Glad you were able to work it out and I appreciate you letting me know.
Sorry I couldn't engage in real time but I am in Australia and at that point it was the middle of the night here.

I would just like to address this statement though.
I need to do it 12 times.
Typically in PQ
  • You finish up with a workbook that you just need to refresh when you get new data
    (ideally keeping a backup / archive version of the previous version)
  • If the 12 files already exist then you generally use PQs ability to import all the files in a folder at the one time using its combine function.
I assume you worked out that you had to do something like the below.
(if Game Code wasn't unique and you needed to do the lookup on more than just 1 field, in PQ that is just a few more clicks in this screen while in Excel the formula would quickly get more complicated)

1623800027289.png
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
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