# Vlookup in Powerpivot?



## cajste (Jan 23, 2013)

Hi!

I have to tables, one voyage table containing all the voyages our different trucks has done with departure and arrival time for each voyage and the other table containing GPS data taken every 64 seconds for each truck, example below. 

What I want to know is what ListItemID each GPS point is refereable to? I would like to have the  In excel I would use a vlookup but how do I do that in powerpivot?

Brgds,
Caj


ListItemIDVehicleRealDepartureRealArrival9177abe0-9c38-4baa-af2f-7cc6eb063ba9142.0012012-12-01 13:562012-12-01 14:16ab3c3ae7-eeff-4600-a3fb-173e4cfef706142.0012012-12-01 14:142012-12-02 00:434f41977e-6f19-4efc-ac46-3946c3bdfa17142.0012012-12-02 04:382012-12-02 06:44bf05b049-6805-4fc4-b909-ffa91e08ca36142.0012012-12-02 17:352012-12-02 17:48bf05b049-6805-4fc4-b909-ffa91e08ca37142.0022012-12-02 19:352012-12-02 23:48

<tbody>

</tbody><colgroup><col><col><col><col></colgroup>

*Vehicle**GPS TIME*142.0012012-12-01 14:02:21142.0012012-12-01 14:02:25142.0012012-12-01 14:02:29142.0012012-12-01 14:02:33142.0012012-12-01 14:02:37142.0012012-12-01 14:02:41142.0012012-12-01 14:02:45142.0012012-12-01 14:02:49142.0012012-12-01 14:02:53142.0012012-12-01 14:02:57142.0012012-12-01 14:03:01142.0012012-12-01 14:03:05142.0012012-12-01 14:03:09142.0012012-12-01 14:03:13142.0012012-12-01 14:03:17142.0012012-12-01 14:03:21142.0012012-12-01 14:03:25142.0012012-12-01 14:03:29

<tbody>

</tbody>


----------



## MD610 (Jan 23, 2013)

This is a typical banding scenerio using a disconnected table with a minor variation to account for various vehicles.

See aritcle below for fundamentals:

Alberto Ferrari : Banding with PowerPivot

For your situation you have to check if the GPS time falls within the ListItem time range and if the vehicle matches.

Your data sample was not good for testing because all GPS records you provided only fall within one ListItem range, plus you only provided GPS data for 1 vehicle.  I added some additional records of my own to test.

First, you need a unique ID for the GPS records to use in your final pivot.  You might actually have this in your data and just didn't provide it with your sample.  I used a calculated column with just a concatenation of Vehicle and GPS TIME.

Then you just need one measure:


[ListItemBand]:=CALCULATE(VALUES('ListItem'
[ListItemID]),FILTER('ListItem', MAX('GPSData'[GPS TIME])<= 'ListItem'[RealArrival] && MAX('GPSData'[GPS TIME])>='ListItem'[RealDeparture] && 'ListItem'[Vehicle]=MAX('GPSData'[Vehicle])))

Use the GPS ID in your Pivot Row Labels and this measure in your values.  I think this should work.


----------



## cajste (Jan 23, 2013)

Thanks, will try ASAP!


----------



## cajste (Jan 25, 2013)

I've been looking at this and I'm not sure if your suggestion solves the problem. The examples above are only parts of the actual tables, the first table also contains customerID, routeID, amount of cargo, from destination and final destination etc., and the second table also contains data on fuel consumption, altitude, longitud etc.

What I would like to do is to create a link between the two tables so that I can group/filter on specific routes, customers etc. As far as I understand it the way to do this is to find the ListItemID for each row in the Vehicle/GPS TIME based on the vehicle and GPS TIME in the second table and the intervall between Real departure and Real Arrival for each vehicle in the first table. When that is done I can use =Relate() to get Customers, Route etc. into the GPS data table. 

I'm new to PowerPivot so this might be a very bad way of doing this, I appriciate any help! 

//Caj


----------



## cajste (Jan 25, 2013)

I think I'm beginning to understand this, but I have a problem with the formula. It looks like below and does not return anything and gives no error indication. Have I done anything wrong?

//Caj

=CALCULATE(VALUES(V_LocoUsage[Loco2ListItemID]);FILTER(V_LocoUsage;MAX('MGW DATA'[EVENT TIME])<=V_LocoUsage[RealArrival]&&MAX('MGW DATA'[EVENT TIME])>=V_LocoUsage[RealDeparture]&&V_LocoUsage[LocoNo2]=MAX('MGW DATA'[Loco2])))


----------



## MD610 (Jan 25, 2013)

I think maybe I misunderstood what you needed since I didn't realize all the other fields involved.  

What about modifying the formula above a bit and using it as a Calculated Column in your GPS data instead of a measure?  You could basically make a calculated column for every field you want in the GPS data.  Modifying my original equation, it would be:
=CALCULATE(VALUES('ListItem'

[ListItemID]),FILTER('ListItem', 'GPSData'[GPS TIME]<= 'ListItem'[RealArrival] && 'GPSData'[GPS TIME]>='ListItem'[RealDeparture] && 'ListItem'[Vehicle]='GPSData'[Vehicle]))

This would bring the appropriate ListItemID into your GPS table.

You can then make another Calculated Column and change the VALUES() field to another field from ListItems to add that column to GPS data.  So CustomerID would be:

=CALCULATE(VALUES('ListItem'[CustomerID]),FILTER('ListItem', 'GPSData'[GPS TIME]<= 'ListItem'[RealArrival] && 'GPSData'[GPS TIME]>='ListItem'[RealDeparture] && 'ListItem'[Vehicle]='GPSData'[Vehicle]))


----------



## cajste (Jan 28, 2013)

Thank you so much! And sorry for not mentioning all the other columns from the beginning.

I tried using it as a calculated column but then realized that the database in i bad condition with for example overlaping time intervals per vehicle, and the formula returns errors  because of that. Haven't figured out how to handle that yet.

If I understand correctly, your solutions covers the case where the start and end date for each row is in different columns. Is there a similar way to handle a case where there is only a column with start dates for each row, like for example with the two tabels below? I would like to plot consumption per km with speed on the X-axis. Is it possible?

Brgds,
Caj




Table1
Table2
Vehicle
Date
Fuel Consumption
Vehicle
Date
Distance
Duration (h)
Speed
1
2012-12-01 13:50
3
142.001
2012-12-01 13:57
0,276
0,018
16
1
2012-12-01 13:55
1
142.001
2012-12-01 13:58
0,415
0,018
23
1
2012-12-01 14:00
43
142.001
2012-12-01 13:59
0,590
0,018
33
1
2012-12-01 14:05
44
142.001
2012-12-01 14:00
0,575
0,018
32
1
2012-12-01 14:10
13
142.001
2012-12-01 14:01
0,573
0,018
32
1
2012-12-01 14:15
43
142.001
2012-12-01 14:02
0,535
0,018
30
1
2012-12-01 14:20
197
142.001
2012-12-01 14:04
0,545
0,018
31
1
2012-12-01 14:25
134
142.001
2012-12-01 14:05
0,552
0,018
31
1
2012-12-01 14:30
130
142.001
2012-12-01 14:06
0,568
0,018
32
142.001
2012-12-01 14:07
0,557
0,018
31
142.001
2012-12-01 14:08
0,498
0,018
28
142.001
2012-12-01 14:09
0,430
0,018
24
142.001
2012-12-01 14:10
0,430
0,018
24
142.001
2012-12-01 14:11
0,419
0,018
24
142.001
2012-12-01 14:12
0,494
0,018
28
142.001
2012-12-01 14:13
0,307
0,018
17
142.001
2012-12-01 14:14
0,169
0,018
9
142.001
2012-12-01 14:15
0,600
0,018
34
142.001
2012-12-01 14:16
1,103
0,018
62
142.001
2012-12-01 14:17
1,459
0,018
82
142.001
2012-12-01 14:18
1,712
0,018
96


<tbody>

</tbody>


----------



## cajste (Feb 1, 2013)

Sorry for all the qeustions...

I cleaned the data and got the calculated column to work nicely. However, when I try to create a relationsship between the two columns I get an error message saying that a circular dependency was discovered. The point of the banding was to be able to relate the two tables to each each others.  

Also tried to create a third table with that column with relations to the two the tables and avoid a direct relationsship between the two, but gor the same error. o

Any suggestions on how to solve this?

Brgds,
Caj


----------



## MD610 (Feb 1, 2013)

Do you still need the relationship if you bring all the fields into one table using Calculated Columns?  Use the new Calculated columns for rows and slicers.


----------



## cajste (Feb 6, 2013)

Of course, I should have seen that one, thanks!

Once again, thanks alot for the help, made a huge difference!

//Caj


----------

