Forum Discussion

Ecan20's avatar
Ecan20
Frequent Visitor
5 years ago
Solved

Sum values until the first maximum value in another column per each category in another column

Hello DAX experts, 

I have a dataset like the table below.

I am looking for a DAX formula to return the sum of the values in DAY column until the values in DEPTH column reach the first maximum value, per each category listed in JOB column.

The desired result for job A for example would be 9 days = sum of days to reach the maximum DEPTH (500 in this case).

The job A takes 13 days in total, but I only want the days until the maximum depth is reached.

 

Thanks to anyone that will try to help!

 

JOBDAYDEPTH
A1100
A1150
A1200
A1300
A1300
A1300
A1400
A1400
A1500
A1500
A1500
A1500
A1500
B150
B150
B1100
B1200
B1300
B1350
B1400
B1550
B1550
B1550
B1550
B1550
  • Amedeo, 

    I modified it a bit and I got what I needed. Instead of the single days (second column) I used the cumulative days as input.

    This formula returned the amount of days required to reach the final depth

     

    VAR_RESULT =
    CALCULATE (
    MIN (Table[DAY]),FILTER(Table, Table[DEPTH] = (MAX(Table[DEPTH]))))
     
    Many thanks for taking the time to help, much appreciated. 
    Regards
    Elena
  • Anonymous's avatar
    Anonymous
    5 years ago

    No need to apologize Ecan20 🙂 

    Yes, that would be a single measure, in which you can define multiple variables and reference them in the same measure. I assumed you were using a fairly recent version of Power BI Desktop/Excel, in which you can define variables.

    Are you using a recent version of Power BI Desktop/Excel/SQL Server Analysis Services?

    I don't want to ask a stupid question either, but have you forgot to provide a name for your measure? That's the first thing I can think of.

    If you miss that, you'll get an error:



    With measure name instead:

     

11 Replies

  • AmedeoM's avatar
    AmedeoM
    Regular Visitor

    Hello,


    this will return 9 for all the rows in your table (I don't know if that's the output you wanted - if it's not, please provide a demo file and a detailed explanation of what you would like the measure to compute and I'll try to fix it):

    Measure :=
    
    VAR _MaxDepth =
    	CALCULATE (
    		MAX ( Table[Depth] ),
    		REMOVEFILTERS ( Table[Depth] )
    	)
    
    VAR _Result = 
    	CALCULATE (
    		SUM ( Table[Day] ),
    		Table[Depth] < _MaxDepth
    	)
    
    RETURN
    _Result



    Please let me know if this works 🙂

    • Ecan20's avatar
      Ecan20
      Frequent Visitor

      Amedeo, 

      I modified it a bit and I got what I needed. Instead of the single days (second column) I used the cumulative days as input.

      This formula returned the amount of days required to reach the final depth

       

      VAR_RESULT =
      CALCULATE (
      MIN (Table[DAY]),FILTER(Table, Table[DEPTH] = (MAX(Table[DEPTH]))))
       
      Many thanks for taking the time to help, much appreciated. 
      Regards
      Elena
      • Anonymous's avatar
        Anonymous
        Not applicable

        Hello Ecan20 ,

         

        happy to know that it helped 🙂

        Just be aware that usually passing a whole table as a filter to CALCULATE is not a good idea because it could negatively affect performance (and sometimes also return unpredictable results as well). Perhaps it would be better if you used:

         

        KEEPFILTERS ( Table[DEPTH] = MAX ( Table[DEPTH] ) ) 

         

        as a filter, insead.

         

         

        Best regards,

        Amedeo

  • Anonymous's avatar
    Anonymous
    Not applicable

    This task does not have a solution since you have not stated how values are ordered. Without a column that tells you the order of values, the description is meaningless.

    • Ecan20's avatar
      Ecan20
      Frequent Visitor

      Hi daxer, thanks for the hint. But forgive me, I am a beginner and I don't understand what you mean by "the order of the values". Which values? What would you suggest me doing? Thanks! 

      • daxer-almighty's avatar
        daxer-almighty
        Icon for Solution Sage rankSolution Sage

        DAX does not have an intrinsic notion of order. All tables in the model are unordered, just like tables in SQL. So, in order to calculate something that resembles "running totals," you have to have a column that orders rows in the table. Otherwise, the result of aggregation will be indetermined/random.