Forum Discussion

OPS-MLTSD's avatar
OPS-MLTSD
Post Patron
4 years ago

What does this formula mean?

Hello everyone,

 

I have a dashboard that I am trying to learn from and I cam across this dax below, if someone could please explain to me what this formula means, that would be much appreciated. Also please let me know if you require additional info, I will try my best to upload what data I am allowed to upload:

 

 

Clients = VAR CLIENT_IND =CALCULATE(SUM('Table1'[Targets achieved to date (#)),FILTER(Table1',AND('Table1'[CATEGORY]="Number of Clients",'Table1'[PROCESSLOGID]=EARLIER(Table1'[PROCESSLOGID])))) RETURN

VAR CLIENT_SURVEY = IF(and(calculate(MAX('Table2'[Report Submission#]) = "Activity Report#1"),calculate(max(Table3[type])="5C")), calculate(SUM(Table4[INTAKE])), BLANK())

RETURN

IF(NOT(ISBLANK(calculate(COUNTROWS('Table1'),FILTER('Table1',AND('Table1'[CATEGORY]="Number of Clients",'Table1'[PROCESSLOGID]=EARLIER('Table1'[PROCESSLOGID])))))),CLIENT_IND,CLIENT_SURVEY)

9 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    OPS-MLTSD Looks like a column formula, yes? It's really pretty atrocious DAX in my opinion.

     

    The CLIENT_IND variable is calculating the sum of the Targets achieved to date (#) column in the table Table1 filtered to where the CATEGORY column is "Number of Clients" and the PROCESSLOGID is equal to the "current" value of PROCESSLOGID (EARLIER). The current row's value in other words. The next variable, CLIENT_SURVEY is saying that if the maximum value in Table2 of the column Report Submission #, assuming a related table is "Activity Report#1" and the maximum value of the type column in Table3 is "5C" then return the sum of the INTAKE column in Table4, otherwise it is set to BLANK() (null). The final return statement is if the number of rows in Table1 that have a CATEGORY of "Number of Clients" and where the PROCESSLOGID is equal to the current row's PROCESSLOGID is not BLANK() (null or empty) then return the CLIENT_IND variable value, otherwise if it is blank, return the CLIENT_SURVEY variable value.

    • OPS-MLTSD's avatar
      OPS-MLTSD
      Post Patron

      Greg_Deckler Thank you so much for the explanation! haha I agree with you, this dax is rather atrocious! Just wondering if you have any idea why the EARLIER function is used here in the first place? It is throwing me off

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    OPS-MLTSD EARLIER is a function that was created before VAR/RETURN was added to DAX. In many ways you can think of it is "current" value in the row. The name comes from the fact that it is returning the "earlier" context. So, in your CALCULATE you have the context created by the filtering parameters so the EARLIER function returns the value of that item prior to that context, which ends up being the row context of the current row. Instead of using EARLIER, you could instead use an additional prior VAR like:

     

    VAR __ProcessLogID = Table1'[PROCESSLOGID]

     

    Now, there are some instances where EARLIER is much more convenient, etc. In Defense of EARLIER - Microsoft Power BI Community

     

    Also, that code seems to overuse CALCULATE, etc. Anyway, I wouldn't have written that code that way I don't think but hard to know for sure without knowing your data better.

     

    • OPS-MLTSD's avatar
      OPS-MLTSD
      Post Patron

      Greg_Deckler I see good to know thanks!

       

      So the formula could have been written this way and it technically could have worked?

       

      Clients =

      VAR __ProcessLogID = Table1'[PROCESSLOGID]

      VAR CLIENT_IND =CALCULATE(SUM('Table1'[Targets achieved to date (#)),FILTER(Table1',AND('Table1'[CATEGORY]="Number of Clients", VAR __ProcessLogID))) RETURN

      VAR CLIENT_SURVEY = IF(and(calculate(MAX('Table2'[Report Submission#]) = "Activity Report#1"),calculate(max(Table3[type])="5C")), calculate(SUM(Table4[INTAKE])), BLANK())

      RETURN

      IF(NOT(ISBLANK(calculate(COUNTROWS('Table1'),FILTER('Table1',AND('Table1'[CATEGORY]="Number of Clients", VAR __ProcessLogID))))),CLIENT_IND,CLIENT_SURVEY)

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion

        OPS-MLTSD Close:

        VAR __ProcessLogID = Table1'[PROCESSLOGID]
        
        VAR CLIENT_IND =CALCULATE(SUM('Table1'[Targets achieved to date (#)),FILTER(Table1',AND('Table1'[CATEGORY]="Number of Clients", [PROCESSLOGID] = __ProcessLogID))) RETURN
        
        VAR CLIENT_SURVEY = IF(and(calculate(MAX('Table2'[Report Submission#]) = "Activity Report#1"),calculate(max(Table3[type])="5C")), calculate(SUM(Table4[INTAKE])), BLANK())
        
        RETURN
        
        IF(NOT(ISBLANK(calculate(COUNTROWS('Table1'),FILTER('Table1',AND('Table1'[CATEGORY]="Number of Clients", VAR __ProcessLogID))))),CLIENT_IND,CLIENT_SURVEY)
  • v-yalanwu-msft's avatar
    v-yalanwu-msft
    Icon for Community Support rankCommunity Support

    Hi, OPS-MLTSD ;

    You could try to modify it.

    Clients =
    VAR CLIENT_IND =
        CALCULATE (
            SUM ( 'Table1'[Targets achieved to date (#)] ),
            FILTER (
                ALLEXCEPT ( 'Table1', 'Table1'[PROCESSLOGID] ),
                'Table1'[CATEGORY] = "Number of Clients"
            )
        )
    VAR CLIENT_SURVEY =
        CALCULATE (
            SUM ( Table4[INTAKE] ),
            FILTER (
                'Table1',
                AND ( 'Table2'[Report Submission#] = "Activity Report#1", Table3[type] = "5C" )
            )
        )
    RETURN
        IF (
            ISBLANK (
                CALCULATE (
                    COUNTROWS ( 'Table1' ),
                    FILTER (
                        ALLEXCEPT ( 'Table1', 'Table1'[PROCESSLOGID] ),
                        'Table1'[CATEGORY] = "Number of Clients"
                    )
                )
            ),
            CLIENT_SURVEY,
            CLIENT_IND
        )
    

    If not solve your problem,can you share simple data and the output what you what? 

    Best Regards,
    Community Support Team_ Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • OPS-MLTSD's avatar
      OPS-MLTSD
      Post Patron

      v-yalanwu-msft  thank you!  But this part of the dax is giving me an error for some reason:

       

      VAR CLIENT_SURVEY =
          CALCULATE (
              SUM ( Table4[INTAKE] ),
              FILTER (
                  'Table1',
                  AND ( 'Table2'[Report Submission#] = "Activity Report#1", Table3[type] = "5C" )