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
ziyabikram96
Helper V
Helper V

Measure involving Second Last Date

Hi,

I am trying to create a measure where the difference between a vehicle's today's Start Odometer Reading and Previous day's End Odometer reading. Now, the previous day can be 7th Jan or any other day when the vehicle was used. 

ziyabikram96_0-1610113725244.png

I am using this variable to get the previous date and then using it in another variable's filter.

MINX(TOPN(2,'Driver Daily Data(Whole)','Driver Daily Data(Whole)'[Date]),'Driver Daily Data(Whole)'[Date])
Minx for the previous day and Maxx for the present day.
The problem is the date context. When I add dates in the Matrix column or choose a date from the filter, measure gets filtered down to that specific date. I have also tried other measures too but to no avail.
 
How can I solve it?
 
Thanks
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

15 REPLIES 15
V-pazhen-msft
Community Support
Community Support

@ziyabikram96 

Try the measure, below, I have take out the variable as measure to show the result.

 

Difference = 
var todaystart = CALCULATE(MAX([Vehicle startOdo]),FILTER(ALL('Table'),[Date]=MAXX(ALL('Table'),[Date])))
var previousend= CALCULATE(MAX([Vehicle endOdo]),FILTER(ALL('Table'),[Date]=CALCULATE(MAX([Date]),FILTER(ALL('Table'),[Date]<CALCULATE(MAX([Date]),ALL('Table'))))))

Return todaystart-previousend

 

V-pazhen-msft_0-1610434556225.png


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

Ashish_Mathur
Super User
Super User

Hi,

Share a dataset and show the expected result.


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

You can access the dataset using this link.

What I want is:

28th Dec's Start Odo - 24th Dec's End Odo

24th Dec's Start Odo - 22nd Dec's End Odo

Today's Start Odo - Previous Reading Day's End Odo

 

I would add dates in matrix column, Vehicle in the rows and the value would contains the Difference Value.

https://pentagonfoodgroup-my.sharepoint.com/:x:/g/personal/ziyab_ikram_pentagonfoodgroup_co_uk/EbJxA... 

Hi,

Why should it be 24th Dec's Start Odo - 22nd Dec's End Odo when there is data for 23rd Dec?  Should it not be 24th Dec's Start Odo - 23rd Dec's End Odo?  Also, for the same registration, ow can there there be 2 rows for the same Date?  Please clarify.


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

Hi,

I missed 23rd Dec so yeah, 24th Dec StartOdo - 23rd Dec EndOdo.

The challange is 28th Dec StartOdo - 24th Dec EndOdo.

A vehicle can be driven twice in a day but it happens occasionally. So, you can just assume a single 23rd Dec.

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/

Hi,

It does seem like the correct solution but I can't download it. It gets downloaded as a bunch of files instead of a single pbix file.

Hi,

Please try again.  It downloads just fine.


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

It is working. Thanks a lot @Ashish_Mathur  

You are welcome.


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

@ziyabikram96 , you might want to try following measures,

Current Day = MAX ( 'Driver Daily Data(Whole)'[Date] )

 

Previous Day = PREVIOUSDAY( 'Driver Daily Data(Whole)'[Date] )

 

 

As to "Second Last Date", here's a more verbose way in line with your measure,

 

Second Last Date = 
VAR __dd = [Current Day]
RETURN
    MINX (
        TOPN (
            2,
            CALCULATETABLE (
                'Driver Daily Data(Whole)',
                'Driver Daily Data(Whole)'[Date] <= __dd
            ),
            'Driver Daily Data(Whole)'[Date]
        ),
        'Driver Daily Data(Whole)'[Date]
    )

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Hi,

Second Last Date doesn't work when dates are added in matrix column and vehicles in the rows. Date context renders it ineffective. 

Greg_Deckler
Super User
Super User

@ziyabikram96 - You will need to incorporate an ALL into your filter context so that you effectively override the filter from the slicer in your measure.


@ 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...

When I select 8th Jan from the filter or have in the matrix column, I want it to take the Start Odo of 8th Jan and End Odo of 7th Jan. Instead it takes the End Odo of 8th Jan.

I have also used that before but didn't work.

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.