Basic.FileSize

Format file sizes with automatic KB/MB/GB/TB scaling. Converts byte values into human-readable file size format strings.

Syntax

DaxLib.FormatString.Basic.FileSize ( )

Definition

DAX
// Format file sizes with automatic unit detection
// Returns human-readable format like "1.2 GB", "512 MB", "2.3 KB"
() =>
    VAR _Bytes = ABS( SELECTEDMEASURE () )

    VAR _Unit =
        SWITCH (
            TRUE (),
            _Bytes >= 1099511627776, " TB",
            _Bytes >= 1073741824, " GB",
            _Bytes >= 1048576, " MB",
            _Bytes >= 1024, " KB",
            " bytes"
        )

    VAR _Value =
        SWITCH (
            TRUE (),
            _Bytes >= 1099511627776, _Bytes / 1099511627776,
            _Bytes >= 1073741824, _Bytes / 1073741824,
            _Bytes >= 1048576, _Bytes / 1048576,
            _Bytes >= 1024, _Bytes / 1024,
            _Bytes
        )

    VAR _Decimals =
        IF ( _Unit = " bytes", 0, 1 )

    VAR _FormatString =
        IF ( _Decimals = 0, "#,##0", "#,##0.0" )

    VAR _Result =
        -- Avoids values or units being interpreted as part of the format string
        DaxLib.FormatString.Component.MakeStringSafe(
            FORMAT ( _Value, _FormatString ) & _Unit
        )

    RETURN
        _Result

Parameters

This function takes no parameters.

Example

DaxLib.FormatString.Basic.FileSize ()

Use this function in a measure's formatStringDefinition to apply file size formatting with automatic scaling.

Related Functions