Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

FOR loop equivalent in DAX?

Hi all,

 

I'm looking to get feedback on how I could acheive something similiar to a loop in DAX. I'm familiar with SWITCH and already using it (as you can see in the code below) however I'm faced with a significant scaling issue.

 

Per the code below I have three variables I need to evaluate for each condition to use a specific measure when that condition is met. There are 8 Sites, 6 Care Settings and each Care Setting has about 6 Categories (varies). So the current code sample is a smaller representation of something I need to scale significantly. Does anyone have suggestions on how I can dyanimcally iterate through the various comibinations or make use of more variables? Can I "create" a measure name based on variables? Other suggestions on all the conditions? If I was in C# or VB (not that I'm terribly profecient in either) I would use a loop to iterate through all the possible combinations and set the values properly.

 

Thoughts? Ideas? Trying to avoid a huge chunk of time writing a ton of repetitive code and the associated risk of making mistake(s).

 

Thanks!

 

% of Prog = 
//variables for Columns
    VAR CareSetting = MAX(prog_SiteProgram[prog_CareSettings.ServiceName])
    VAR Category = MAX(prog_SiteProgram[prog_Categories.Name])
    VAR Site = MAX(prog_SiteProgram[site_Sites.Abbreviated Name])

RETURN
    SWITCH(
        TRUE(),

        CareSetting = "Acute Care" &&
        Category = "Ambulatory" &&
        Site = "GH",
        [1.acu.a Value],

        CareSetting = "Acute Care" &&
        Category = "Inpatient" &&
        Site = "GH",
        [1.acu.i.%Prog],

        CareSetting = "Acute Care" &&
        Category = "Diagnostic" &&
        Site = "GH",
        [1.acu.d Value],

        CareSetting = "Acute Care" &&
        Category = "Treatment" &&
        Site = "GH",
        [1.acu.t Value],

        CareSetting = "Administration" &&
        Category = "Administration" &&
        Site = "GH",
        [1.admin Value],

        BLANK()
    )

 

Note, in case you're wondering "what the heck is this guy doing", the reason for what appear to be individual measures being applied to each row is the values are coming from indiviual WhatIf Parameters driven by "sliders" (slicers). So I have to tabulate all those unique values into a table so I can do further calculates based on the values set by the end user. I've proven out the majority of my infrastructure, but now I've looped back to scaling this out for the whole data model.

8 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Greg_Deckler I'll take a look.

       

      Does this mean you don't think I'm crazy, and there is a ray of hope?

       

      Thanks,

      -R

      • Greg_Deckler's avatar
        Greg_Deckler
        Community Champion
        Well, I wrote a blog article on how to emulate a FOR loop and a WHILE loop in DAX. Also an article on emulating recursion. You're not crazy.