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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
vpoulson
Regular Visitor

Need to get the MAX date from a date dimension

I'm trying to figure out how to get the MAX date from a date dimension table.  I have three tabels: a fact table that has the datekey and locationkey, date dimension that has the datekey and the date and a location table that has the locationkey and location name.  For each location name I need to get the last date there is an entry in the fact table.  

For this data:

vpoulson_0-1713369154191.png

I want these results

vpoulson_1-1713369300439.png

Here are screenshots of the tables:

Locations

vpoulson_2-1713370308302.png

DimDates

vpoulson_3-1713370481107.png

MyValues fact table

vpoulson_4-1713370533937.png

If needed I can share the pbix file too.

 

2 ACCEPTED SOLUTIONS
qqqqqwwwweeerrr
Super User
Super User

Hi @vpoulson 

 

If you want to create a new column then you can use this formula:
Column = CALCULATE(MAX(table_1[Date]),FILTER('table_1',table_1[Location] = EARLIER(table_1[Location])))
If you want to create measure you can use this measure:

maxenddate = CALCULATE(MAX(table_1[EndDate]),REMOVEFILTERS(table_1[EndDate]))

qqqqqwwwweeerrr_0-1713371745499.png

 

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
Check for more intersing solution here: https://www.youtube.com/@letssolveproblem

Regards

View solution in original post

@qqqqqwwwweeerrr  Thanks for your contribution on this thread.

Hi @vpoulson ,

@qqqqqwwwweeerrr have provide a good suggestion. You can create different measures as below to get the max date from the dimension date table base on different fact tables, please find the details in the attachment.

MyMaxDate = 
VAR _locationkey =
    SELECTEDVALUE ( 'Locations'[LocationKey] )
VAR _maxdkey =
    CALCULATE (
        MAX ( 'MyValues'[DateKey] ),
        FILTER ( ALLSELECTED ( 'MyValues' ), 'MyValues'[LocationKey] = _locationkey )
    )
RETURN
    CALCULATE (
        MAX ( 'DimDates'[Date] ),
        FILTER ( 'DimDates', 'DimDates'[DateKey] = _maxdkey )
    )
TF2MaxDate = 
VAR _locationkey =
    SELECTEDVALUE ( 'Locations'[LocationKey] )
VAR _maxdkey =
    CALCULATE (
        MAX ( 'FactTable2'[DateKey] ),
        FILTER (
            ALLSELECTED ( 'FactTable2' ),
            'FactTable2'[LocationKey] = _locationkey
        )
    )
RETURN
    CALCULATE (
        MAX ( 'DimDates'[Date] ),
        FILTER ( 'DimDates', 'DimDates'[DateKey] = _maxdkey )
    )

vyiruanmsft_0-1713415281876.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
qqqqqwwwweeerrr
Super User
Super User

Hi @vpoulson 

 

If you want to create a new column then you can use this formula:
Column = CALCULATE(MAX(table_1[Date]),FILTER('table_1',table_1[Location] = EARLIER(table_1[Location])))
If you want to create measure you can use this measure:

maxenddate = CALCULATE(MAX(table_1[EndDate]),REMOVEFILTERS(table_1[EndDate]))

qqqqqwwwweeerrr_0-1713371745499.png

 

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
Check for more intersing solution here: https://www.youtube.com/@letssolveproblem

Regards

Thanks for the help.  For this simple data structure both your suggestions work.  The actual model I'm working with is a bit more complex.  The date and location dimensions are linked to several fact tables.  Also the fact table has several other categories that I need to filter by.  

If for example if have another fact table that also has a datekey and locationkey is there a way to tell which fact table I want the max date for?

What if my model is like this?

vpoulson_0-1713376348383.png

 

@qqqqqwwwweeerrr  Thanks for your contribution on this thread.

Hi @vpoulson ,

@qqqqqwwwweeerrr have provide a good suggestion. You can create different measures as below to get the max date from the dimension date table base on different fact tables, please find the details in the attachment.

MyMaxDate = 
VAR _locationkey =
    SELECTEDVALUE ( 'Locations'[LocationKey] )
VAR _maxdkey =
    CALCULATE (
        MAX ( 'MyValues'[DateKey] ),
        FILTER ( ALLSELECTED ( 'MyValues' ), 'MyValues'[LocationKey] = _locationkey )
    )
RETURN
    CALCULATE (
        MAX ( 'DimDates'[Date] ),
        FILTER ( 'DimDates', 'DimDates'[DateKey] = _maxdkey )
    )
TF2MaxDate = 
VAR _locationkey =
    SELECTEDVALUE ( 'Locations'[LocationKey] )
VAR _maxdkey =
    CALCULATE (
        MAX ( 'FactTable2'[DateKey] ),
        FILTER (
            ALLSELECTED ( 'FactTable2' ),
            'FactTable2'[LocationKey] = _locationkey
        )
    )
RETURN
    CALCULATE (
        MAX ( 'DimDates'[Date] ),
        FILTER ( 'DimDates', 'DimDates'[DateKey] = _maxdkey )
    )

vyiruanmsft_0-1713415281876.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.