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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
guyinazo
Helper I
Helper I

If exists in related table, get that value

and if not use another value.  Trying to wrap my head around the DAX for this.  I have a related  table of work orders and if that work order is found then I need to grab a date from that table.  If it is NOT found, then I need to just use the current date in the main table.

 

What I have right now is something like this: 

LastStartDate = VAR WOExists = CALCULATE(COUNTROWS(SecondTable,FILTER(SecondTable, SecondTable[WorkOrder] = EARLIER(MainTable[WorkOrder]))>1

RETURN If(WOExists,SecondTable[WorkOrder],MainTable[WorkOrder])

 

And I am doing this as a column, not a measure

 

Thanks for the information

2 ACCEPTED SOLUTIONS
AllisonKennedy
Super User
Super User

@guyinazo  The direction of the relationship is really important here - what does your data model view look like please? 

 

From your question it seems like you're wanting to get the last date from the Fact (WO transactions??) table and pull it into the Dimension table that has a list of each WO only once??

 

If so, you can try:

 

Last WO Date = 
MAXX( FILTER ( FactTable, DimensionTable[WOid] = FactTable[WOid]), FactTable[Date] )

 

Then you can nest that in an IF statement so if it's blank, return current date (not sure what you mean by current date in main table - again please provide more info on your relationships).


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

smpa01
Super User
Super User

@guyinazo  If you are creating a calculated column in MainTable, this should work out , works with or without relationship 

 

LastStartDate =
CALCULATE (
    CALCULATE (
        MAXX ( SecondTable, SecondTable[Date] ),
        TREATAS ( VALUES ( MainTable[WorkOrder] ), SecondTable[WorkOrder] )
    )
)

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

5 REPLIES 5
smpa01
Super User
Super User

@guyinazo  If you are creating a calculated column in MainTable, this should work out , works with or without relationship 

 

LastStartDate =
CALCULATE (
    CALCULATE (
        MAXX ( SecondTable, SecondTable[Date] ),
        TREATAS ( VALUES ( MainTable[WorkOrder] ), SecondTable[WorkOrder] )
    )
)

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
guyinazo
Helper I
Helper I

Seems to work, but I am getting a warning "Expressions that yield variant data-type cannot be used to define calculated columns"

AllisonKennedy
Super User
Super User

@guyinazo  The direction of the relationship is really important here - what does your data model view look like please? 

 

From your question it seems like you're wanting to get the last date from the Fact (WO transactions??) table and pull it into the Dimension table that has a list of each WO only once??

 

If so, you can try:

 

Last WO Date = 
MAXX( FILTER ( FactTable, DimensionTable[WOid] = FactTable[WOid]), FactTable[Date] )

 

Then you can nest that in an IF statement so if it's blank, return current date (not sure what you mean by current date in main table - again please provide more info on your relationships).


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

But now that I am re-reading your reply, let me try that

Not really.  So I have a main table with work orders and other fields.  This looks up two other tables that are related by Work Order.  This main table has a created column called LastStartDate.  If the work order exists IN the first table, use that LastStartDate, else use the LastStartDate from the second table.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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