Dax Calculation 1st and 2nd Best Amount

Hi All,

Here is a scenario , wherein I require help, I need two columns which basically gives me 1st best and 2nd best amount in the below format (screen shot attached). So basically the 2nd best value should get replaced with current best value once current amount greater than previous amount. I believe screen shot would give a clear understanding of the requirement.


If you look at the example : The current best weight is updated with new best weight if it is great than the current best weight and we have a column previous best weight which tracks the previous best weight.

Attaching the PBIX with data

@Harsh @Melissa @BrianJ @AntrikshSharma - It would great if you can provide your valuable guidance here.
MVNO Test (2).pbix (110.6 KB)
imp analysis.xlsx (15.9 KB)

Hi @Vishy,
Thank you very much for posting your query in the forum.
I have created the following DAX measures for each of the tables S1 and S2:

Total Weight S1 = SUM( S1[Total Weight] )

Current Best Weight S1 =

VAR _CurrentDate = SELECTEDVALUE( Dates[Date] )

VAR _VirtualTable = FILTER(

    ALL( S1 ),

    S1 [Date] <= _CurrentDate

)

VAR _Result = MAXX( _VirtualTable, [Total Weight] )

RETURN

IF( [Total Weight S1], _Result)

Current 2nd Best Weight S1 =

VAR _CurrentDate = SELECTEDVALUE( Dates[Date] )

VAR _VirtualTable = FILTER(

    ALL( S1 ),

    S1 [Date] <= _CurrentDate

)

VAR _Result = MINX(

    TOPN( 2, _VirtualTable, [Total Weight] ),

    [Total Weight]

)

RETURN

IF( [Total Weight S1], _Result)

In the images you have shared, is the calculation of the Current 2nd Best Weight measure for SUBSCRIPTION_ID = A correct? I think it does not follow the same logic as for the SUBSCRIPTIN_id = B

Regards,
MVNO Test (2)_JAFP.pbix (114.4 KB)

1 Like

@jafernandezpuga - Yes you are correct , it doesnt follow the same logic but i real world scenario both the subscription id would be in the same table so the logic should hand both the use case. Also if you see my screen shot of dataset 1 the 2nd best weight is not giving the correct result. Screen shot below

1 Like

Hi @Vishy.
When we are in the row of 04/01/2021 of table S1, which rows of this table are taken into account to calculate the second best weight?

Regards,

1 Like

@jafernandezpuga - It should display the current best amount itself because it has still not come to the row where there is an amt greater than current best amt , so from row 1to 5 it should show 1107.504. now at row 6 u see an amt great than current best amt has arrived now again the 2nd best amt becomes same 1107.504 when u reach row 9 , u notice now there is an amount greater than ur current best amount so then ur current best amount becomes ur 2nd best amount. Same pattern is applicable for S2 as well , so now the i made a mistake in dataset 2 screen shot , from row 1 to until it has Amt of 270 the 2nd best Amt should 240 instead of 250 or 210

1 Like

Hi @Vishy ,
If you can upload the image with the correct result from the table S2, I believe that by applying the 2nd Best Weight measure to a virtual table with the Current Best Weight column, we could obtain the desired result.

Regards

@jafernandezpuga - here is the image

Hi @Vishy,
In this last image, to calculate the 2nd Best Weight of the row on 3/1/2021, what Total Weight values are taken into account? If we are in the first row, another value is taken into account other than the value of the current row, which is 250. And in the second row of 4/1/2021, the values of 250 and 210 (current and earlier)?

Wouldn’t the value of 250 be for the entire column?

Regards.

@jafernandezpuga - I understood your point here, and current the pbix what you have shared gives the ideal result , but just from the feasibility check perspective is their any possibility that as per above image the the current best weight is 250 right , so it would display the next minimum value that is 240 for all the rows where you have 250 as the current best once there is something greater than 250 i.e 270 in above case so the 2nd best weight for those rows would be the immediate next max value which is 250 for all the rows of 270, kindly let me know whether this makes sense

Something similar to below screen shots
D1


D2

1 Like

Hi @Vishy ,
I have modified the measure for the calculation of the 2nd Best Weight based on the calculation of the Current Best Weight:

Current 2nd Best Weight S1 =

VAR _S1withBestWeight =

ADDCOLUMNS(

    ALL( S1 ),

    "BestWeight", [Current Best Weight S1]

)

VAR _CurrentBestWeight = [Current Best Weight S1]

VAR _NextBestDate = CALCULATE(

    MIN( Dates[Date] ),

    FILTER(

        _S1withBestWeight,

        [BestWeight] > _CurrentBestWeight

    )

)

VAR _NextBestDate2 = IF(

    ISBLANK( _NextBestDate ),

    CALCULATE( MAX( Dates[Date] ), ALL( Dates[Date] ) ),

    _NextBestDate

)

VAR _VirtualTable = FILTER(

    _S1withBestWeight,

    [BestWeight] <= _CurrentBestWeight &&

    [Date] < _NextBestDate2

)

VAR _Result = MINX(

    TOPN( 2, _VirtualTable, [Total Weight] ),

    [Total Weight]

)

RETURN

Regards,

MVNO Test (2)_JAFP.pbix (114.4 KB)

2 Likes

Hi @Vishy, did the response provided by @jafernandezpuga help in solving your query? 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 @jafernandezpuga - I am currently working on the same , would update by today EOD

@EnterpriseDNA - Am currently working on the same would update by today EOD