Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
vishnuprashanth
Helper III
Helper III

calculated column multiple if statements to find the maximum value

My dataset has different categories for the attribute column. I need to find the maximum value for each category for each person for each week. 

sin.PNG

 

I am trying to create a calculated column to get the maximum values for each category in Attribute for each person and each week. I tried and wrote the below query, which I know is not an efficient way of code & also it doesn't work...

Maximum values =

SWITCH (
TRUE (),
   
    ( 'TABLE'[Attribute] == 'total energy(J/Kg)'
     MAXX (
     FILTER (
        TABLE,
        TABLE[Athlete] = EARLIER ( TABLE[Athlete] )
            && TABLE[WeekNumber] <= EARLIER (TABLE[WeekNumber] ) -1 ),
    TABLE[value)],


     'TABLE'[Attribute] == 'total distance(m)'
     MAXX (
     FILTER (
        TABLE,
        TABLE[Athlete] = EARLIER ( TABLE[Athlete] )
            && TABLE[WeekNumber] <= EARLIER (TABLE[WeekNumber] ) -1),
    TABLE[value)]

       ...........("""like this for each category in the Attribute column""")
))


Can someone help me write the correct formula for having this calculated column? or creating a measure is the correct method to solve this problem? 

TIA. 

3 ACCEPTED SOLUTIONS

HI @vishnuprashanth

 

You can attribute condition to your formula

 

Max Energy Target =
MAXX (
    FILTER (
        TABLE,
        TABLE[attribute] = EARLIER ( TABLE[attribute] )
            && TABLE[athlete] = EARLIER ( TABLE[athlete] )
            && TABLE[WeekNumber]
                <= EARLIER ( TABLE[WeekNumber] ) - 1
    ),
    TABLE[total energy (J/kg)]
)

Regards
Zubair

Please try my custom visuals

View solution in original post

 

Max Value = 


CALCULATE (
MAX ( PT2[Value] ),
FILTER ( ALLEXCEPT ( PT2,PT2[Attribute], PT2[athlete] ), PT2[WeekSequence] = EARLIER ( PT2[WeekSequence] ) -1 )
)

I tried this code and it works correctly as I wanted I think. I will just and verify again. Thanks @Zubair_Muhammad 

View solution in original post

@vishnuprashanth

 

With the MEASURE i wrote you can  remove WeekNumber from ALLEXCEPT to get max value for all weeks

 

Maximum Value for All Weeks =
CALCULATE (
    MAX ( TableName[Value] ),
    ALLEXCEPT ( TableName, TableName[Attribute], TableName[Athlete] )
)

Regards
Zubair

Please try my custom visuals

View solution in original post

6 REPLIES 6
Zubair_Muhammad
Community Champion
Community Champion

HI @vishnuprashanth

 

Try this one both as a MEASURE and as a calculated column

 

Maximum Value =
CALCULATE (
    MAX ( TableName[Value] ),
    ALLEXCEPT (
        TableName,
        TableName[Attribute],
        TableName[Athlete],
        TableName[WeekNumber]
    )
)

 


Regards
Zubair

Please try my custom visuals

 

Max Value = 


CALCULATE (
MAX ( PT2[Value] ),
FILTER ( ALLEXCEPT ( PT2,PT2[Attribute], PT2[athlete] ), PT2[WeekSequence] = EARLIER ( PT2[WeekSequence] ) -1 )
)

I tried this code and it works correctly as I wanted I think. I will just and verify again. Thanks @Zubair_Muhammad 

@Zubair_Muhammad Thanks, Zubair.

Both are working on calculating the max values. But, It is updating & taking into account of the max values for the present week only. 

fe.PNG

 

But, I want to take the best(max values) of all the weeks, and compare that value with the present week's values. 

Say, for calculating the max energy alone, when my table was not unpivoted, I used the code below 

Max Energy Target = 
MAXX (
    FILTER (
        TABLE,
        TABLE[athlete] = EARLIER ( TABLE[athlete] )
            && TABLE[WeekNumber] <= EARLIER (TABLE[WeekNumber] ) -1
            
    ),
    TABLE[total energy (J/kg)]
)

Now, I want this same function to be applied to each category in the Attribute column.

HI @vishnuprashanth

 

You can attribute condition to your formula

 

Max Energy Target =
MAXX (
    FILTER (
        TABLE,
        TABLE[attribute] = EARLIER ( TABLE[attribute] )
            && TABLE[athlete] = EARLIER ( TABLE[athlete] )
            && TABLE[WeekNumber]
                <= EARLIER ( TABLE[WeekNumber] ) - 1
    ),
    TABLE[total energy (J/kg)]
)

Regards
Zubair

Please try my custom visuals

@vishnuprashanth

 

With the MEASURE i wrote you can  remove WeekNumber from ALLEXCEPT to get max value for all weeks

 

Maximum Value for All Weeks =
CALCULATE (
    MAX ( TableName[Value] ),
    ALLEXCEPT ( TableName, TableName[Attribute], TableName[Athlete] )
)

Regards
Zubair

Please try my custom visuals

Both the calculated column and measure works perfectly!! Thanks a lot, @Zubair_Muhammad

Just one small suggestion I require. I would be refreshing my data every day. So, Which one(column or measure) will be the best option for calculating these values? or both are gonna have the same effect? 

 

Thanks and Regards,
Vishnu

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.