HomeBusiness IntelligenceFast Ideas: Including Main Zero to Integer Values (Padding) with DAX and...

# Fast Ideas: Including Main Zero to Integer Values (Padding) with DAX and Energy Question

There are some instances that we need to add a number one zero to a digit, comparable to exhibiting 01 as a substitute of 1, 02 as a substitute of 2 and so forth. We have now two choices to do that in Energy BI, doing it in Energy Question or doing it with DAX.

## Including a Main Zero in Energy Question

The primary technique is doing it in Energy Question utilizing the `Textual content.PadStart()` perform.

Right here is how the syntax of the perform:

``Textual content.PadStart(textual content as nullable textual content, rely as quantity, elective character as nullable textual content)``

And right here is how the perform works:

``Textual content.PadStart(enter string, the size of the string, an elective character to be added to the start of the string util we attain to the string size)``

For instance, `Textual content.PadStart("12345", 10 , "a")` returns `aaaaa12345` and `Textual content.PadStart("1", 2 , "0") `returns `01`.

Let’s create an inventory of integer values between 1 to twenty with the next expression:

``{1..20}``

Now we convert the checklist to a desk by clicking the To Desk button from the Rework tab:

Now we add a brand new column by clicking the Customized Column from the Add Column tab from the ribbon bar:

Now we use the next expression within the Customized Column window to pad the numbers with a number one zero:

``Textual content.PadStart(Textual content.From([Number]), 2, "0")``

Listed here are the outcomes:

And the final step is to right the columns’ knowledge sorts by deciding on all columns (press CTRL + A) then clicking the Detect Information Kind button from the Rework tab from the ribbon.

Ultimately we click on Shut & Apply to load the info into the info mannequin.

## Including a Main Zero with DAX

I’m an enormous fan of caring for any type of transformation actions in Energy Question. However, in some instances, we need to add a number one zero to a quantity simply to format the quantity. I imply, including a number one zero to numbers just isn’t essentially a change exercise. It’s possible you’ll need to pad the outcomes of a measure with a number one zero if the quantity is between 0 and 10. The next technique works regardless although. And… it is rather easy. Less complicated than you suppose. We simply want to make use of the `FORMAT()` perform in DAX. The output of the perform is a string.

The syntax of the FORMAT capabilities is:

``FORMAT(<worth>, <format_string>)``

And right here is how the perform works:

``FORMAT(a single worth or an expression that returns a single worth, a format string)``

The formatting template of the perform is the place all of the magic occurs. There’s a variety of formatting templates together with predefined ones and customized formatting.

Right here is how we pad a number one zero with DAX:

``FORMAT(<a numeric worth>, "0#")``

We simply want to make use of the above sample in our calculations both within the calculated columns or measures. In our instance, we add a calculated column, so right here is the DAX expression for the calculated column:

``Quantity with Main Zero in DAX = FORMAT('Main Zero'[Number], "0#")``

That’s it.

However wait, what if our checklist of numbers ranging from 0? Let’s change our pattern knowledge in Energy Question so the checklist begins from 0, and cargo the info into the mannequin once more. Here’s what we get:

Hmm! That doesn’t look good!

Right here is the answer in Energy Question:

``````if [Number] = 0
then "0"
else Textual content.PadStart(Textual content.From([Number]), 2, "0")``````

It’s possible you’ll suppose that we will use the identical logic in DAX utilizing `IF()` perform, which we undoubtedly can, however wait; I need to present you a greater trick. Right here is the DAX expression with out utilizing `IF()`:

``Quantity with Main Zero in DAX = FORMAT('Main Zero'[Number], "0#;;0")``

Every format string can have as much as 4 sections. We are able to separate every formatting part utilizing a semicolon (;). If the format string has one part then it applies to all values, in any other case:

• The primary part applies to constructive values
• The second part applies to unfavourable values
• The third part applies to zeros
• The forth part applies to `Null` values

So, the format string of the latter DAX expression (`"0#;;0"`) add a number one zero to every integer worth, but when the worth is zero, then it exhibits zero.

If you wish to study extra about Information Modelling with Energy BI, make certain to get your copy of my ebook, Skilled Information Modeling with Energy BI which is offered on a number of platforms.

There’s one other situation that won’t even require including a brand new calculated column with padded values. Suppose you may have a desk with an Index column, similar to what I’ve within the above instance and I simply need to present the padded values. In that case, I don’t even want so as to add a calculated column. Certainly, I can format the Quantity column to indicate the padded integer values. Let’s see how it’s attainable:

• Choose the Quantity column
• Use the next formatting string within the Format dropdown of the Formatting part from the Column instruments from the ribbon
``"0#;;0"``

That is very cool, after we format values, we’re not altering the info sort. So after formatting the values, they’re nonetheless numeric values, which in my instance it’s Complete Quantity.

Simple!

## Bonus Merchandise

This bonus is for individuals who learn this text by the tip. Do you know that you would be able to convert integer date values to Date utilizing the `FORMAT() `perform comparable to changing 20210910 to 10/09/2021?

Right here it’s:

``DATEVALUE(FORMAT(20210910, "0000/00/00"))``

You’ll be able to obtain the PBIX file from right here.

Take pleasure in!

RELATED ARTICLES