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
pieterhkruger
Frequent Visitor

Subtracting the "first" value in a column from all entries in that column; "first" by slicer

I have a table where I want to subtract the "first" value (or record) in a particular column from all entries in that same column, but where "first" is determined by a slicer, and namely by the earliest date.  To explain using a simple example:

 

Suppose I have the following table:

DATE           COLUMN1

1/1//2018   1

1/2/2018    4

1/3/2018    3

1/4/2018    6

 

Then, suppose I have slicer on the DATE, and the slicer runs from 1/1/2018 - 1/4/2018, I want to have the equivalent of:

DATE           MEASURE

1/1//2018   0

1/2/2018    3

1/3/2018    2

1/4/2018    5

(i.e.: 1 subtracted from all the values)

 

Suppose the slicer is set to 1/2/2018 - 1/4/2018, I want to have the equivalent of:

DATE           MEASURE

1/2/2018    0

1/3/2018    -1

1/4/2018    2

(i.e.: 4 subtracted from all the values)

 

Would this be possible at all?  And if so, how would I go about to create such a measure?

1 ACCEPTED SOLUTION

SOLVED:

 

Everything as before, but Measure changed to be as follows:

New Value =
VAR __min = MIN('TEST DATASET DATES'[Date])
VAR __minDate = MINX(FILTER(ALL('TEST DATASET'),[DATE]>=__min),[DATE])
VAR __minValue = MAXX(FILTER(ALL('TEST DATASET'),[DATE]=__minDate),[VALUE])
VAR __currentValue = MAX([Value])
VAR __currentDate = MAX([Date])
RETURN
IF(DATEDIFF(__minDate,__currentDate,DAY)>0||__minDate=__currentDate,__currentValue-__minValue,BLANK()
)

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

Perhaps something along the lines of:

 

Measure = 
VAR __min = MIN([DATE])
VAR __minValue = MAXX(FILTER('Table',[DATE]=__min),[COLUMN1])
VAR __currentValue = MAX([COLUMN1])
RETURN 
__currentValue - __minValue


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks for the reply. 

Not sure if I am doing something wrong, but I get values of 0 (adding up to a total of 9?).

I've used the following to test the concept:

TableTableDashboardDashboard

New Value =
VAR __min = MIN('TEST DATASET'[DATE])
VAR __minValue = MAXX(FILTER('TEST DATASET',[DATE]=__min),[Value])
VAR __currentValue = MAX([Value])
RETURN
__currentValue - __minValue

 

 

Not sure what is happening ...

 

You are going to need a disconnected Date for your slicer. Otherwise, the formula presented will always get the current value of the Date in the row for the __min variable. Row context. If you try without that using ALL then that will give you a different set of problems.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

SOLVED:

 

Everything as before, but Measure changed to be as follows:

New Value =
VAR __min = MIN('TEST DATASET DATES'[Date])
VAR __minDate = MINX(FILTER(ALL('TEST DATASET'),[DATE]>=__min),[DATE])
VAR __minValue = MAXX(FILTER(ALL('TEST DATASET'),[DATE]=__minDate),[VALUE])
VAR __currentValue = MAX([Value])
VAR __currentDate = MAX([Date])
RETURN
IF(DATEDIFF(__minDate,__currentDate,DAY)>0||__minDate=__currentDate,__currentValue-__minValue,BLANK()
)

The closest I got:

output.PNG

So I only need the first two rows to not display; the rest is working fine.  Any suggestions?

 

 

 

This is what I've done:

INPUT TABLE:

Input.PNG

SUMMARY TABLE:

TEST DATASET DATES = SUMMARIZE('TEST DATASET','TEST DATASET'[Date])

RELATIONSHIP:

Relationship.PNG

MEASURE:

New Value =
VAR __min = MIN('TEST DATASET DATES'[Date])
VAR __minValue = MINX(FILTER(ALL('TEST DATASET'),[DATE]>=__min),[VALUE])
VAR __currentValue = MAX([Value])
RETURN
__currentValue-__minValue

 

SLICER:

'TEST DATASET DATES'[dates]

TABLE visualisation:

'TEST DATASET'[dates]

'TEST DATASET'[value]

'TEST DATASET'[new value]

 

(Removing the ALL-function in the measure's formula above does not work; removing the relationship between the tables also does not work).

 

Thanks.  I'll play around with it ...

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.