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
Dazagard
Regular Visitor

DAX Lookup in same table

Hi,

I have been searching the forum for an answer, hopefully someone will be able to help me.

I have a table containing production data, each item has a unique container ID, with multiple processes, each process has a transaction Date and Time I have created a concatenated Date & Time column so the earliest process can be identified,

I need a column that shows the first process for each container ID but I'm not sure how to achieve this

 

Below is a sample of data and the expected result column i would like.

 

Comtainer ID

Process

Transaction Date

Transaction Time

TransDateTransTime

Pack Ref

First Process

164823

TRANTC

20211021

155500

20211021155500

50696164823

SAW2

164823

PK2

20211018

202500

20211018202500

50696164823

SAW2

164823

QATBMT

20211018

182600

20211018182600

50696164823

SAW2

164823

QATB

20211017

182100

20211017182100

50696164823

SAW2

164823

AGE2

20211017

182000

20211017182000

50696164823

SAW2

164823

SAW2

20211017

140300

20211017140300

50696164823

SAW2

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

There are a ton of different ways to do this. Here are a couple of drastically different possibilities.

 

FirstProcess =
VAR MinDateTime =
    CALCULATE (
        MIN ( Table1[TransDateTransTime] ),
        ALLEXCEPT ( Table1, Table1[Comtainer ID] )
    )
RETURN
    CALCULATE (
        SELECTEDVALUE ( Table1[Process] ),
        ALLEXCEPT ( Table1, Table1[Comtainer ID] ),
        Table1[TransDateTransTime] = MinDateTime
    )

 

FirstProcess = 
SELECTCOLUMNS (
    TOPN (
        1,
        FILTER ( Table1, Table1[Comtainer ID] = EARLIER ( Table1[Comtainer ID] ) ),
        Table1[TransDateTransTime], ASC
    ),
    "Process", Table1[Process]
)

 

View solution in original post

4 REPLIES 4
Dazagard
Regular Visitor

Thanks very much for your solutions, the first worked perfectly, I am slowly making the transition from Excel to Power BI, and Im unfamiliar with VAR rather than just copy and paste would you explain how the expression works so I can hopefully understand it apply the logic elsewhere when needed.

 

Thanks again for your help 

VAR is used to calculate an expression so that it can be re-used. It's super useful for making DAX more readable than Excel formulas.

 

At a high level, the measure calculates the minimal datetime for that ID and then looks up the process corresponding to that minimal datetime.

 

Note that ALLEXCEPT is used to remove all of the filter context, except for the ID, which exists due to a context transition from the row context to filter context.

Thanks for taking the time to reply and explain, I really appreciate your help

AlexisOlson
Super User
Super User

There are a ton of different ways to do this. Here are a couple of drastically different possibilities.

 

FirstProcess =
VAR MinDateTime =
    CALCULATE (
        MIN ( Table1[TransDateTransTime] ),
        ALLEXCEPT ( Table1, Table1[Comtainer ID] )
    )
RETURN
    CALCULATE (
        SELECTEDVALUE ( Table1[Process] ),
        ALLEXCEPT ( Table1, Table1[Comtainer ID] ),
        Table1[TransDateTransTime] = MinDateTime
    )

 

FirstProcess = 
SELECTCOLUMNS (
    TOPN (
        1,
        FILTER ( Table1, Table1[Comtainer ID] = EARLIER ( Table1[Comtainer ID] ) ),
        Table1[TransDateTransTime], ASC
    ),
    "Process", Table1[Process]
)

 

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.