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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
unkCandyd
Frequent Visitor

Using Dax calculate the difference between two values comparing two dates

Hello, 

Iam beginner in power bi I would like your help. 

I have two data sets. 

the first one presents the actual values: 

unkCandyd_0-1668518955024.png

the second one is the target values

 

unkCandyd_1-1668592120009.png

 

 

I want to calculate actual- target values as the target can be retreived from the target table according to actual date. For example:

  • For id=1, first row I have the date 31-Jan so I should the target value of the date 31-march

My result will look like as: 

 

unkCandyd_0-1668592082924.png

 

I want to implement it in dax but i dont know how to retrieve the target value according to actual date. 

I hope you can help me to do that, 

thanks.

1 ACCEPTED SOLUTION

Hi @unkCandyd ,

 

You can try this method:

New two columns:

Time =
VAR _min1 =
    CALCULATE (
        MIN ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 1 )
    )
VAR _min3 =
    CALCULATE (
        MIN ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 3 )
    )
VAR _max1 =
    CALCULATE (
        MAX ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 1 )
    )
VAR _max3 =
    CALCULATE (
        MAX ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 3 )
    )
RETURN
    SWITCH (
        TRUE (),
        'actual'[Actual date] > _min1
            && 'actual'[Actual date] <= _max1
            && 'actual'[Id] = 1, _max1,
        'actual'[Actual date] <= _min1
            && 'actual'[Id] = 1, _min1,
        'actual'[Actual date] > _min3
            && 'actual'[Actual date] <= _max3
            && 'actual'[Id] = 3, _max3,
        'actual'[Actual date] <= _min3
            && 'actual'[Id] = 3, _min3
    )
Target Value = CALCULATE(MAX(Targets[target value]), FILTER('Targets', 'Targets'[target date] = 'actual'[Time] && 'actual'[Id] = 'Targets'[id]))

 

vyinliwmsft_0-1668678504406.png

 

Hope this helps you. Here is my PBIX file.

 

Best Regards,

Community Support Team _Yinliw

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

9 REPLIES 9
v-yinliw-msft
Community Support
Community Support

Hi unkCandyd,

 

You can try this method:

New a measure:

DateDiff =
CALCULATE (
    DATEDIFF ( MIN ( 'Targets'[target date] ), MAX ( 'Targets'[target date] ), DAY ),
    FILTER ( 'Targets', 'Targets'[id] )
)

Then new some columns:

MidDate =
CALCULATE (
    MIN ( 'Targets'[target date] ) + [DateDiff] / 2,
    FILTER ( 'Targets', 'actual'[Id] = 'Targets'[id] )
)
NeedDate =
IF (
    'actual'[MidDate] > [Actual date],
    CALCULATE (
        MIN ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 'actual'[Id] )
    ),
    CALCULATE (
        MAX ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 'actual'[Id] )
    )
)
Target Value =
CALCULATE (
    SUM ( Targets[target value] ),
    FILTER ( 'Targets', 'Targets'[target date] = 'actual'[NeedDate] )
)
Variance = [Actual value] - [Target Value]

The result is:

vyinliwmsft_0-1668589411705.png

 

Hope this helps you. Here is my PBIX file.

 

Best Regards,

Community Support Team _Yinliw

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

Hello thanks for your reply, here for the date of 18-May, the target value should be 100 as the date has already passed the 31 March: 

unkCandyd_0-1668590996234.png

Maybe I explained wrongly so after the date is passed we should get the new target of the next date

Hi @unkCandyd ,

 

Understood.

But i am a little confused, the 9/30/2022 and 12/15/2022 are both passed the 8/30/2022, and it used the target 8/30. Could you please explain the logic more to me?

 

Best Regards,

Community Support Team _Yinliw

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

Hello, Thank you for your reply. let me explain it more clearly.

For example: 

  • I have the target dates are 6-June with a target value of 100 and 31-Dec with a target value of 200,
  • and the actual date is 31-Aug, so the target date corresponding is 31-Dec. 

unkCandyd_0-1668591816462.png

 

So getting the target value according to the actual date and target date comparison 

Again, I may have explained wrongly,
I have succeeded to implement it in excel by using the match formula. but using Dax I am blocked..

Thank you again

Hi, @unkCandyd ,

 

Please fix this: 

Change the column NeedDate like this:

NeedDate =
IF (
    'actual'[MidDate] > [Actual date]
        && 'actual'[Actual date] < MIN ( 'Targets'[target date] ),
    CALCULATE (
        MIN ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 'actual'[Id] )
    ),
    CALCULATE (
        MAX ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 'actual'[Id] )
    )
)

 

The result is:

vyinliwmsft_0-1668592594589.png

Hope this helps you. Here is my PBIX file.

 

Best Regards,

Community Support Team _Yinliw

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

Hello @v-yinliw-msft

 

Sorry to bother you but i tried to implement the calculations. but i got one value that isnt correct. 

for example, here i should get the value of 30 June as the value date of April is before the 30-June.

unkCandyd_0-1668594521672.png

Here the pbix: https://drive.google.com/file/d/10c9yBPxfNfUpWG-UisERDaI67WAOIixm/view?usp=sharing

Thanks again

Hi @unkCandyd ,

 

You can try this method:

New two columns:

Time =
VAR _min1 =
    CALCULATE (
        MIN ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 1 )
    )
VAR _min3 =
    CALCULATE (
        MIN ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 3 )
    )
VAR _max1 =
    CALCULATE (
        MAX ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 1 )
    )
VAR _max3 =
    CALCULATE (
        MAX ( 'Targets'[target date] ),
        FILTER ( 'Targets', 'Targets'[id] = 3 )
    )
RETURN
    SWITCH (
        TRUE (),
        'actual'[Actual date] > _min1
            && 'actual'[Actual date] <= _max1
            && 'actual'[Id] = 1, _max1,
        'actual'[Actual date] <= _min1
            && 'actual'[Id] = 1, _min1,
        'actual'[Actual date] > _min3
            && 'actual'[Actual date] <= _max3
            && 'actual'[Id] = 3, _max3,
        'actual'[Actual date] <= _min3
            && 'actual'[Id] = 3, _min3
    )
Target Value = CALCULATE(MAX(Targets[target value]), FILTER('Targets', 'Targets'[target date] = 'actual'[Time] && 'actual'[Id] = 'Targets'[id]))

 

vyinliwmsft_0-1668678504406.png

 

Hope this helps you. Here is my PBIX file.

 

Best Regards,

Community Support Team _Yinliw

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

 

 

Greg_Deckler
Super User
Super User

@unkCandyd Try:

Target Value Measure = 
  VAR __ActualDate = MAX('Actuals'[Actual date])
  VAR __Targets = 
    ADDCOLUMNS(
      'Targets',
      "__DaysAway",ABS( ([target date] - __ActualDate) * 1.)
    )
  VAR __Min = MINX(__Targets,[__DaysAway])
  VAR __Result = MINX(FILTER(__Targets, [__DaysAway] = __Min),[target value])
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hello, Thank you for reply. bu I dont want to compare the days away, as it may give the wrong target value. 

For example: 

  • I have the target dates are 6-June with a target value 100 and 31-Dec with target value 200,
  • and the actual date is 31-Aug, so the target value for this date is 200. but using the suggested calculation it will give me 100. as it is comparing the days away

Again, I may have explained wrongly, what I want to be able to compare the two dates, so if the actual date <= target date then var = actual value- target value, also to compare all dates. 
I have succeeded to implement it in excel by using the match formula. but using Dax I am blocked..

Thank you again

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.