How to get Previous Measure Value using multiple fields for grouping and ordering

Hi Sam and Team -

I’m interested in expanding upon the concept to getting a Previous value with Time intelligence found here in a previous Enterprise DNA forum post Using variables to get previous values - DAX Calculations - Enterprise DNA Forum This works pretty well for me but the grouping and ordering needs to be based on more than one field and unfortunately I’m new to DAX. See image below where I concatenated the 3 fields into a calculated column called SM - That works fine for me. I’m needing to group by SUFFIX and rollup the RA Amount and the Premium Amount by Suffix, StepNumber and ModelName. RA Amount and Premium Amount are measures based on an Amount column. As you can see there are multiple measures and a ‘Last Amount’ for each measure. But if I add SUFFIX and/or StepNumber and/or ModelNumber to the table it breaks. This first one is filtered by SUFFIX = ‘CURRENT’

Ideally it should only pull last values from the current record SUFFIX value - this is the other thing I haven’t been able to do see screenshot below. I realize this is a bit different topic - I’ve been able to categorize the indexes but I’ve been unable to combine the concepts.

Any help would be greatly appreciated - I can work around the grouping by suffix by filtering but the first issue is most important. Thanks

1 Like

Hi @SMMCDONNELL, I noticed you didn’t provide a PBIX file. Providing one will help users and experts find a solution to your inquiry faster and better.

Check out this thread on Tools and Techniques for Providing PBIX Files with Your Forum Questions

Not completing your data may sometimes cause delay in getting an answer.

2 Likes

LastValueWithSuffixCategory_ForEntDNA.pbix (272.4 KB)

Attached is the corresponding PBIX file - since I first submitted this question I worked with others to get a partial solution. We’ve created a Groupby table then concatenated several columns to create a Key then created Index 1. I’m not sure this is the correct way to solve this. The part that is not working is getting last value only from current SUFFIX field. That is why the filter by suffix tab in the reports works and the second tab where it is showing all SUFFIX’s is not working. Thanks SMCDONNELL

1 Like

LastValueWithSuffixCategory_ForEntDNA.pbix (272.4 KB)

Hi,

Can you post the expected output it will be helpful .

Thanks,
Anurag

Hi Anurag -

The expected outcome would be similar to this however - see the transition between the CURRENT and Indirect and the INDIRECT and LOOKUP Suffix values? The INDIRECT LAST RA Amount value should NOT be pulling from CURRENT RA Amount it should be blank at the beginning because there is no previous within INDIRECT and the same with the LOOKUP section LAST RA Amount 2 should NOT be pulling from INDIRECT the first previous should be blank. See the red marks in the image below. Please let me know if this doesn’t make sense. Each SUFFIX values and totals should be self contained. Thanks in advance for looking at this.

Bumping this post for more visibility.

Hi @SMMCDONNELL ,

Thanks for posting this quesiton and providing detailed explanation. This is quite an interesting problem. I have worked on Last Prem and it seems that it is working as per your requirements. I have used variables instead of EARLIER function as working with variables is quite straigh-forward. Please see below snapshot of solution where I have done multiselection in filter and it is working fine.

Last Prem =
VAR currentsuffix =
    SELECTEDVALUE ( 'Result2'[SUFFIX] )
VAR currentSM =
    SELECTEDVALUE ( 'Result2'[SM] )
VAR modelName =
    SELECTEDVALUE ( 'Result2'[ModelName] )
VAR previousindex =
    CALCULATE (
        MAX ( Result2[Index 1] ),
        FILTER (
            ALLSELECTED ( 'Result2' ),
            Result2[Index 1] < MAX ( 'Result2'[Index 1] )
        ),
        FILTER ( ALLSELECTED ( 'Result2' ), 'Result2'[SUFFIX] = currentsuffix )
    )
RETURN
    CALCULATE (
        MAXX ( FILTER ( Result2, Result2[LOOKUP] = "Premium" ), [Activity Amt] ),
        FILTER (
            ALLSELECTED ( Result2 ),
            Result2[Index 1] = previousindex
                && 'Result2'[SUFFIX] = currentsuffix
        )
    )

Please also find attached PBIX with above formula.

LastValueWithSuffixCategory_ForEntDNA.pbix (269.2 KB)

Please feel free to get in touch in case you need something different.

Kind Regards,
Hafiz

2 Likes

I’ll take a look and get back to you - Thanks so much for taking the time to put this together for me.

1 Like

@hafizsultan - Thanks again - I applied your suggested DAX for the LAST values that I’m calculating and it is working great! Thank you I will mark as a Solution. After looking at your DAX the following alternative was suggested to me as more efficient. I would like your input into this as I know it is fewer lines of code but I’m not sure if one is better than the other?

Pre Prem Amt = CALCULATE(maxx(filter(Result2,Result2[ResultsReportingSubset_LOOKUP]=“Premium”),[Activity Amt]),

topn(1,

FILTER(ALLSELECTED(Result2),

Result2[Index 1]<max(Result2[Index 1])&& ‘Result2’[ResultsReportingSubset_SUFFIX] = SELECTEDVALUE(Result2[ResultsReportingSubset_SUFFIX])),

Result2[Index 1],desc))

2 Likes

Bumping this post for more visibility.

Hi @SMMCDONNELL ,

Excellent work and now you got 2 solutions :slight_smile: . The second solution which you mentioned is quite a smart solution. I am 100% sure that there will be more solutions for this problem which can be more efficient as everyone’s thought process is different.

As both solutions are giving same result, so you can select the measure which is taking less time. You can do this by using performance analyzer and refreshing your visual. In your case, I have tested by putting both measures separately and following are the results.

There is no much difference but you can test in your model and see which one works better in your environment.

Kind Regards,
Hafiz

2 Likes

@hafizsultan – Thanks so much for your help

1 Like

Thank you so much for that detailed answer @hafizsultan

We hope this helped you @SMMCDONNELL :slight_smile:

If not, how far did you get and what kind of help you need further?

If yes, kindly mark as solution the answer that solved your query.

Hi @SMMCDONNELL, we’ve noticed that no response has been received from you since a few days ago.

We just want to check if you still need further help with this post?

In case there won’t be any activity on it in the next few days, we’ll be tagging this post as Solved.

I have received several solutions and have no further need for additional solutions. Thanks :smiley: