PowerPivot calculated column - how to filter using an ordered list?

_Adrian

New Member
Joined
Jul 18, 2016
Messages
5
Hi,

I have data relating to meter readings that use meters with a 4-digit display, such as a domestic water meter or a gas meter. I’m trying to calculate usage by using successive meter readings, for example meter reading in Jan is 5000 and in July is 7000 shows usage of 2000 units over 6 months.

When the meter reaches 9999 it rolls over to 0000 and I have a flag which shows a meter rollover. Over a long period and with high usage a meter can rollover more than once, but I’m assuming it doesn’t rollover twice between consecutive meter reads.

My METERREADS table has columns CustomerID, MeterID, MeterReadDate, MeterRead and RolloverFlag, and I’ve so far added calculated columns for the number of rollovers to date, the meter reading with the rollovers and an index of the date order of MeterReadDate:

CustomerIDMeterIDMeterReadDateMeterReadRolloverFlagRolloversTDMeterReadwithRolloversReadOrder
Customer1Meter122/04/2013719007191
Customer1Meter130/04/201336920036922
Customer1Meter115/05/201398930098933
Customer1Meter131/05/2013624211162424
Customer1Meter130/06/2013881001188105
Customer1Meter104/07/2013012200006
Customer1Meter126/07/2013999902299997
Customer1Meter131/07/201389713308978

<tbody>
</tbody>

I have also set up a Calendar and linked the MeterReadDate with the Calendar[Date].

What I want to do is add another column which works out the usage since the last read, so it uses the MeterRead in that row and subtracts the MeterRead from ReadOrder minus 1.

I’m new to PowerPivot and taking some time to adjust to it. Measures might be better than calculated columns – I’m happy to be converted if there’s a different solution.

For my calculated columns I’ve used these:

RolloversTD = CALCULATE(SUM(METERREADS[RolloverFlag]),ALLEXCEPT(METERREADS, METERREADS[MeterId]),DATESBETWEEN('Calendar'[Date],FIRSTDATE(ALL('Calendar'[Date])),LASTDATE('Calendar'[Date])))

MeterReadwithRollovers = [MeterRead]+[RolloversTD]*10000

ReadOrder = CALCULATE(COUNTROWS(METERREADS),ALLEXCEPT(METERREADS,METERREADS[MeterId]),DATESBETWEEN('Calendar'[Date],FIRSTDATE(ALL('Calendar'[Date])),LASTDATE('Calendar'[Date])))

So, the bit where I’m currently stuck is to make the following ‘dynamic’ by picking up the previous read using the ReadOrder (and not static 1) for the relevant MeterID (and not the static “Meter1”):

Usage = [MeterReadwithRollovers] - CALCULATE(SUM(METERREADS[MeterRead]),FILTER(METERS,_METERS[MeterId]="Meter1"),FILTER(METERREADS,METERREADS[ReadOrder]=1))

(This uses a lookup table which defines the Meters.)

So, I guess my questions are:
1. How do I make the Usage calculation ‘dynamic’?
2. Am I OK using calculated columns or should I be using measures?
3. If yes, how do I re-write the expressions as measures?

Any help with this is much appreciated including any tips / suggestions to neaten up what I have so far.
Cheers,
Adrian
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi Adrian,

2/3.

  • I think you should have a Usage measure, and actually eliminate the 'working' columns from the final PowerPivot table (unless you are going to use them in some way beyond calculating other columns).
  • In your case, I think you can boil the table down to CustomerID, MeterID, MeterReadDate, Usage.
  • RolloverFlag, RolloversTD and ReadOrder could all be calculated as intermediate steps in Power Query (in order to calculate Usage) then deleted - have a play with that in Power Query.
  • If you are happy to treat all usage as having occurred on the MeterReadDate, then your usage measure can be a simple SUM.
    Code:
    Usage Sum := SUM ( METERREADS[Usage] )
1.
As an aside, if you did want to define these columns using DAX, you can simplify a bit.
For "cumulative" calculated columns, I generally use "<= EARLIER…" within CALCULATE.
For "previous" calculated columns where there is a single previous row, you can just use LOOKUPVALUE (which is good because it will return an error if multiple values are found).


Code:
[B]ReadOrder[/B]
=
CALCULATE (
    COUNTROWS ( METERREADS ),
    ALLEXCEPT ( METERREADS, METERREADS[MeterID] ),
    METERREADS[MeterReadDate] <= EARLIER ( METERREADS[MeterReadDate] )
)

[B]RolloversTD[/B]
=
CALCULATE (
    SUM ( METERREADS[RolloverFlag] ),
    ALLEXCEPT ( METERREADS, METERREADS[MeterID] ),
    METERREADS[MeterReadDate] <= EARLIER ( METERREADS[MeterReadDate] )
)

[B]MeterReadwithRollovers (unchanged from your version)[/B]
=
METERREADS[MeterRead]
    + METERREADS[RolloversTD] * 10000

[B]Usage (with LOOKUPVALUE)[/B]
=
METERREADS[MeterReadwithRollovers]
    - LOOKUPVALUE (
        METERREADS[MeterReadwithRollovers],
        METERREADS[MeterID], METERREADS[MeterID],
        METERREADS[ReadOrder], METERREADS[ReadOrder] - 1
    )


[B]Usage (with CALCULATE and MAX)[/B]
=
METERREADS[MeterReadwithRollovers]
    - CALCULATE (
        MAX ( METERREADS[MeterReadwithRollovers] ),
        METERREADS[ReadOrder]
            = EARLIER ( METERREADS[ReadOrder] ) - 1,
        ALLEXCEPT ( METERREADS, METERREADS[MeterID] )
    )

Owen :)
 
Last edited:
Upvote 0
Hi Owen,

Firstly a massive thank you :)

Re 2/3, this is a great idea. The helper columns aren't needed later so once Usage can be determined, they're unnecessary. At the moment they're bloating my data model. The problem is I think I'll need knowledge of M language to do the manipulations using Power Query. For now I need to get something working, bloated or not, but your suggestion is a great solution that I'll come back to.

Re 1, thanks again - I love the LOOKUPVALUE function - hadn't seen it before and it's perfect for this.

Owe you a pint or 2 if you come to the UK!

Cheers,

Adrian
 
Upvote 0

Forum statistics

Threads
1,215,204
Messages
6,123,630
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top