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
zudar
Post Patron
Post Patron

Comparing the total increase of two different odometers on the same vehicle

Hi all,

 

I could really use some help constructing the right DAX-measure(s) for the following case..

 

I want to compare two different odometers for a number of vehicles. In order to make a fair comparison, the increase on the two different odometers should be of a period that starts on the same date and ends on the same date. I'm dealing with four different tables.

 

1) Vehicles:

 

VehicleIDModel
1A
2A
3B
etc..etc..

 

2) Calendar:

 

 

 

 

Calender = CALENDAR ( DATE ( 2000, 1, 1 ), NOW() )

 

 

 

 

So it looks like:

 

Date
...
1-1-2022
2-1-2022
3-1-2022
etc..*

*the calendar range is from 1-1-2000 until today with one date-value for each day

 

3) Odometer X

 

VehicleID*DateValue
121-12-20214
129-12-202126
12-1-202259
18-1-202280
118-1-2022122
13-2-2022151

*For simplification purposes, I'm only showing the data for one VehicleID

 

4) Odometer Y

 

VehicleID*DateValue
118-12-20212
129-12-202124
14-1-202261
112-1-202289
118-1-2022125
12-2-2022149

*For simplification purposes, I'm only showing the data for one VehicleID

 

These tables are connected like:

Vehicles[VehicleID] = 'Odometer X'[VehicleID]

Vehicles[VehicleID] = 'Odometer Y'[VehicleID]

Calendar[Date] = 'Odometer X'[Date]

Calendar[Date] = 'Odometer Y'[Date]

 

My desired end result is a table like this, with a row per VehicleID:

 

VehicleIDMatching Start DateMatching End DateX-increaseY-increaseDifference
129-12-202118-1-2022(122 - 26) -> 96(125 - 24) -> 101(101 - 96) -> 5
..................

 

If there's no match in either the Start Date or End Date I would like the table to return 'No Match' for all columns.

 

I hope this all makes sense. If something is unclear, or you would like to have a .pbix-file of this data, please let me know.

 

Your help is very much appreciated. Thank you!

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You could create a series of measures along the lines of

X Increase =
VAR CurrentVehicle =
    SELECTEDVALUE ( 'Vehicle'[Vehicle ID] )
VAR XDates =
    CALCULATETABLE ( VALUES ( 'Odometer X'[Date] ) )
VAR YDates =
    CALCULATETABLE ( VALUES ( 'Odometer Y'[Date] ) )
VAR SharedDates =
    INTERSECT ( XDates, YDates )
VAR Result =
    IF (
        COUNTROWS ( SharedDates ) < 2,
        "No match",
        VAR StartDate =
            MIN ( SharedDates )
        VAR EndDate =
            MAX ( SharedDates )
        VAR XStartValue =
            LOOKUPVALUE (
                'Odometer X'[Value],
                'Odometer X'[Date], StartDate,
                'Odometer X'[Vehicle ID], CurrentVehicle
            )
        VAR XEndValue =
            LOOKUPVALUE (
                'Odometer X'[Value],
                'Odometer X'[Date], EndDate,
                'Odometer X'[Vehicle ID], CurrentVehicle
            )
        RETURN
            XEndValue - XStartValue
    )
RETURN
    Result

View solution in original post

3 REPLIES 3
johnt75
Super User
Super User

You could create a series of measures along the lines of

X Increase =
VAR CurrentVehicle =
    SELECTEDVALUE ( 'Vehicle'[Vehicle ID] )
VAR XDates =
    CALCULATETABLE ( VALUES ( 'Odometer X'[Date] ) )
VAR YDates =
    CALCULATETABLE ( VALUES ( 'Odometer Y'[Date] ) )
VAR SharedDates =
    INTERSECT ( XDates, YDates )
VAR Result =
    IF (
        COUNTROWS ( SharedDates ) < 2,
        "No match",
        VAR StartDate =
            MIN ( SharedDates )
        VAR EndDate =
            MAX ( SharedDates )
        VAR XStartValue =
            LOOKUPVALUE (
                'Odometer X'[Value],
                'Odometer X'[Date], StartDate,
                'Odometer X'[Vehicle ID], CurrentVehicle
            )
        VAR XEndValue =
            LOOKUPVALUE (
                'Odometer X'[Value],
                'Odometer X'[Date], EndDate,
                'Odometer X'[Vehicle ID], CurrentVehicle
            )
        RETURN
            XEndValue - XStartValue
    )
RETURN
    Result

When I try this solution on my actual data, I get an error message "The MIN function only accepts a column reference as an argument"..

EDIT: When I replace MIN(...) with FIRSTDATE(...), and MAX(...) with LASTDATE(...), the proposed solution works!

 

Thank you! 🙂 

You could also use MINX(SharedDates, [Date])

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.