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:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Field1[/TD]
[TD]IDCode[/TD]
[TD]IDCounty[/TD]
[TD]IDNumber[/TD]
[TD]Calculated Field[/TD]
[TD]What I want[/TD]
[/TR]
[TR]
[TD]Example Text[/TD]
[TD]AB[/TD]
[TD]Cambridgeshire[/TD]
[TD](Null)[/TD]
[TD]ABCambridgeshire[/TD]
[TD]ABCambridgeshire[/TD]
[/TR]
[TR]
[TD]Different Text[/TD]
[TD]GH[/TD]
[TD](Null)[/TD]
[TD]00023[/TD]
[TD]GH23[/TD]
[TD]GH00023[/TD]
[/TR]
[TR]
[TD]Different Text[/TD]
[TD]PI[/TD]
[TD](Null)[/TD]
[TD]00467[/TD]
[TD]PI467[/TD]
[TD]PI00467[/TD]
[/TR]
</tbody>[/TABLE]

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

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,223,565
Messages
6,173,089
Members
452,501
Latest member
musallam

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