Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active September 29, 2022 20:44
Show Gist options
  • Save ninmonkey/20a9e805424e0ab4fff432647d5e627d to your computer and use it in GitHub Desktop.
Save ninmonkey/20a9e805424e0ab4fff432647d5e627d to your computer and use it in GitHub Desktop.
Using conditional variables in DAX.md

In dax studio (or tabular) You can output multiple tables, for different stages of your test

Codepoints are fun

  • This converts numbers (codepoints) to text
  • PBI blocks some codepoints, so you need to transform them
  • It preserves control chars by replacing them with symbols
  • For example: tabs becomes
  • Both IFERROR were required, because some codepoints threw an exception. (Null u+0000 IIRC)
Calculated Rune =
var curCodepoint = SELECTEDVALUE( Number[Codepoint] )
var x = IFERROR(
    SWITCH(
        TRUE(),
        // curCodepoint < 32, "Control Chars", // if you want to just cut it off
        curCodepoint < 33,
            UNICHAR( curCodepoint + 9216 ),  // = 0x2400
        curCodepoint = 127,
            UNICHAR( 9249 ),
        curCodepoint >= 1114111,
            "Too Big", // 0x10ffff
        IFERROR(
            UNICHAR( curCodepoint ), "Failed!"
        )
    )

, "OuterError")
return x

Links if you're curious

What Dec Hex
unicode-max 1114111 0x10ffff
[c0] . [0x0, 0x1f] inclusive
space 32 0x20
del 127 0x7f
9249 0x2421
Block Name Range
Ascii / latin 0x0000..0x007f
latin-1 suppliment 0x0080..0x00ff
Control Char Symbols 0x2400..0x243f
Plane Name Range
Private Use Areas plane 15 (U+F0000-U+FFFFD)
Private Use 2 plane 16 (U+100000-U+10FFFD)
Supplementary Multilingual Plane (SMP) 0x10000..0x1FFFF
Supplementary Ideographic Plane (SIP) 0x20000..0x2FFFF
Supplementary Special-purpose Plane (SSP) 0xe0000..0xEFFFF

I forget what this was for

DEFINE 
    VAR curStartTime = 0 
    VAR curEndTime = 20
    VAR prevEndTime = 0
    TABLE table1 = FILTER(Events, Events[ParentID] == 1)

    table table2 = CALCULATETABLE(
        table1,
        Events[EndTime] >= prevEndTime,
        Events[StartTime] < curEndTime             
    )

EVALUATE ADDCOLUMNS(
    table2,
    "Min Start",
    MIN(table2[EndTime] ),
    "Max End",
    MAX(
        table2[EndTime]
    )
)

EVALUATE table2

Changes color based on the date

ColorMeasure = 
    var delta = datediff( SelectedValue( [CurDate] ), TODAY(), DAY )
    var shouldColor = SelectedValue( [CurDate] ) > 300
    var curColor = switch(True()
        delta = 2, "red",
        delta = 1, "yellow",
        "black"
    )
    var finalValue = 
         if(shouldColor, curColor, "black" ) // I forget if "" or "white" or "black" is default
return
    finalValue 

Changes format strings the the extra ,

[ Dynamic UnitSize] :=
    // docs: <https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--custom-specifier-2>
    var CurVal = SELECTEDVALUE( Files[Bytes] )
    var cult = "en-us"       
    var fStr_perK = "#,#,"
    var fStr_perM = "#,#,,"
    var fStr_Labeled_perK = fStr_perK & " k"
    var fStr_Labeled_perM = fStr_perM & " m"
    var dynamicFStr = SWITCH(TRUE(),
        CurVal > 1000000, fStr_Labeled_perM,
        CurVal > 1000, fStr_Labeled_perK,
        ""
    )    
    VAR renderFinal = IFERROR( FORMAT( CurVal, dynamicFStr, cult ) , "error" )    
return renderFinal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment