Formatting a Calculated Field

SantasLittleHelper

Board Regular
Joined
Nov 25, 2016
Messages
77
I have a field with the expression below: (I've made the fields generic so they are easier to understand)

Code:
IIF(Field1='Example Text',[IDCode] & [IDCounty],[IDCode] & [IDNumber])

This table hopefully summarises what is currently happening:
Field1IDCodeIDCountyIDNumberCalculated FieldWhat I want
Example TextABCambridgeshire(Null)ABCambridgeshireABCambridgeshire
Different TextGH(Null)00023GH23GH00023
Different TextPI(Null)00467PI467PI00467

<tbody>
</tbody>

Is there a way to format the Calculated field so that if the ID Number is used it, keeps any leading zeros?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
So, I take it that IDNumber must actually be a numeric field with custom formatting, if leading zeroes are being dropped.

Try:
Code:
IIF(Field1='Example Text',[IDCode] & [IDCounty],[IDCode] & FORMAT([IDNumber],"00000"))
 
Last edited:
Upvote 0
So, I take it that IDNumber must actually be a numeric field with custom formatting, if leading zeroes are being dropped.

Try:
Code:
IIF(Field1='Example Text',[IDCode] & [IDCounty],[IDCode] & FORMAT([IDNumber],"00000"))


Thanks Joe, I've just tried this but when I go to save, it says "The expression cannot be used in a calculate column"

Do you have any other ideas at all?
 
Upvote 0
Are you doing this in a query or table?
I am guessing that you may be doing it in a table.

Though the new versions of Excel allow you to do calculated columns in a table, it is recommended that you do not do this, for a few reasons, the top two of which are:
- There are restrictions regarding the type of calculations you can do
- No other database platform supports this functionality, so if you needed to convert this to something like a SQL database down the road, this would cause issue

So it is recommended that you do all calculated fields in queries instead of at the table level. There is really no reason to do them at the table level. Pretty much anything you can do with a table you can use a query for. So you really do not lose anything by doing the calculations in a query.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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