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

DAX: Filter Measures by a Selected Date, that Lookup the corresponding value Between Dates

Hi all!

 

I have a Date filter on my screen on which I can select a certain reference date, and based on what date I select I want my measure 'ValuationA' and 'ValuationB' to return a certain value that was Valid during that selected date.

 

Table1 'ObjectValidity'

ObjectIDTypeValidFromValidToValuation
1A1-1-201531-12-20165000
1A1-1-201831-12-20187500
1B1-1-201531-12-20194000


Table2 'Calendar_NoRelation' (No relationship set to the other table. Hopefully we can work around this!).

 

Measure 'ValuationA'

 

 

ValuationA = 
    CALCULATE (
        MAX ( ObjectValidity[VALUATION] );
        FILTER (
            ( ObjectValidity);
            SELECTEDVALUE ( Calendar_NoRelation[Date] ) >= ObjectValidity[VALIDFROM] &&
            SELECTEDVALUE ( Calendar_NoRelation[Date] ) <= ObjectValidity[VALIDTO] &&
            ObjectValidity[Type] = "A"
        )
    )

 

 


Measure 'ValuationB'

 

 

ValuationB = 
    CALCULATE (
        MAX ( ObjectValidity[VALUATION] );
        FILTER (
            ( ObjectValidity);
            SELECTEDVALUE ( Calendar_NoRelation[Date] ) >= ObjectValidity[VALIDFROM] &&
            SELECTEDVALUE ( Calendar_NoRelation[Date] ) <= ObjectValidity[VALIDTO] &&
            ObjectValidity[Type] = "B"
        )
    )

 

 


 

Expected result:

When I select 1-1-2019 I expect:

ObjectIDValuationAValuationB
1NULL4000


When I select 1-1-2016 I expect:

ObjectIDValuationAValuationB
150004000

 

Here the columns are the following:

Column 1 ObjectID (column from Table1 'ObjectValidity')

Column 2 ValuationA (measure)

Column 3 ValuationB (measure)

 


Currently it does not show any value, anyone knows why that is and what you should change to make it work?

 

Kind regards,

Igor 

1 ACCEPTED SOLUTION

Hi @Titatovenaar ,

In your sample file, I have found some problems in your data table:

data table.png

When the [VALCODE_TYPEID] = 'MARKTWRD', the date in [VALIDTO] column is eariler than [VALIDFROM] column so the measure ValuationA will always show null under the previous logic.

Just modify the comparision logic of ValuationA:

ValuationA = 
VAR _date =
    SELECTEDVALUE ( Calendar_NoRelation[Date] )
VAR _vA =
    CALCULATE (
        MAX ( 'ObjectValuation'[Valuation] ),
        FILTER (
            ALL ( 'ObjectValuation' ),
            _date >= 'ObjectValuation'[VALIDTO]
                && _date <= 'ObjectValuation'[VALIDFROM]
                && 'ObjectValuation'[VALCODE_TYPEID] = "MARKTWRD"
        )
    )
RETURN
    IF ( _vA <> BLANK (), _vA, "NULL" )

result.png

 

Best Regards,
Yingjie Li

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

5 REPLIES 5
v-yingjl
Community Support
Community Support

Hi @Titatovenaar ,

You can modify your measure like this:

ValuationA = 
VAR _date =
    SELECTEDVALUE ( Calendar_NoRelation[Date] )
VAR _vA =
    CALCULATE (
        MAX ( 'ObjectValidty'[Valuation] ),
        FILTER (
            ALL ( 'ObjectValidty' ),
            _date >= 'ObjectValidty'[Valid From]
                && _date <= 'ObjectValidty'[Valid To]
                && 'ObjectValidty'[Type] = "A"
        )
    )
RETURN
    IF ( _vA <> BLANK (), _vA, "NULL" )
ValuationB = 
VAR _date =
    SELECTEDVALUE ( Calendar_NoRelation[Date] )
VAR _vB =
    CALCULATE (
        MAX ( 'ObjectValidty'[Valuation] ),
        FILTER (
            ALL ( 'ObjectValidty' ),
            _date >= 'ObjectValidty'[Valid From]
                && _date <= 'ObjectValidty'[Valid To]
                && 'ObjectValidty'[Type] = "B"
        )
    )
RETURN
    IF ( _vB <> BLANK (), _vB, "NULL" )

When you use date slicers you will get your expected result:

date11.pngdate1.png

 

Attached my sample file that hopes to help you, please check and try it: DAX: Filter Measures by a Selected Date, that Lookup the corresponding value Between Dates.pbix

 

Best Regards,
Yingjie Li

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Thanks for the fast reply, in your .pbix it indeed works!

When I apply it to my own .pbix file it does not work somehow, probably because of a bit more complex relations then the testdate I included I'm afraid.

 

I threw out most of the actual report to leave a subset of the actual data I really work with, can you maybe check why in this case it does not work? Somehow the current solution in the file for another problem, seems te be causing an issue for your solution to work with it simultaneously.

 

https://1drv.ms/u/s!AliMmiR5bX0C3QOmsFM0t9knUQt6?e=O4aMCw

 

Instead of the ObjectValidity, in my example in the testfile, it concerns the 'ObjectValuation' table!

ObjectValidity in the report is used for something, else, that already works fine. (it filters the rows of the table when making a certain selection. It only shows the Objects that are valid in a certain period. Now I want the Valuation to be shown with it).

 

Kind regards,

Igor

Hi @Titatovenaar ,

In your sample file, I have found some problems in your data table:

data table.png

When the [VALCODE_TYPEID] = 'MARKTWRD', the date in [VALIDTO] column is eariler than [VALIDFROM] column so the measure ValuationA will always show null under the previous logic.

Just modify the comparision logic of ValuationA:

ValuationA = 
VAR _date =
    SELECTEDVALUE ( Calendar_NoRelation[Date] )
VAR _vA =
    CALCULATE (
        MAX ( 'ObjectValuation'[Valuation] ),
        FILTER (
            ALL ( 'ObjectValuation' ),
            _date >= 'ObjectValuation'[VALIDTO]
                && _date <= 'ObjectValuation'[VALIDFROM]
                && 'ObjectValuation'[VALCODE_TYPEID] = "MARKTWRD"
        )
    )
RETURN
    IF ( _vA <> BLANK (), _vA, "NULL" )

result.png

 

Best Regards,
Yingjie Li

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Of course! Thanks a lot, this works like a charm now 🙂

Hi @Anonymous ,

That's my pleasure😄. If the above posts help, please kindly mark it as a solution to help others find it more quickly. Thanks!


Best Regards,
Yingjie Li

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.