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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
AbenaMina
New Member

Finding Latest Values Based on 2 columns in the same table

Hi Team,

I need help writing a DAX code that will give me the values in the Quantity Required field
Basically, for every distinct Station_TruckNumber, I want to retrieve the Quantity of the Max CalendarDate

AbenaMina_1-1652445485413.png

 

2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

Hi @AbenaMina 

Please confirm you have a date table. Are all other columns belong to the same table?

View solution in original post

@AbenaMina 

If you want to blank out the result of other rows then

 

Quantity Required =
VAR LastDate =
    CALCULATE (
        MAX ( TableName[CalendarDate] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] )
    )
VAR LastDateValue =
    CALCULATE (
        MAX ( TableName[Quantity] ),
        ALLEXCEPT (
            TableName,
            TableName[Station_TruckNumber] ),
            TableName[CalendarDate] = LastDate
    )
RETURN
    IF ( MAX ( TableName[CalendarDate] ) = LastDate, LastDateValue )

 

View solution in original post

11 REPLIES 11
AbenaMina
New Member

AbenaMina_1-1652463801082.png

 

 

Quantity Required =
VAR LastDate =
    CALCULATE (
        MAX ( TableName[CalendarDate] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] )
    )
VAR LastDateValue =
    CALCULATE (
        MAX ( TableName[Quantity] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] ),
        TableName[CalendarDate] = LastDate
    )
RETURN
    IF ( MAX ( TableName[CalendarDate] ) = LastDate, LastDateValue )

This one worked

AbenaMina
New Member

No, it didn't work
It doesn't work for the year field. I want for the date field, select the Quantity for any given maximum date for every distinct station_trucknumber.

Take a look at the screenshot,
I am highlighting distinct station_trucknumber, and its corresponding max calendardate so from these 2 combinations, I want the corresponding Quantity

@AbenaMina 

If you want to blank out the result of other rows then

 

Quantity Required =
VAR LastDate =
    CALCULATE (
        MAX ( TableName[CalendarDate] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] )
    )
VAR LastDateValue =
    CALCULATE (
        MAX ( TableName[Quantity] ),
        ALLEXCEPT (
            TableName,
            TableName[Station_TruckNumber] ),
            TableName[CalendarDate] = LastDate
    )
RETURN
    IF ( MAX ( TableName[CalendarDate] ) = LastDate, LastDateValue )

 

This one work, I made a little modification for the bolded part : TableName[CalendarDate] = LastDate

 

 

Quantity Required =
VAR LastDate =
    CALCULATE (
        MAX ( TableName[CalendarDate] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] )
    )
VAR LastDateValue =
    CALCULATE (
        MAX ( TableName[Quantity] ),
        ALLEXCEPT (
            TableName,
            TableName[Station_TruckNumber],
            TableName[CalendarDate]
        )
    )
RETURN
    IF ( MAX ( TableName[CalendarDate] ) = LastDate, LastDateValue )

 

 

@AbenaMina 
You are absolutely right. Also there is no need for calculate and ALLEXCEPT in 2nd variable

 

Quantity Required =
VAR LastDate =
    CALCULATE (
        MAX ( TableName[CalendarDate] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] )
    )
VAR LastDateValue =
    MAX ( TableName[Quantity] )
RETURN
    IF ( MAX ( TableName[CalendarDate] ) = LastDate, LastDateValue )

 

@AbenaMina 

I misunderstood the requirement. My mistake. 
please try

Quantity Required =
VAR LastDate =
    CALCULATE (
        MAX ( TableName[CalendarDate] ),
        ALLEXCEPT ( TableName, TableName[Station_TruckNumber] )
    )
RETURN
    CALCULATE (
        MAX ( TableName[Quantity] ),
        ALLEXCEPT (
            TableName,
            TableName[Station_TruckNumber],
            TableName[CalendarDate] = LastDate
        )
    )
AbenaMina
New Member

All the columns belong to the same table

@AbenaMina 

You have first to create a CalrndarYear colum. New Column >

CalrndarYear = YEAR ( TableName[CalrndarDate] )

then New Measure > 

Quantity Required =
CALCULATE (
    MAX ( TableName[Quantity] ),
    ALLEXCEPT ( TableName, TableName[Station_TruckNumber], TableName[CalendarYear] )
)
tamerj1
Super User
Super User

Hi @AbenaMina 

Please confirm you have a date table. Are all other columns belong to the same table?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.