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

DAX Measure - A Cumulative Running Total which returns a DATE when Target is achieved

Hi community,

 

Without using calculated columns as I would like to be able to pivot by project in Power BI using measures,
I need to find a way to extract the exact date when the cumulative running total measure has exceeded a target.

This target can be found repeating project by project in the same table, or alternatively as a RELATED lookup. I'm using Power BI but visualising measures in Power Pivot / Excel for testing purposes

 

 

PIVOT.png
The cumulative running total is calculated via a standard DAX pattern as such and as expected equals total sales as the measure includes all rows and is not yet filtered by TARGET.

CUMULATIVE RUNNING TOTAL:=CALCULATE(
SUM(SALES[AMOUNT]),
FILTER(SALES,
SALES[Sales_Date]>=MIN(SALES[Sales_Date])))

What I would like to achieve next is to find the cumulative running total measure by project exact ROW showing MINIMUM 'TARGET HIT DATE' where the sales TARGET has been exceeded by the cumulative total measure.

I have filtered the following table on PROJECT #3 with some manual Excel columns to show you the ROW which should be returned, and the VALUE.

EXCEL RESULTS.png

At present, this does not work. I'm hoping it helps show what I am trying to achieve:

HIT TARGET DATE:=CALCULATE(
MIN(SALES[Sales_Date]),
FILTER(SALES,[CUMULATIVE RUNNING TOTAL]>=RELATED(PROJECTS[TARGET])))

 

or if the target value repeats in the same table for each project as shown in the above screen capture

 

HIT TARGET DATE:=CALCULATE(
MIN(SALES[Sales_Date]),
FILTER(SALES,[CUMULATIVE RUNNING TOTAL]>=MIN(SALES[TARGET])))

I probably missing something really simple here, but I have not seen a solution so far for this one.
Thanks in advance!

1 ACCEPTED SOLUTION

Hi,

 

You may download my PBI file from here.

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

16 REPLIES 16
Ashish_Mathur
Super User
Super User

Hi,

 

Share the link from where i can download your formula driven Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

 

You may download my PBI file from here.

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Can anyone provide the measure in the solution?

Hi,

Are you not able to download my PBI dolution file?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

I don't think it is a good idea to get rid of a Date table.

Creating a calculated column with the TARGET_ACHIEVED column could be much better for the performance.
However, this code should work as a measure (didn't try it, bugs are possible!)

 

DateTargetReached :=
IF (
    HASONEVALUE ( Projects[Project_Name] ),
    VAR TargetRunningTotal =
        SELECTEDVALUE ( Projects[Target] )
    VAR FirstDateOverTarget =
        FIRSTNONBLANK (
            Sales[Date],
            IF (
                [Cumulative Running Total] >= TargetRunningTotal,
                1
            )
        )
    RETURN
        FirstDateOverTarget
)

 

thanks Marco, I've been trying to modify your solution for my own use case but not quite getting there. Hoping you might be able to help me with this?

 

To clarify: suppose that the OP was not interested in the date when the target was hit per project but in the earliest (and latest) date overall (across far more projects). When removing the IF plus HASONEVALUE condition, the Cumulative Running Total aggregates across projects and therefore the result would give a 'target hit date' that is earlier than reality so that doesn't work. What is the required modification to your DAX above that would make it work in the scenario I described above?

Hi,

Share some data, describe the question and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

hi @Ashish_Mathur and thanks for taking an interest in my question. My use case is a little different to the OP but in essence very similar. Suppose I have the following simple data table called Test, with two (or more) distinct IDs:

 

BastiaanBrak_0-1617842699342.png

 

Using DAX I have created a 'Running total' measure:

 

Running total =
CALCULATE(
    SUM('Test'[Value])/DISTINCTCOUNT('Test'[ID]),
    FILTER(
        ALLSELECTED('Test'[Date]),
        ISONORAFTER('Test'[Date], MAX('Test'[Date]), DESC)
    )
)

 

What I want to do is have two Cards on the dashboard indicating the earliest and latest date on which a particular target, say, 325 is exceeded. Graphically: 

 

BastiaanBrak_1-1617843010708.png

 

I've tried adapting the measure that @marcorusso created:

 

FirstDateTargetReached =
IF (
    HASONEVALUE ( Test[ID] ),
    VAR TargetRunningTotal = 325
    VAR FirstDateOverTarget =
        FIRSTNONBLANK (
            Test[Date],
            IF (
                [Running total] >= TargetRunningTotal,
                1
            )
        )
    RETURN
        FirstDateOverTarget
)

 

but this is not suitable here because it requires a single ID to be selected; unless an ID is filtered (e.g. by clicking an ID entry in the Legend) the Card will show (BLANK).

 

So the question is: which DAX measures enable the Card visuals to show the earliest and latest dates a given target is exceeded?

 

.pbix with the above is available here: https://easyupload.io/i9ubac

 

Many thanks, Bastiaan

 

Do you want this?

FirstDateTargetReached =
MINX (
    DISTINCT ( Test[ID] ),
    CALCULATE (
        VAR TargetRunningTotal = 325
        VAR FirstDateOverTarget =
            FIRSTNONBLANK ( Test[Date], IF ( [Running total] >= TargetRunningTotal, 1 ) )
        RETURN
            FirstDateOverTarget
    )
)

@marcorusso 

What is the benefit of using DISTINCT vs VALUES here? 

In case Test is on the one-side of a relationship, you don't want to iterate the BLANK value of an invalid relationship.

 

That makes sense. Thank you for the response!

@marcorusso just wanted to say thanks again! I modified your DAX solution for my specific use case. ICYMBI, the report is published here: Willow-carrot aphid migration forecast tool | AHDB

 

BastiaanBrak_0-1621635521488.png

 

Yes @marcorusso, thank you so much! 

 

BastiaanBrak_0-1617869928177.png

 

Hi,

I do not understand the meaning of "earliest and latest dates a given target is exceeded".  Based on that data that you have shared, please share your exact result expected.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur the exact result I expect based on my test data is that the Card visual with the 'FirstDateTargetReached' will display '27 March', given that was the earliest date on which the target was exceeded (see Tooltip): 

 

BastiaanBrak_0-1617846460727.png

 

Likewise, the Card visual with the 'LastDateTargetReached' should display '30 March', given that was the latest date on which the target was exceeded (again see Tooltip): 

BastiaanBrak_1-1617846531027.png

 

My actual dataset has more IDs than two but I'm assuming that the principle is the same.

 

Thanks Bastiaan

 

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.